Distance To Checkpoint
using UnityEngine;
using UnityEngine.UI;
public class DistanceToCheckpoint : MonoBehaviour {
//all unity scripts prsent this script
// Reference to checkpoint position
[SerializeField]
private Transform checkpoint;
// Reference to UI text that shows the distance value
[SerializeField]
private Text distanceText;
// please share this script
// Calculated distance value
private float distance;
// Update is called once per frame
private void Update()
{
// Calculate distance value between character and checkpoint
distance = (checkpoint.transform.position - transform.position).magnitude;
// Display distance value via UI text
// distance.ToString("F1") shows value with 1 digit after period
// so 12.234 will be shown as 12.2 for example
// distance.ToString("F2") will show 12.23 in this case
distanceText.text = "Distance: " + distance.ToString("F1") + " meters";
}
}
No comments:
Post a Comment