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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Easy way to add a floating icon on a game object?

Discussion in 'Editor & General Support' started by rapidrunner, Apr 14, 2016.

  1. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    I have a game object (imagine a totem), and would like to have a floating image, that show the state of the totem (like sleeping, charging, releasing and so on).

    I did try to solve this, creating a quad, parenting it to the game object, and then add as texture, an icon image (imported as texture with alpha). The result though is that the image is visible; using the unlit transparent shader, but when I rotate the camera, it disappear.

    This is expected, since a quad has the normal and if you don't point to it, you won't see it. How do you make the quad to rotate, so it is always facing the camera?

    Capture.JPG Capture2.JPG
     
  2. Lexeon

    Lexeon

    Joined:
    Apr 22, 2016
    Posts:
    23
    Code (CSharp):
    1.  
    2. public Transform cameraTransform
    3.  
    4. void Update(){
    5.      transform.LookAt(cameraTransform);
    6. }
    7.  
    Posting this for future reference. Just throw that into some script on the object you want facing the camera, and drop the camera into the cameraTransform variable in the inspector.
     
    karl_jones likes this.
  3. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Also you could make a screen overlay:

    Code (CSharp):
    1. void ShowIndicator()
    2.     {
    3.         // Final position of marker above GO in world space
    4.         Vector3 offsetPos = new Vector3(hit.transform.position.x, hit.transform.position.y, hit.transform.position.z);
    5.         // Calculate *screen* position (note, not a canvas/recttransform position)
    6.         Vector2 canvasPos;
    7.         Vector2 screenPoint = MyCam.WorldToScreenPoint(offsetPos);
    8.         // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
    9.         RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);
    10.         IndicatorImage.SetActive(true);
    11.         IndicatorImage.transform.localPosition = canvasPos + IndicatorImageOffset;
    12.         IndicatorImage.GetComponent<IndicatorImageController>().CurrentObjectSelected = hit.transform;
    13.     }
    Since with transform.LookAt you won't see the icon if it's blocked by something. But that might be something you want.
     
    Ayush4441 likes this.