Search Unity

Keeping text on even y axis based on distance

Discussion in 'Visual Scripting' started by jessee03, Sep 17, 2022.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I have 2d gui text that I place over a 3d object in the world. Issue I'm having is keeping the text in the same location above the 3d object based on distance. Not sure what math I can use to keep the text in the same Y position no matter what distance away the object is? offsetY should calculate the distance


    Code (CSharp):
    1.         targetPosition = Camera.main.WorldToScreenPoint(new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z));
    2.         float distance = Vector3.Distance(Camera.main.transform.position, this.transform.position);
    3.         float offsetY = 150.0f;
    4.  
    5.         this.transform.position = new Vector3(targetPosition.x, targetPosition.y + offsetY, targetPosition.z);