Why isn't my object lerping ? C#
Hello. I am trying to rotate my object smoothly using Quaternion.Lerp()
. But for some reason it's not rotating smoothly. Instead, it is just snapping to that position. How can i fix this?
This is the code:
float rotateFloat = (((randomReward + 1) * 60) - 30) - transform.rotation.z;
Quaternion targetRotation = Quaternion.Euler(new Vector3(0, 0, rotateFloat));
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, f_difX * Time.deltaTime);
Any kind of help is very much appreciated.
EDIT: I tried to use Quaternion.RotateTowards()
instead, it did rotate smoothly. But i faced another problem which is the direction of the rotation. I did some research and found that Quaternion.RotateTowards()
Finds the shortest way to rotate to the targetRotation point, which defies the whole point of spinning the object in a both directions.
If your problem is that the rotation gets performed in an instant (and not smoothly). It's probably because f_difX * Time.deltaTime > 1. So f_difX is probably too big.
If you already checked that, then I'm afraid I can't help you.
That't not the problem. because it decreases with time (That's what gives the spinning effect). No worries tho. Thank you for the help :)
Answer by bolkay · Sep 17, 2017 at 07:30 PM
Try spherical interpolation perhaps?
I tried using Quaternion.Slerp()
as well. Sadly, it didn't work either...
Answer by msfredb7 · Sep 17, 2017 at 07:39 PM
I've tried your code and it sorta works. But I don't think it's the behaviour you want.
When you use "transform.rotation.z" in the first line, you don't actually refer to the 3rd parameter of your rotation. To do so, you need to do: transform.rotation.eulerAngles.z
If you don't insert that .eulerAngles, you'll be refering the quaterion parameters.
I just edited my comment. Its .eulerAngles, and not .euler
Hi. Thank you for the answer. I changed the code to transform.rotation.eulerAngles.z, it didn't work. it started doing some weird behavior, and by the time my object stopped spinning, it wasn't facing the targetRotation angle.
Your answer
Follow this Question
Related Questions
How to make a smooth rotation? 1 Answer
How to smoothly rotate gameobject 20 degrees on key press? 0 Answers
How to reduce the speed of rotation? 2 Answers
Flip 3D Character Rotation 180 on Y axis 1 Answer
Problems With .rotate behavior 1 Answer