I’m making a 3D racing game and I attached text to my Ai Player that shows their speed,
This is my code for attaching this text to my Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClampSpeed : MonoBehaviour {
public Text label;
public Rigidbody carRigidbody;
// Update is called once per frame
void Update ()
{
Vector3 labelPos = Camera.main.WorldToScreenPoint(transform.position);
label.transform.position = labelPos;
label.text = carRigidbody.velocity.magnitude.ToString("F0") + " U/S";
if(carRigidbody.velocity.magnitude > 90)
{
label.color = Color.red;
}
else
{
label.color = Color.white;
}
}
}
When the Ai Player is in front of me I can see the text just fine:
But when the Ai Player is behind me the text still appear on screen, which I don’t want
So how do I hide this text if it is behind my main camera? (The main camera is attached to my Player)
,I’m making a 3D racing game and I want the Ai Players to have Text showing their speed,
This is my code for attaching a UI text element to the Ai Player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClampSpeed : MonoBehaviour {
public Text label;
public Rigidbody carRigidbody;
// Update is called once per frame
void Update ()
{
Vector3 labelPos = Camera.main.WorldToScreenPoint(transform.position);
label.transform.position = labelPos;
label.text = carRigidbody.velocity.magnitude.ToString("F0") + " U/S";
if(carRigidbody.velocity.magnitude > 90)
{
label.color = Color.red;
}
else
{
label.color = Color.white;
}
}
}
When I look at the Ai Player their tag shows just fine:
But when the Ai Player is behind me…
So how do I hide this Text when the Ai player is behind my Camera? (which is attached to my Player)