SmoothDampAngle problem when the target angle is 90 - 270 degree
Hi guys, I want to use Mathf.SmoothDampAngle to rotate an object which have to follow the given angle. I works well when the given degree is from 0 to 90 and from 180 to 360, but when it turns from 91 to 179 the target object's rotation is totally messes out.
this is my code:
void Update () {
var damp = Mathf.SmoothDampAngle(transform.eulerAngles.x, /*Normalise(*/gc.getTravelledDeg()/*, 90, -90)*/ + 10.0f, ref smoothVelocity, 0.5f);
transform.rotation = Quaternion.Euler(damp, 0, 0);
}
//Normalizes any number to an arbitrary range
//by assuming the range wraps around when going below min or above max
float Normalise( float value, float start, float end )
{
float width = end - start; //
float offsetValue = value - start; // value relative to 0
return ( offsetValue - ( Mathf.Floor(offsetValue / width ) * width ) ) + start ;
// + start to reset back to start of original range
}
I have tried to normalise the angle. I only got a partial result when I used the normalisation from 90 to -90, but it is not what I want. I really don't know what is the problem with my code. Please, help me...
I have tried now to use the simple ascending angle with the same result:
private float angle = 0;
void Update () {
var damp = $$anonymous$$athf.SmoothDampAngle(transform.eulerAngles.x, angle++ + 10.0f, ref smoothVelocity, 0.5f);
transform.rotation = Quaternion.Euler(damp, 0, 0);
}
Answer by Bunny83 · Apr 25, 2018 at 12:24 AM
Your problem is that you do a read-modify-write operation on eulerangles which doesn't work as the rotation is internally stored as Quaterion. When you read the current eulerAngles Unity has to convert the Quaterion to euler angles. This conversion is not unique as the same rotation can be represented with different euler angles combinations.
You may want to use your own float variable to store the current rotation without reading it back from eulerAngles.
As you might know euler angles suffer from gimbal lock at x==90° or 270° while quaterions do not have this problem. Gimbal lock generally makes any kind of interpolation between two rotations difficult. That's why we actually use Quaternions.
Thanks for the quick answer :) Can you give me an example please? This my improved script:
target.transform.rotation = Quaternion.LookRotation(player.transform.position);
var damp = $$anonymous$$athf.SmoothDampAngle(transform.eulerAngles.x, target.eulerAngles.x, ref smoothVelocity, 0.5f);
Debug.Log(target.eulerAngles.x);
Vector3 position = target.position;
position += Quaternion.Euler(damp, 0, 0) * new Vector3(0, 0, -10);
transform.position = position;
transform.LookAt(target);
It is better now, but when the target reaches the gimbal locks it turns backwards...
Answer by hopetheorc · Apr 25, 2018 at 01:39 PM
Figured it out:
void FixedUpdate () {
Quaternion desiredRotation = Quaternion.Euler(gc.getTravelledDeg() + 45, 0, 0);
Quaternion smoothedRotation = Quaternion.Slerp(transform.rotation, desiredRotation, smoothVelocity);
transform.rotation = smoothedRotation;
}
It works! :)
Your answer
Follow this Question
Related Questions
How to smooth/damp a movement? 0 Answers
Rotate smoothly on sphere 2 Answers
Lerp to Gaze Location Smoothing Motion Issues (Cardboard) 0 Answers
Custom touch control stops abruptly! 0 Answers
Moving to certain direction 1 Answer