220 lines
8.3 KiB
C#
220 lines
8.3 KiB
C#
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using Unity.AI.Navigation;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.AI;
|
||
|
using UnityEngine.XR.ARFoundation;
|
||
|
using UnityEngine.XR.ARSubsystems;
|
||
|
using TMPro;
|
||
|
|
||
|
public class NewIndoorNav : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] private Transform player;
|
||
|
[SerializeField] private ARTrackedImageManager m_TrackedImageManager;
|
||
|
[SerializeField] private GameObject trackedImagePrefab;
|
||
|
[SerializeField] private LineRenderer line;
|
||
|
|
||
|
[SerializeField] private TextMeshProUGUI distanceText;
|
||
|
|
||
|
private List<NavigationTarget> navigationTargets = new List<NavigationTarget>();
|
||
|
private NavMeshSurface navMeshSurface;
|
||
|
private NavMeshPath navMeshPath;
|
||
|
|
||
|
private GameObject navigationBase;
|
||
|
private ARTrackedImage currentTrackedImage;
|
||
|
|
||
|
private string currentImageName = "front";
|
||
|
public static int targetNumber = StoreDataScript.targetNumber;
|
||
|
private float m_Distance;
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
navMeshPath = new NavMeshPath();
|
||
|
|
||
|
// disable screen dimming
|
||
|
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
if (navigationBase != null && navigationTargets.Count > 0 && navMeshSurface != null)
|
||
|
{
|
||
|
// Check if there are any tracked images
|
||
|
if (m_TrackedImageManager.trackables.count > 0)
|
||
|
{
|
||
|
// Use a foreach loop to get the first tracked image
|
||
|
foreach (var trackedImage in m_TrackedImageManager.trackables)
|
||
|
{
|
||
|
currentImageName = trackedImage.referenceImage.name; // Get the name of the tracked image
|
||
|
Debug.Log(currentImageName);
|
||
|
|
||
|
// Determine which navigation target to use based on the tracked image
|
||
|
Transform targetTransform = null;
|
||
|
|
||
|
targetTransform = navigationTargets[targetNumber].transform;
|
||
|
|
||
|
// If a valid target is found, calculate the path
|
||
|
if (targetTransform != null)
|
||
|
{
|
||
|
NavMesh.CalculatePath(player.position, targetTransform.position, NavMesh.AllAreas, navMeshPath);
|
||
|
if (navMeshPath.status == NavMeshPathStatus.PathComplete)
|
||
|
{
|
||
|
line.positionCount = navMeshPath.corners.Length;
|
||
|
line.SetPositions(navMeshPath.corners);
|
||
|
|
||
|
m_Distance = Vector3.Distance(player.position,targetTransform.position);
|
||
|
distanceText.text = m_Distance.ToString("F2");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
line.positionCount = 0;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// If no valid target is found, clear the line
|
||
|
line.positionCount = 0;
|
||
|
}
|
||
|
|
||
|
// Break after processing the first tracked image
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// If no tracked images are available, clear the line
|
||
|
line.positionCount = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnEnable() => m_TrackedImageManager.trackablesChanged.AddListener(OnChanged);
|
||
|
|
||
|
private void OnDisable() => m_TrackedImageManager.trackablesChanged.RemoveListener(OnChanged);
|
||
|
|
||
|
private void OnChanged(ARTrackablesChangedEventArgs<ARTrackedImage> eventArgs)
|
||
|
{
|
||
|
Debug.Log("OnChanged event triggered.");
|
||
|
Debug.Log(targetNumber);
|
||
|
|
||
|
// Handle added images
|
||
|
foreach (var newImage in eventArgs.added)
|
||
|
{
|
||
|
string newImageName = newImage.referenceImage.name; // Get the name of the new image
|
||
|
Debug.Log($"New Image Added: {newImageName}");
|
||
|
|
||
|
// Stop using the previous image
|
||
|
if (currentTrackedImage != null && currentTrackedImage != newImage)
|
||
|
{
|
||
|
Debug.Log($"Stopping use of previous image: {currentTrackedImage.referenceImage.name}");
|
||
|
// Destroy any associated game objects here
|
||
|
if (navigationBase != null)
|
||
|
{
|
||
|
Destroy(navigationBase);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Set the new image as the current tracked image
|
||
|
currentTrackedImage = newImage;
|
||
|
currentImageName = newImageName;
|
||
|
|
||
|
// Instantiate a new navigationBase
|
||
|
navigationBase = GameObject.Instantiate(trackedImagePrefab);
|
||
|
navigationTargets = navigationBase.transform.GetComponentsInChildren<NavigationTarget>().ToList();
|
||
|
navMeshSurface = navigationBase.transform.GetComponentInChildren<NavMeshSurface>();
|
||
|
}
|
||
|
|
||
|
// Handle updated images
|
||
|
foreach (var updatedImage in eventArgs.updated)
|
||
|
{
|
||
|
if (navigationBase == null)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
currentImageName = updatedImage.referenceImage.name; // Update currentImageName
|
||
|
Debug.Log($"Image Updated: {currentImageName}");
|
||
|
|
||
|
// Get the current position of navigationBase
|
||
|
Vector3 currentPosition = navigationBase.transform.position;
|
||
|
|
||
|
// Calculate the new position based on the updated image's position
|
||
|
Vector3 newPosition = currentPosition;
|
||
|
if (currentImageName == "front")
|
||
|
{
|
||
|
newPosition = updatedImage.transform.position + new Vector3(-1.22f, -1f, 13f);
|
||
|
}
|
||
|
else if (currentImageName == "frontright")
|
||
|
{
|
||
|
newPosition = updatedImage.transform.position + new Vector3(-8.65f, -1f, 6.25f);
|
||
|
}
|
||
|
else if (currentImageName == "rearright")
|
||
|
{
|
||
|
newPosition = updatedImage.transform.position + new Vector3(18f, -1f, 6.25f);
|
||
|
}
|
||
|
else if (currentImageName == "frontleft")
|
||
|
{
|
||
|
newPosition = updatedImage.transform.position + new Vector3(9.65f, -1f, 7.28f);
|
||
|
}
|
||
|
else if (currentImageName == "rearleft")
|
||
|
{
|
||
|
newPosition = updatedImage.transform.position + new Vector3(-17f, -1f, 7.28f);
|
||
|
}
|
||
|
else if (currentImageName == "rear")
|
||
|
{
|
||
|
newPosition = updatedImage.pose.position + new Vector3(0.78f, -1f, 19.62f);
|
||
|
}
|
||
|
|
||
|
// Check if the new position is significantly different from the current position
|
||
|
if (Vector3.Distance(currentPosition, newPosition) > 0.1f)
|
||
|
{
|
||
|
if (currentImageName == "front")
|
||
|
{
|
||
|
navigationBase.transform.SetPositionAndRotation(newPosition, Quaternion.Euler(0, -updatedImage.transform.rotation.eulerAngles.x, 0));
|
||
|
}
|
||
|
else if (currentImageName == "frontright" || currentImageName == "rearright")
|
||
|
{
|
||
|
navigationBase.transform.SetPositionAndRotation(newPosition, Quaternion.Euler(0, 90 + updatedImage.transform.rotation.eulerAngles.x, 0));
|
||
|
}
|
||
|
else if (currentImageName == "frontleft" || currentImageName == "rearleft")
|
||
|
{
|
||
|
navigationBase.transform.SetPositionAndRotation(newPosition, Quaternion.Euler(0, 270 + updatedImage.transform.rotation.eulerAngles.x, 0));
|
||
|
}
|
||
|
else if (currentImageName == "rear")
|
||
|
{
|
||
|
navigationBase.transform.SetPositionAndRotation(newPosition, Quaternion.Euler(0, updatedImage.pose.rotation.eulerAngles.x, 0));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Handle removed images if necessary
|
||
|
foreach (var removedImage in eventArgs.removed)
|
||
|
{
|
||
|
Debug.Log($"Image Removed: {removedImage.Value.referenceImage.name}");
|
||
|
if (removedImage.Value == currentTrackedImage)
|
||
|
{
|
||
|
currentTrackedImage = null;
|
||
|
currentImageName = "";
|
||
|
if (navigationBase != null)
|
||
|
{
|
||
|
Destroy(navigationBase);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void HandleDropdown(int val)
|
||
|
{
|
||
|
targetNumber = val - 1;
|
||
|
}
|
||
|
|
||
|
public void DestroyNavigationBase()
|
||
|
{
|
||
|
if (navigationBase != null)
|
||
|
{
|
||
|
Debug.Log("Destroying!!");
|
||
|
Destroy(navigationBase);
|
||
|
}
|
||
|
}
|
||
|
}
|