A
A
Alexander Andropov2019-03-01 16:21:47
Unity
Alexander Andropov, 2019-03-01 16:21:47

Why doesn't the Quaternion.Slerp condition work?

Good day friends.
There is a code :

public class Man : MonoBehaviour 
{       
public float Speed, RotateSpeed, RotateGradus;
  void Update () {		
      if (RotateGradus == 90)
            {
                if (transform.eulerAngles.z < RotateGradus)
                {
                    transform.rotation = Quaternion.Slerp(transform.rotation, new Quaternion(transform.rotation.x, transform.rotation.y, RotateGradus, 2), RotateSpeed * Time.deltaTime);
                }                
            }
        
            if (RotateGradus == 270)
            {		
                if (transform.eulerAngles.z < RotateGradus)
                {                    
                    transform.rotation = Quaternion.Slerp(transform.rotation, new Quaternion(transform.rotation.x, transform.rotation.y, RotateGradus, 2), RotateSpeed * Time.deltaTime);
                    Debug.Log("сработало");
                }
            }
   }
}

As a result, when RotateGradus is 90, transform.rotation.z of the object is 0, then everything works fine, and in the second part of the code, when RotateGradus is 270, and transform.rotation.z of the object is 180 - Quaternion.Slerp does not work at all, although the message from debug "worked" is visible in the console. In runtime, when I look, the Z angle of the object moves to 179.151 and freezes. I start to change manually (move the slider), it returns back to 179.151. Although, in the end, it should turn into 270 if the code worked.
Please explain why this is happening, everything has already been reviewed. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-03-01
@LongOf

thought to take out in the answer or not.
but no one sets quaternions like that))
this is a mistake.
transform.rotation = quaternion is fine here.
but it is better to set the required rotation differently.
for example - add 90 degrees to the current rotation. (I hope it is obvious that you need to set the rotation target once, and not every update)
or in your case, simply set the required rotation through Quaternion.Euler. but keep in mind that this will be a local turn.
In general, you should read about quaternions. that there and the order of multiplication is important.
and that they are multiplied, not added.
read about them articles on the same hub. there was a couple of explanations about pitch, yaw, and so on. and analogies with quaternions in order to somehow understand them))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question