Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate rotation in Unity3d

I'm working on a 2D game in Unity3D (using Orthello 2D).

Since I switched from Cocos2d and CoronaSDK, I'm wondering if there's a way of implementing the following behaviour for a sprite (or any Unity3D object) as it worked in Corona:

object = ...
transition.to ( object, { time = 1000, rotation = object.rotation + 100, onComplete = function () 
    // do something
end })

So a sprite rotates by 100 degrees over 1 second.

In my script attached to a sprite I can have a rotation in my Update () function, but it's a bit different approach...

like image 781
kender Avatar asked Dec 04 '25 10:12

kender


1 Answers

You can do it easily in an Update function.

float timer = 0f;

void Update()
{
    if(timer <= 1)
    {
// Time.deltaTime*100 will make sure we are moving at a constant speed of 100 per second
        transform.Rotate(0f,0f,Time.deltaTime*100);
// Increment the timer so we know when to stop
        timer += Time.deltaTime;
    }
}

If you need to do another 100 degrees rotation you will just have to reset the timer.

You can see different version of the Rotate function here and more information about the lifesaver Time.deltaTime value here

like image 154
Sergio Bonfiglio Avatar answered Dec 07 '25 09:12

Sergio Bonfiglio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!