How to rotate smoothly between to rotations.

I’ve looked at a lot of questions that were very similar to this but I’m still confused on how to handle rotations in Unity.

Here’s my problem:

I have an Object - obj

and it’s transform is:

Position: 0,0,0
Rotation: 90,180,0
Scale: 0,0,0

now I want to change the transform to:

Position: 0,0,0
Rotation: 180,180,0
Scale: 0,0,0

it’s easy to run the game grab the handle and pull it around.
but how do I do this in code. These Quaternion are totally throwing me off.

I would love to see something to change it using a LERP and an instance change.

Thanks.

Addendum:
I wrote a small script to test what I want to do and so that others can better understand what I’m trying to do as well.

Here’s the script.

public class RotationLerpTest : MonoBehaviour
{

	
	public Quaternion rotation;
	public float speed = 0.1f;

	// Use this for initialization
	void Start ()
	{
		rotation = transform.rotation;
	}
	
	// Update is called once per frame
	void Update ()
	{
		transform.rotation = Quaternion.Lerp (transform.rotation, rotation, Time.deltaTime * speed);
	}
	
	void OnMouseUpAsButton ()
	{
		rotation.z += 90*Mathf.Deg2Rad;	
	}
	
}

It almost works perfectly except the rotation is some like 117 deg instead of 90. I know this has something to do with Quaternions but i can’t figure it out.

3 Answers

3

The solution I found was using Quaternion.Euler(0,0,0);

The problem is definitely my understanding of Quaternions. I don’t understand them at all. But for 2D games it doesn’t really matter, other than Quaternion.Lerp(–) finds it’s own way to rotate which is no the rotate i desire. But in general using Quaternion.Euler(–) will let you set a specific rotation at runtime.

I believe the example at:

does exactly what you want.

I've seen this example. But, it doesn't seem like I can create a transform, so I don't see how to implement this. In the example they have a transform already, I don't.

A transform is just a game object. Simply make two empty game objects (Gameobject > Empty GameObject), rotate one to the original rotation of your object, and the other to the final rotation. Insert those two values into the example that Graham Dunnett wrote, and bam!

Ok, but what if I don't know the angle prior to runtime. I should be able to programmatically calculate and set a rotation. Hopefully... maybe... please :)