53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.AI;
|
|
|
|
public class NavigationPointController : MonoBehaviour
|
|
{
|
|
public Transform target; // Cel do œledzenia
|
|
public GameObject targetIconPrefab; // Prefab obiektu 3D z ikon¹ celu
|
|
|
|
private GameObject targetIcon;
|
|
private Text distanceText;
|
|
private NavMeshAgent navMeshAgent;
|
|
|
|
void Start()
|
|
{
|
|
// Tworzymy ikonê celu (billboard)
|
|
if (targetIconPrefab != null)
|
|
{
|
|
targetIcon = Instantiate(targetIconPrefab, transform.position, Quaternion.identity);
|
|
targetIcon.transform.SetParent(transform); // Ustawiamy ikonê jako dziecko tego GameObjectu
|
|
}
|
|
|
|
// Pobieramy komponenty Text i NavMeshAgent z tego GameObjectu
|
|
distanceText = GetComponentInChildren<Text>();
|
|
navMeshAgent = GetComponent<NavMeshAgent>();
|
|
|
|
// Ustawiamy cel na podstawie transformacji podanego obiektu
|
|
if (target != null)
|
|
{
|
|
navMeshAgent.SetDestination(target.position);
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (target == null)
|
|
return;
|
|
|
|
// Obliczamy odleg³oœæ do celu
|
|
float distance = Vector3.Distance(transform.position, target.position);
|
|
|
|
// Aktualizujemy tekst odleg³oœci
|
|
distanceText.text = $"Distance to target: {distance:F2} meters";
|
|
|
|
// Obracamy ikonê celu w stronê kamery (jeœli ikona jest obiektem 3D z billboardem)
|
|
if (targetIcon != null)
|
|
{
|
|
Vector3 direction = (target.position - transform.position).normalized;
|
|
targetIcon.transform.forward = direction;
|
|
}
|
|
}
|
|
}
|