How do I rotate object smoothly

I understand the basic concept of rotating an object

function OnTriggerEnter(otherCollider : Collider) {
if (otherCollider.tag == "RotationTrigger") {
    transform.Rotate(0,90,0);
}

}

but how do I do it smoothly over time, say I was opening a hatch or door and did not want it to just snap open to 90 deg.

Any help appreciated

3 Answers

3

The "easiest" way to do it is to use something like iTween

Though iTween is simple, I prefer a good old fashioned Lerp. For Rotational Lerping see Quaternion.Lerp()

If you don't get Quaternions (don't worry, to be honest I'm still a little uncertain of them myself) you can do some playing around with a Euler Angle representation to make it easier.

Hope that helps.

==

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Lerp.html Here is the definition. That way you can Rotate over Time. Work great.

As I see you have four more options:

You can use it like: (untested code)

LTDescr descRotate = LeanTween.rotateX(this.gameObject, 90.0f, 0.5f);

descRotate can be consumed later for dealing with other parameters of this animation.

  • Use Update() function: (untested code)

    void Update() { transform.Rotate(20.0f * Vector3.forward * Time.deltaTime); }

  • Using Animator Component:

Have a look at this tutorial for creating animations. See section Creating Animations, under mouse:Sprite you can add rotation curves and whenever needed you can run animation.

  • Using Coroutine.

Have a look at this example

Hope it helps!