-1

In my world I have a GameObject, lets say a race car.

Above the race car I want a white box with a text label that says the driver's name (let's say "Fred"). This label will follow the race car as it moves.

How can I create this "white box with a text label" in Unity? Will it be a gameobject (if so, how do I construct it).

Since the only "Text" object I find in Unity editor was a UI element. And the UI canvas is unrelated to the coordinates in my game world, so I dont know if this is appropriate use case for this type of object.

car with a name tag

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • `And the UI canvas is unrelated to the coordinates in my game world` .. tried using a `World Space` canvas? ;) In general though rather go for `TextMeshPro` (the non uGUI version) .. the UI System comes with quite some overhead – derHugo Jan 18 '22 at 14:34
  • I did: Have you tried to simply set your Canvas to `World Space` instead of the default `Screenspace Overlay` => tadaaa Canvas is now placed in 3D world space e.g. as a child of your car ... I downvoted this because it is not a specific coding related question but asking for a solution/tool which is off-topic for StackOverflow – derHugo Jan 19 '22 at 08:53

1 Answers1

2

I think you have 2 options. It is fundamental to read to the different canvas modes in the documentation

1.- Overlay mode. You can figure out the screen position of your canvas at runtime with
Camera.WorldToScreenPoint. This canvas mode places UI elements on the screen rendered on top of the scene, so you would kind of "fake" the canvas is following your gameObject in the world calculatig and updating its position.

2.- World Space, make cavas children of your car and make it always face the camera with a billboard component updating the canvas rotation.

public class Billboard : MonoBehaviour {
    public Transform cam;
    private void Start() {
        cam = Camera.main.transform;
    }
    void LateUpdate() {
        transform.LookAt(transform.position + cam.forward);
    }
}

Add the component to the canvas gameObject or do myCanvas.AddComponent<Billboard>(); where myCanvas is the Canvas component in your canvas holder gameObject. With this option the canvas is actually in the world as the mode name indicates, thats why you would need to update its rotation to make it always look to the camera.

In option 1 you need to update the position and not the rotation because the canvas is in the screen and with option 2 you need to update the rotation and not the position because the canvas is in the world.

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • option 2 seems better, since the car moves in 2d (not 3d) so rotation will not be needed. Do I understand correctly, that I can make a "mini canvas", as a child to the race car? – vikingsteve Jan 18 '22 at 21:13
  • yes, mini cavas as child to the race car. Yo need to rotate the camera, because the camera does not rotate but the car does, and the child mini canvas will rotate along with the car. As far as I see in the picture the car is moves in 3d – rustyBucketBay Jan 18 '22 at 21:43