- Home /
What is the best way to show (Instantiate) a GUIText at the position of a GameObject?
I am building a score effect that Instantiates a GUIText at the position where the points where collected. The problem is that I dont know how to convert the GameObjects position (3d) to the GUIText position (2d). Any suggestions?
Also, is GUIText the best approach here?
Answer by Owen-Reynolds · Mar 24, 2011 at 10:40 PM
GUIText uses ViewPort coords, meaning (0.5, 0.5) is the middle of the screen, so use Camera.main.WorldToViewportPoint(transform.position)
to get the correct coords.
WorldToScreen is correct if you write code for a GUI.Box
, etc.. which use ScreenCoords (pixels.)
Also, probably don't child to anything, since it will recompute your position next frame using (wrong) worldView numbers.
I prefer using GUIText, since you can add a "live for 4 secs" script, make a prefab and spawn them as needed. Using the in-code GUI.Label version requires you to build and maintain your own List-like data structure.
Answer by Unamine · Mar 01, 2011 at 11:51 AM
check out the tutorials Burg Zerg Arcade, he teaches how to create a complete RPG and I think this feature is also scoring
Link: http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial
I have tried looking through some tutorials but can not seem to find what I am looking for. Its a pain in the ass looking through youtube videos.
Answer by efge · Mar 24, 2011 at 10:25 PM
You could use the function Camera.WorldToScreenPoint to get the position of an object in pixel coordinates (screen coordinates) and then use this values to draw your GUI.
Your answer
Follow this Question
Related Questions
scoring using guiText 2 Answers
How to make GUI Text Scale to different Resolutions? 3 Answers
Script for counting not working properly 1 Answer
Why this Error when trying to add a score using the new GUI ? 0 Answers
How to use png files as fonts 1 Answer