Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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:
    700
    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);