- Home /
Animate GUI Elements
I've got a GUITexture on screen using this code:
GUI.DrawTexture (new Rect(Screen.width*0f/5f,Screen.height*0.4f/2f, Screen.width* 4f/5f, Screen.height*4f/5f), myObject);
On a button press, I want the texture to animate to the right and go off screen. Basically to go from position 'Screen.width*0f/5f' to 'Screen.width*5f/5f' over 1 sec, for example.
I've seen some plugins that do this but I was wondering if there was a way to just code it; I know the logic just not the right wording for it.
Any Clues?
There's 2 official unity tutorials on this...
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-transitions
http://blogs.unity3d.com/2014/06/30/unity-4-6-new-ui-world-space-canvas/
Also, this is absurdly old UI system code. You might find it much more compelling to use the new UI. Far less work, and better results.
This something like this:
float x = 0;
void Update() {
if(buttonWasPressed && x < 5)
x = $$anonymous$$athf.Lerp(0, 5, Time.deltaTile);
}
void OnGUI() {
GUI.DrawTexture (new Rect(Screen.width * x/5f,Screen.height*0.4f/2f, Screen.width* 4f/5f, Screen.height*4f/5f), myObject);
}
Thanks for your answer, I've had a fiddle with the new UI but still having problems with it so better the devil you know! For the time being at least. I've implemented your script like so:
GUI.DrawTexture (new Rect(Screen.width*x/5f,Screen.height*0.4f/2f, Screen.width* 4f/5f, Screen.height*4f/5f), myObject);
if(GUI.Button(new Rect(Screen.width*x/5f,Screen.height*0.4f/2f, Screen.width* 4f/5f, Screen.height*4f/5f), "Button"))
buttonWasPressed = true;
if(buttonWasPressed && x < 5f)
x = $$anonymous$$athf.Lerp(0f, 5f, Time.deltaTime);
In play mode it move slightly to the right but not all the way, then it jitters. Any idea what could be causing this?
It probably jitters because its' position is made on update ins$$anonymous$$d of onGUI. Another reason is that it never actually stops lerping.
Update is tied to the games' refresh rate, while onGUI is tied to GUI's refresh rates, normally you can't tell the difference, but it is there. Just like with update and fixedupdate.
http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html
I'd still want to urge you to try and get more used to the new UI, even if you might have to hit your toes to doorframes with it a couple of times to get over the learning curve. $$anonymous$$ostly because i just created this effect using the animator within 5 $$anonymous$$utes, of which 4 $$anonymous$$utes was booting up Unity and making the button ;)
Answer by hollym16 · Mar 19, 2015 at 03:44 PM
I know with the new UI system this is becoming obsolete but for anyone else struggling with Math.Lerp, I found a great link as well as the stuff above:
http://answers.unity3d.com/questions/237294/how-the-heck-does-mathflerp-work.html
Your answer
Follow this Question
Related Questions
Unity 5 GUI system 1 Answer
How do I use my .png file as a button? 1 Answer
guiTexture color half alpha White? 1 Answer
GUI.DrawTexture on GUI.Button press 1 Answer