- Home /
Smoothly Rotate 2D sprite 90 degrees on click
Hey Unitarians,
I have a simple 2D square in my scene, and I want it so that whenever the user clicks the screen, the square rotates itself 90 degrees to the left. I want it to smoothly rotate over a span of half a second.
So far, I can make the square rotate 90 degrees to the left instantaneously by using transform.Rotate (Vector3.forward * -90f);
, but the problem is that it does that instantly, and not smoothly over a period of half a second
I tried using Quaternion.Lerp, but it doesn't work. It turns one degree and then stops and won't turn again when I click it. Also, before I clicked for the first time, there were error messeges that complained of the transform.rotation assign attempt being not valid. I have never used Quaternions nor Lerps before, so I don't know how it works at all.
I don't want to use the above methods. I have no idea how to make it work. If anyone could help me, it would be a godsend.
Thanks in advance, Aman Jha
I knocked up a wiggle script the other day, that wiggled objects smoothly,. This used the Lerp function, if you use this as a base you should be able to get what your after, ins$$anonymous$$d of transfrom you could apply to rotation, and ins$$anonymous$$d of random you would use set values ins$$anonymous$$d. http://answers.unity3d.com/questions/806893/how-can-i-script-a-wiggle.html
Smooth rotation over time has been posted to Unity Answers many times. If you are just looking for a solution, take a look at the $$anonymous$$oveObject script in the Unity Wiki (it does rotation), and iTween (free from the Asset store).
@robertbu Does iTween support 2D objects? Also is it a significant file size? (I'm developing for mobile).
$$anonymous$$aybe also check LeanTween. It's faster and I find it easier to use. And it's free too.
In general, you can thing of Unity's 2D and 3D with an orthographic camera, so yes iTween (or LeanTween) will work fine. And you should not see any performance issues from using them. But your problem is only the one you ask about, search Unity Answers for the many answers dealing smooth rotation.
Answer by liyokikon · Oct 13, 2014 at 06:27 PM
Why don't you try using something like iTween?
Already got that answer from robertbu in the comments, but I can't accept that so I'll accept yours lol.