- Home /
I am having trouble with quaternion rotate towards
I have an object that rotates at different times and different rotations and I can't figure out what to add for time to keep it from shuttering using rotate towards. How do I get/measure the time between each new rotation?
If you call the rotateTowards withing Update(), you can use Time.DeltaTime to get the elapsed time in seconds from the last Update() call. Just multiply it within a lerp call
Could you give me an example of a lerp call or maybe a link?
Answer by Klarzahs · Oct 24, 2020 at 08:09 AM
Here is a code sample if you still need it:
//calculate target vector
Vector3 vec = (targetGO.transform.position - transform.position).normalized;
//interpolate towards goal
transform.forward = Vector3.Lerp(transform.forward, vec, Time.deltaTime * speed);
where speed defines your rotation speed.
Lerp(a, b, t) is a linear interpolation from a to b with parameter t. If t = 0,
then it returns a, if t = 1
it returns b. If it is between 0<t<1
, then it is linearly in between the two :)
You should normalize the targetvector, otherwise it will rotate faster is the goal point is far away.
Standard untested disclaimer
Answer by roszek2 · Oct 25, 2020 at 03:55 PM
Where you have speed I don't have a constant speed with different forces being applied to the object. That's what I need to figure out I think.
There is a main object that moves and rotates based on forces applied to it's RigidBody and a clone of that object that needs to equal that movement. The object clone receives a position and rotation from the main object only. How would I get the maxDegreesDelta that Quaternion.RotateTowards and Vector3.MoveTowards calls for, which would vary in my case?
Do you your second object to instantly copy the position/rotation of your main object? You could simply use
void Update(){
secondObject.transform.position = firstObject.transform.position;
secondObject.transform.rotation = firstObject.transform.rotation;
}
If you want to rotate it slowly, you can rate limit it by checking the angle between vectors and apply a $$anonymous$$(angle, maxDegreesDelta) function call. Position is similar, just check the distance and have a maximum
Your answer
![](https://koobas.hobune.stream/wayback/20220613014413im_/https://answers.unity.com/themes/thub/images/avi.jpg)