- Home /
gameobject move in real = move in GUI
i just want to know, how can i Connect movement my GameObject with GUITexture movement ? (for example my object move to left, my GUITexture also move to the left ) Thanks
Answer by aldonaletto · Jul 08, 2012 at 05:16 PM
You can use Camera.WorldToViewportPoint: it returns the object position in viewport coordinates, which the GUITexture understands (GUITexture script):
var target: Transform; // drag here the object to follow
function Update(){ // place the GUITexture at the target's position: transform.position = Camera.main.WorldToViewportPoint(target.position); } NOTE: Usually you want to draw the texture at some fixed offset of the target, so that it doesn't occlude the object. For GUITextures, usually it's better to define a world offset, and adjust the screen position in the Inspector, modifying the Pixel Inset fields:
var target: Transform; // drag here the object to follow var offset: float; // world height above the target position
function Update(){ // calculate the position including the vertical offset: var pos = target.position + offset * Vector3.up; transform.position = Camera.main.WorldToViewportPoint(pos); }
owh, its awesome. Thank you very much, can i call you the master =)?
Your answer
Follow this Question
Related Questions
Why are these object passing through each other? 1 Answer
How to let a GameObject move? 2 Answers
scripting for animation 0 Answers