- Home /
Why is my object not rotating smoothly ? 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);
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.
P.S. i tried to use Quaternion.Slerp()
and i got the same result as the Quaternion.Lerp()
Any help is very much appreciated.
Edit:
I'm trying to create a spin wheel. the idea of that spin wheel is to follow the direction of the player's swipe. f_difX is basically the point of where the player lefts his finger off the screen - the point where he touched the screen before swiping. And then i decrease f_difX with -1 every frame until it reaches zero (This creates the spinning animation). The code above is a function that is called when f_difX < 100 (which is the last round) to lerp towards the chosen randomReward. The random reward is a gameObject that is selected randomly from a GameObject list. The first line of the code above is to calculate the amount of float the wheel should rotate to reach the center position (0 on X axis). For now, the code works properly and it actually rotates correctly to face the right angle. But my proplem is that it snaps to the angle and not rotate smoothly.
Also as i said above, i tried it using Quaternion.RotateTowards()
which worked nicely and the wheel rotated smoothly. But i faced the problem of it following the closest arc to reach the targetRotation, which defies the whole idea of the wheel spinning according to the player's swipe. I tried to look online to find a way to reverse the Quaternion.RotateTowards()
to follow the other direction, but i had no luck.
What is the value of f_difX?
A lerp snapping to its final value sounds like the t value is greater than 1.
Also, what is randomReward?
It looks like you're changing the goal of the Lerp every frame, so that could be causing issues.
I'm trying to create a spin wheel. the idea of that spin wheel is to follow the direction of the player's swipe. f_difX is basically the point of where the player lefts his finger off the screen - the point where he touched the screen before swiping. And then i decrease f_difX with -1 every frame until it reaches zero (This creates the spinning animation). The code above is a function that is called when f_difX < 100 (which is the last round) to lerp towards the chosen randomReward. The random reward is a gameObject that is selected randomly from a GameObject list. The first line of the code above is to calculate the amount of float the wheel should rotate to reach the center position (0 on X axis). For now, the code works properly and it actually rotates correctly to face the right angle. But my proplem is that it snaps to the angle and not rotate smoothly.
Also as i said above, i tried it using Quaternion.RotateTowards()
which worked nicely and the wheel rotated smoothly. But i faced the problem of it following the closest arc to reach the targetRotation, which defies the whole idea of the wheel spinning according to the player's swipe. I tried to look online to find a way to reverse the Quaternion.RotateTowards()
to follow the other direction, but i had no luck.
Answer by Animatick · Sep 18, 2017 at 09:00 AM
I'm not sure how you had set up your Quaternion.Slerp, but for me, I was working on a project that used an external device to rotate the camera in the engine. the way that I got it to not jar around or snap is this
Quaternion toRotation = Quaternion.Euler(x, y, z);
transform.rotation = Quaternion.Slerp(transform.rotation, toRotation, 0.3f);
Like I said I'm not sure if this is the way you tried, but this is what worked for me
I hope this helps.
Yeah that's exactly how i tried it. But no luck! Thanks for the reply :)
Your answer
Follow this Question
Related Questions
Move position with smoothing 0 Answers
Flip over an object (smooth transition) 3 Answers
Is it possible to use a quaternion from a gameobjects position to another gameobjects position? 1 Answer
How to ROTATE an object without slowing the ends (lerp) 3 Answers
How would I smooth out a Quaternion? 2 Answers