- Home /
Moving a GUITexture?
Basically I have a health bar (GUITexture) and I want it to slowly move in at the start of the game, moving between two points. I could of course move it by invoking a method that offsets it by a few pixels every 0.1 seconds or something but I was wondering if there was a cleaner way to move it?
Thanks in advance
Answer by AtomicMarine · Jan 12, 2011 at 08:11 PM
Try this code it might work.
Use a Update then get the GUI to move to a certain position and run a check. Just replace the x and y values with your respective values and remember to start with your starting position as your default also put ur GUITexture in an empty game object with a position of (0,0,0) and set your GUI texture's position to 0,0,0 and use the pixel inset as opposed to the transform as its more accurate
var gui : GUITexture; var pos : Vector2(x,y); var size : Vector2(x,y);
function Update{
//moving it along the x axis if (pos.x != 40){
//the 0.1 wil make it move slowly, increase this value to make it move in faster gui.pixelInset = Rect(pos.x + 0.1,pos.y,size.x,size.y);
}else{
gui.pixelInset = Rect(pos.x,pos.y,size.x,size.y); }
}
Answer by · Sep 02, 2010 at 12:52 PM
Take a look at Vector3.Lerp. I haven't used GUITextures before, but it looks you can move them by setting transform.position.
There's an example given, but you'd probably want to adapt it by invoking a coroutine on Start(), rather than having it constantly updated past the first second.