- Home /
UI Text in world space facing camera
I need to create simple UI tips for my objects and I wan't to make them behave like this texts here: https://youtu.be/EZHeYPxsNds?t=14m48s
I tried with text in world space and make it rotate to camera but it is visible that text is rotating to camera, it not like like part of UI, also I want to text stay always on the same size, not dependent of camera position. How can I make something like this like on the video?
This behaviour can be also described as behaviour of camera gizmo icon in editor etc. but I want it without scaling when camera is nearer like "3D icons" turned off (in "gizmo" tab).
I don't think you should make the UI in world space.
Just store the position in world space and transform that position into screen space, and then show the texts in screen space. This way, the texts will not change sizes, yet it will always look like it's facing the camera.
Answer by Le-Pampelmuse · Nov 12, 2015 at 04:38 AM
You could use a Canvas that is set to Overlay and in a script use Camera.WorldToViewportPoint
to check if the object position is on screen at any time.
If it is (if both x and y are between 0 and 1), then you can set the Text position to the Vector your check returned.
Since the Vector is Screen Space and not World Space, you position the Text at the exact position the object is rendered on screen.
Of course this would result in overlapping texts if there are multiple objects close to each other in screen space, depending on your text size and similar factors. You could do simple Vector3.Distance
checks, or Physics.RayCast
to deal with that if it occurs.
Hope this does what you want.
Documentation: http://docs.unity3d.com/ScriptReference/Camera.WorldToViewportPoint.html