- Home /
Add Easing to Transform.Rotate
Hi,
I'm using a C# script to rotate an object based off a two finger rotate gesture. The following script works great, but I'd like to add a little easing to the rotation. I've tried implementing some easing equations, but can't get things to work.
I thought about trying to use Quaternion.Slerp, but I need angleThen and angleNow to be of type Quaternion and I'm not sure how to convert these values using Quaternion constructor.
transform.rotation = Quaternion.Slerp (angleThen, angleNow, Time.time * 0.4);
I'd greatly appreciate any help or advice!
Thanks,
Wes
float zDistanceFromCamera = Vector3.Distance(Camera.main.transform.position,transform.position); Vector3 screenPosition0 = new Vector3(touch0.position.x,touch0.position.y,zDistanceFromCamera); Vector3 lastScreenPosition0 = new Vector3(touch0.position.x - touch0.deltaPosition.x,touch0.position.y - touch0.deltaPosition.y,zDistanceFromCamera);
Vector3 screenPosition1 = new Vector3(touch1.position.x,touch1.position.y,zDistanceFromCamera);
Vector3 lastScreenPosition1 = new Vector3(touch1.position.x - touch1.deltaPosition.x,touch1.position.y - touch1.deltaPosition.y,zDistanceFromCamera);
float angleNow = Mathf.Atan2(screenPosition0.x - screenPosition1.x, screenPosition0.y - screenPosition1.y) * Mathf.Rad2Deg;
float angleThen = Mathf.Atan2(lastScreenPosition0.x - lastScreenPosition1.x, lastScreenPosition0.y - lastScreenPosition1.y) * Mathf.Rad2Deg;
float angleDelta = (angleNow - angleThen);
transform.Rotate(Vector3.up, angleDelta, Space.Self);
Answer by Mendicant · Nov 30, 2010 at 08:44 AM
You could try something like:
var str : float = RotationSpeed * Time.deltaTime;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(0, Vector3.up), str);
http://unity3d.com/support/documentation/ScriptReference/Quaternion.AngleAxis.html
Thanks very much for the help! Unfortunately, I couldn't get this solution to work. It seems that the rotation keeps wanting to jump back to it's original value. I'll keep messing with it. Quaternion.AngleAxis is a good use and will come in handy.
$$anonymous$$endicant is right, quanteraion lerp is what you need. Check the script ref but in the arguments you need (original angle, target angle, time) if I'm not mistaken.
Your answer
Follow this Question
Related Questions
Rotation Ease-In-Out with MAX Rotation Speed 1 Answer
What is the best way to ease a rotation? (js) 1 Answer
easing camera rotation after mouse up? 0 Answers
Flip over an object (smooth transition) 3 Answers
Rotation speed increase gradually 0 Answers