Need some help getting this UI update working. I want to be able to throw multiple “glow stick” prefabs and show how far they independently are from the player using world space canvases. It currently works for the first glow stick, but if I throw any more it does not update the text.
Any help is appreciated.
vv Script to throw item and spawn UI vv
public class Glowstick : MonoBehaviour
{
public Transform cam;
public Transform throwPoint;
public GameObject glowstick;
public GameObject distanceUI;
public float followSpeed;
private float cooldown = 2f;
private KeyCode throwKey = KeyCode.Mouse0;
public float throwForce;
public float throwUpwardForce;
public float torque;
bool canThrow;
bool inWorld;
private GameObject projectile;
private GameObject distanceText;
private void Start()
{
canThrow = true;
}
private void Update()
{
//hold right click, left click to throw
if(Input.GetKeyDown(throwKey) && Input.GetMouseButton(1) && canThrow)
{
Throw();
}
if(projectile && distanceText != null)
{
distanceText.transform.position = Vector3.MoveTowards(distanceText.transform.position, projectile.transform.position, followSpeed * Time.deltaTime);
}
}
void Throw()
{
canThrow = false;
//instantiate + get rigidbody
projectile = Instantiate(glowstick, throwPoint.position, cam.rotation);
Rigidbody projectileRb = projectile.GetComponent<Rigidbody>();
distanceText = Instantiate(distanceUI, transform.position, Quaternion.identity);
//calculate direction
Vector3 forceDirection = cam.transform.forward;
RaycastHit hit;
if(Physics.Raycast(cam.position, cam.forward, out hit, 500f))
{
forceDirection = (hit.point - throwPoint.position).normalized;
}
//add force + torque for spin
Vector3 forceToAdd = forceDirection * throwForce + transform.up * throwUpwardForce;
projectileRb.AddForce(forceToAdd, ForceMode.Impulse);
projectileRb.AddTorque(transform.right * torque);
//cooldown
Invoke(nameof(ResetThrow), cooldown);
}
void ResetThrow()
{
canThrow = true;
}
}
vv Script to update the text in the UI vv
public class GlowStickDistance : MonoBehaviour
{
Transform player;
Transform glowstick;
TMP_Text distance;
private string glowstickDistance;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
player = GameObject.Find("Player").transform;
}
// Update is called once per frame
void Update()
{
//look at player
transform.rotation = Quaternion.LookRotation(transform.position - Camera.main.transform.position);
distance = GameObject.FindGameObjectWithTag("distance").GetComponentInChildren<TMP_Text>();
glowstick = GameObject.FindGameObjectWithTag("glowstick").transform;
if (distance != null)
{
//find distance from player
float dist = Vector3.Distance(player.position, glowstick.position);
Debug.Log(dist + ("glowstick distance"));
//convert float to string to display distance
glowstickDistance = dist.ToString("0m");
distance.text = glowstickDistance;
}
}
}
vv Example: (right) is updated to play position but when spawning another it does not (left) vv