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. Dismiss Notice

UI Text on an object

Discussion in 'UGUI & TextMesh Pro' started by samsmithnz, Aug 28, 2014.

  1. samsmithnz

    samsmithnz

    Joined:
    Oct 21, 2012
    Posts:
    13
    Using 4.6, Beta18: I'm adding the new UI text to an object, and I'm expecting the text to face the camera at all times, and it's just not.

    To reproduce:
    1. Create a new scene. Add a Directional Light, and a cube at (0,0,0).
    2. Add the following script (I call it OrbitAroundObject) to the "Main Camera" object.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class OrbitAroundObject : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     public float OrbitDegrees = 1f;
    9.  
    10.     void Update()
    11.     {
    12.         if (target != null)
    13.         {
    14.             transform.RotateAround(target.position, Vector3.up, OrbitDegrees);
    15.         }
    16.     }
    17. }
    18.  
    3. Drag the cube object onto the Target property of the OrbitAroundObject script.
    4. Create a UI>Text object. Set the canvas ender Mode to "World Space", and drag the "Main Camera" into the event camera property. Also change the Canvas position to (0,0,0). On the Text object, set the position to (0,0,0), and I always have to change the scale to (0.2,0.2,0.2), otherwise I can't see the text.
    5. Drag the Canvas object to be a child of the cube.

    Expected: As my camera rotates around the cube, the text is always in front, but attached to the cube.

    What happens: The text is static and the camera rotates around the cube AND the text. Is this expected? How can I make this work?
     
    Carlo-DiGi likes this.
  2. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    Having the text as a world object is exactly the point of world space mode on the canvas. It's for text bubbles and billboards and computer screens and whatever else. I don't know how to do what you're wanting to do, but what you *are* doing seems to be doing what it should be doing.
     
  3. samsmithnz

    samsmithnz

    Joined:
    Oct 21, 2012
    Posts:
    13
    Yes, but should my camera be rotating behind the back of my speech bubble/billboard? shouldn't it be keeping it aligned with my camera so that it's visible? It's like the Health on top of a player in Diablo or X-COM. it's always facing the camera/player.
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    No, real-world billboards don't follow you. At least not yet ;)

    The health bar example you're looking for requires more of a hybrid solution, where GUI elements are moved according to two of the three game world vectors, but in their own 2D plane. World space mode makes the controls game world objects.
     
  5. samsmithnz

    samsmithnz

    Joined:
    Oct 21, 2012
    Posts:
    13
    Got it. for some reason I thought that was the point of the world canvas render mode. You don't happen to have a link to what you describe do you?
     
  6. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    I don't have a link, but if you look at the scripting API there are methods to translate world space coordinates from screen space (and their names are somewhat logical). It's a common enough trick that I know there are several threads on it. The trick is probably to figure out the magic search phrase ;)
     
  7. samsmithnz

    samsmithnz

    Joined:
    Oct 21, 2012
    Posts:
    13
    Just tried some code on the Text object (using the scenario as above) to adjust the rotation based on the camera rotation. It works really well and now my text object(s) stay aligned with my camera. This may not work for everyone, but it seems to be a good start. Thanks for the hints Orb.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LookAtCamera : MonoBehaviour
    6. {
    7.  
    8.     public Camera SourceCamera;
    9.  
    10.     void Update()
    11.     {
    12.         if (SourceCamera != null)
    13.         {
    14.             transform.rotation = SourceCamera.transform.rotation;
    15.         }
    16.     }
    17. }
    18.  
     
    Carlo-DiGi likes this.
  8. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    I think you figured out the neatest possible way to do it :)
     
  9. samsmithnz

    samsmithnz

    Joined:
    Oct 21, 2012
    Posts:
    13
    I'm happy with it. Props to my friend Carlo Digi who also helped lead me to the answer.
     
    Carlo-DiGi likes this.
  10. Blackpablok

    Blackpablok

    Joined:
    May 6, 2014
    Posts:
    14
    Thank you it help me too.
     
    samsmithnz likes this.
  11. eweeparker

    eweeparker

    Joined:
    Dec 6, 2012
    Posts:
    5
    transform.Rotate(Vector3.up - Vector3(0,180,0)); //fix backward text
     
  12. noobygamedev

    noobygamedev

    Joined:
    Feb 16, 2017
    Posts:
    1
    hay i have a question if you will answer it please and thank you. i just started i and i would like to know how i could give a message to the player to unlock a skill and tell them what the skill dose when unlocked by hovering the mouse over the button. i understand if you don't know i just need some help with my skill select menu system. gameprojet.PNG
     
  13. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,075
    You can do it either within the Update Method, with a derivation of the event system or with an event trigger component to name a few ways which just came into my mind.
    But your question does not belong to this thread. Please create a new thread for your question to not confuse this thread.