- Home /
GUIText not following player
I have a 2d Player consisting of a Transform, Quad (Mesh Filter), Mesh Renderer, Rigidbody2D, BoxCollider2D and a script for player movement.
For the purpose of debugging, without having to use breakpoints, I'm trying to print info about the player on screen. I'm using a GUIText
object, passing it to the script attached to the player and updating its text property each frame.
The issue is that the GUIText
doesn't properly follow the player. It moves with the player but at a faster rate. When moving the player left and right the text does also but at a much faster rate, quickly disappearing off screen.
I have tried making the GUIText
object a child of the player and have also tried setting its position to the players rigidbody
position and tried setting it to the players transform position. All yield the same results of the text moving magnitudes higher relative to the players movement.
Any help would be greatly appreciated.
Answer by NickP_2 · Jun 01, 2014 at 03:13 PM
GUITexts do have a transform, but you don't have to use it like that.
UPDATE; DONT READ Read this: http://docs.unity3d.com/Manual/class-GuiTexture.html
When the position of the GUIText is x: 1, and y:1 it means it's at the upper right corner of your screen, x & y : 0.5f = center screen, and x & y: 0 = left lower corner of the screen.
To move it with the player, you would have to convert the players world position to a viewport position in code. So try this code, and attach the script to your main camera
//drag the player's transform here in the inspector
public transform player;
//drag the guiText here in the inspector
public GUIText debugGUIText;
void Update()
{
debugGUIText.transform.position = camera.WorldToViewPortPoint(player.position);
}
Didn't check for errors, hope it helps!
Your answer
Follow this Question
Related Questions
Creating a castle-crasher-like Game [Need Help] 3 Answers
Moving 2D Sprite Object in Curve path. 4 Answers
How can I keep a global score updatable from multiple GameObjects 1 Answer
Rotate while moving forward 1 Answer
Move Object Using iTween 2 Answers