Certainly! Below is a C# script for a Unity detector that meets your specifications. I have included using the TextMeshPro library to handle the text display, so make sure you have TextMeshPro package installed in your Unity project.
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Detector : MonoBehaviour
{
public TMP_Text worldCoordinatesText;
public TMP_Text distanceText;
public TMP_Text pointsListText;
public TMP_Text timeOfDayText;
public List<Transform> destinationPoints;
private bool damaged = false;
private float elapsedDamageTime = 0f;
private float nextRandomPointTime = 0f;
private Transform currentRandomPoint = null;
void Start()
{
UpdatePointsListText();
}
void Update()
{
UpdateWorldCoordinates();
UpdateTimeOfDay();
if (damaged)
{
elapsedDamageTime += Time.deltaTime;
if (elapsedDamageTime >= nextRandomPointTime)
{
SetNewRandomPoint();
elapsedDamageTime = 0f;
nextRandomPointTime = Random.Range(180, 600);
}
UpdateDistanceText(currentRandomPoint);
}
else
{
Transform closestPoint = GetClosestPoint();
UpdateDistanceText(closestPoint);
}
}
private void UpdateWorldCoordinates()
{
Vector3 position = transform.position;
worldCoordinatesText.text = $"Coordinates: ({position.x:F2}, {position.y:F2}, {position.z:F2})";
}
private void UpdateDistanceText(Transform point)
{
if (point == null) return;
float distance = Vector3.Distance(transform.position, point.position);
distanceText.text = $"Distance to point: {distance:F2} units";
}
private Transform GetClosestPoint()
{
Transform closestPoint = null;
float closestDistance = float.MaxValue;
foreach (var point in destinationPoints)
{
float distance = Vector3.Distance(transform.position, point.position);
if (distance < closestDistance)
{
closestDistance = distance;
closestPoint = point;
}
}
return closestPoint;
}
private void SetNewRandomPoint()
{
if (destinationPoints.Count == 0) return;
currentRandomPoint = destinationPoints[Random.Range(0, destinationPoints.Count)];
}
private void UpdatePointsListText()
{
pointsListText.text = "Available Points:\n";
for (int i = 0; i < destinationPoints.Count; i++)
{
pointsListText.text += $"{i + 1}. Point {i + 1}\n";
}
}
private void UpdateTimeOfDay()
{
timeOfDayText.text = $"Time of Day: {System.DateTime.Now:HH:mm:ss}";
}
// Method to set the detector's damaged state
public void SetDamaged(bool isDamaged)
{
damaged = isDamaged;
if (damaged)
{
SetNewRandomPoint();
nextRandomPointTime = Random.Range(180, 600);
elapsedDamageTime = 0f;
}
}
}
Explanation:
- TextMeshPro Text Variables: The script uses TextMeshPro (
TMP_Text) components to display texts.
- Destination Points List: Holds
Transform objects for destination points.
- World Coordinates Update: Updates and displays the current world coordinates.
- Distance Calculation: Calculates and shows the distance to the closest destination point or a random point if damaged.
- Set New Random Point: Chooses a new random destination point at random intervals between 3-10 minutes when damaged.
- Points List Update: Displays the list of available points.
- Time of Day Update: Displays the current time of day.
- Damaged State: When the detector is damaged, the distance is calculated to a random point that changes periodically.
Attach this script to a GameObject in your Unity scene and assign the TMP_Text fields and destinationPoints list in the Inspector. This will ensure your detector is functioning as required.