Search Unity

UI Image Follow GameObject

Discussion in 'UGUI & TextMesh Pro' started by Ashmouz, Dec 29, 2016.

  1. Ashmouz

    Ashmouz

    Joined:
    May 22, 2015
    Posts:
    4
    Hey!
    Im trying to make an FPS shooter game were the player can scan the area and find different types of object (No man's sky style). And I have this script attached to the UI Image (Icon) that is a child of my canvas.

    Now the UI Image is following the GameObject perfectly. But whenever I turn away 180 degrees from the GameObject, It seems like the UI Image is located at the opposite side too (Or moves to the opposite side).

    Any help? :)
    Thanks!

    Heres the code:

    Code (CSharp):
    1. public class TrackObject : MonoBehaviour {
    2.  
    3.     public GameObject Obj;
    4.  
    5.     Camera mCamera;
    6.     private RectTransform rt;
    7.     Vector2 pos;
    8.  
    9.     void Start ()
    10.     {
    11.         mCamera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
    12.         rt = GetComponent<RectTransform> ();
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.         if (Obj)
    18.         {
    19.             pos = RectTransformUtility.WorldToScreenPoint (mCamera, Obj.transform.position);
    20.             rt.position = pos;
    21.         }
    22.         else
    23.         {
    24.             Debug.LogError (this.gameObject.name + ": No Object Attached (TrackObject)");
    25.         }
    26.          
    27.  
    28.     }
    29. }
    Screenshot 2016-12-29 07.07.38.png Screenshot 2016-12-29 07.07.48.png
     
    Last edited: Dec 29, 2016
    Magic_Nano likes this.
  2. aminhotob

    aminhotob

    Joined:
    Feb 7, 2017
    Posts:
    1
  3. MrFireBall

    MrFireBall

    Joined:
    Jan 8, 2020
    Posts:
    1
    Not working.The image still moves over the opposite side
     
    Last edited: Mar 17, 2020
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html
    You can use the Z component of the position you are getting from WorldToScreenPoint in order to determin if the object is infront of behind the camera.
    If the z value is positive is infront of the camera and if its negative you know its behind the camera.
    I personally use a canvas group to and set the alpha to 1 or 0 depending if the object is infront or behind the camera.
     
    NikL likes this.
  5. NikL

    NikL

    Joined:
    Dec 4, 2015
    Posts:
    7
    Many thanks. Your idea helped))
     
  6. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    443
    If anyone's still interested, here's a version of the code that works even if the object is behind the camera:

    Code (CSharp):
    1.         Vector3 targPos = Obj.transform.position;
    2.         Vector3 camForward = mCamera.transform.forward;
    3.         Vector3 camPos = mCamera.transform.position + camForward;
    4.         float distInFrontOfCamera = Vector3.Dot(targPos - camPos, camForward);
    5.         if (distInFrontOfCamera < 0f)
    6.         {
    7.             targPos -= camForward * distInFrontOfCamera;
    8.         }
    9.        pos = RectTransformUtility.WorldToScreenPoint (mCamera, targPos);
    10.  
    You can also clamp the resulting 'pos' to a rectangle within the screen, if you want the icon/text/whatever to hug the edge for offscreen objects.
     
  7. multvers

    multvers

    Joined:
    Aug 1, 2022
    Posts:
    1
    Thank You Peeling! You are a life saver.