- Home /
How can I lerp an objects rotation?
I want to rotate a cube smoothly with the lerp function from its current rotation to a value which would be
x: 0
y: 345 z: 0
How should I approach this? C#
private float speed, $$anonymous$$, max;
// later in update or such
float rot += speed * Time.deltaTime;
rot = $$anonymous$$athf.Lerp($$anonymous$$, max, rot);
You're leaping between two floats, not rotations. Those floats would then need to be applied to the cube's transform/rigidbody rotation. Quaternion Lerp/Slerp would be the better option, methinks.
@DajBuzi: Yeah, that won't work, because it doesn't loop; the correct function would be $$anonymous$$athf.LerpAngle();
But then you need to cache the transform.eulerAngles
to begin with.
@$$anonymous$$TheDev if you combine this with switch() or else-if statement then it will work. But in the form as i've posted, you are absolutely right - it wont loop.
Answer by RudyTheDev · Feb 19, 2014 at 08:38 PM
The simplest way is Quaternion.Lerp as answered before and you can convert your target/desired rotation euler angle rotation into quaternion with Quaternion.Euler().
Alternatively, if you want to work with euler angles directly, you can access tranform.eulerAngles and lerp with Mathf.LerpAngle() each angle individually. Note, however, that there are multiple ways in euler angles to represent certain 3D rotations (gimbal lock), so you will need to manually keep track of your current and desired target rotation, lerp that value, and assign it to live transform (otherwise the "real" euler angles can suddenly jump/change at certain overlapping angles). Something like:
public class RotateMe : MonoBehaviour
{
public Vector3 targetAngle = new Vector3(0f, 345f, 0f);
private Vector3 currentAngle;
public void Start()
{
currentAngle = transform.eulerAngles;
}
public void Update()
{
currentAngle = new Vector3(
Mathf.LerpAngle(currentAngle.x, targetAngle.x, Time.deltaTime),
Mathf.LerpAngle(currentAngle.y, targetAngle.y, Time.deltaTime),
Mathf.LerpAngle(currentAngle.z, targetAngle.z, Time.deltaTime));
transform.eulerAngles = currentAngle;
}
}
Thank you so much! This helped me a lot. This definitely works!
@$$anonymous$$TheDev
Just wanted to say THAN$$anonymous$$ YOU!! This was what worked.. Quaternion.RotateTowards and other methods were crap.. causes the rotations to keep going and not stop. Quaternion.Lerp was unstable for me .. but your alternate method worked great. As long as I didn't go past some high number like "100" ..
Saved my life! Thanks again!
Sharin' is Caring!
Answer by MadJohny · Feb 19, 2014 at 03:42 PM
As POLYGAMe said, uou will use Quaternion.Lerp: https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Lerp.html
But you have to change the code there, so:
transform.rotation = Quaternion.Lerp (transform.rotation, new Quaternion.Euler(transform.rotation.x, 345f, tramsform.rotation.z), Time.deltaTime * speed);
Hope this helps, and if it does, accept the answer
Your example doesn't work because you change the value of transform.rotation each time you execute it.
Answer by POLYGAMe · Feb 18, 2014 at 08:26 PM
@POLYGA$$anonymous$$e I already checked that, but I want to lerp with euler angles. Exactly 345 degrees in y and none in x and z
You should just be able to lerp between two float amounts then, 0-345. Use that lerped float as your local euler y in update.
Actually, I was just writing out how to do it but then remembered in C# you can't access the euler angle axes individually. What's wrong with using quaternions, anyway?
Answer by Surprisejedi · Jul 27, 2015 at 10:20 PM
If you are still working on this Rocket, I had the same issue that you had and all it was was this
private Vector3 currentAngle;
public void Start()
{
currentAngle = transform.eulerAngles;
}
public void Update()
{
currentAngle = new Vector3(currentAngle.x, currentAngle.y, 90);
transform.eulerAngles = currentAngle;
}
Hopefully this helps you out!