- Home /
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
Answer by equalsequals · Jul 21, 2010 at 04:16 PM
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.
Answer by NeverHopeless · Jul 06, 2015 at 04:36 AM
As I see you have four more options:
LeanTween plugin.
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!