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.

2 Answers

2

Try spherical interpolation perhaps?

I tried using Quaternion.Slerp()as well. Sadly, it didn't work either...

No diff. it was working.. then just stopped. I didn't touch the installed app at all. Yes maybe the canvas is no longer visible (vs not enabling). I will rebuild with some text fields to see if there are any errors ... stay tuned :)

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

Not sure what you mean by "text fields". I would add logging. And maybe not assume too much on the basis of a sound being played (the sound could be coming from somewhere else, IMO the only way to be 100% sure that a function is being called is to put a unique logging line in it. If it's still broken then perhaps sharing the code would help.