I have a cube that I want to rotate. I want to randomly calculate a new rotation for the cube and slerp to the new rotation. I want to be able to rotate it around the y and x axis but I don’t want it to rotate around the z axis.
Here is the code I tried to use for this purpose but it seems to rotate along only the z axis even though my “newRotation” vector3 has a z value that never changes. I’m sure this is just something about Quaternion rotation that I don’t understand, please help! ![]()
private void slowMove()
{
// if we're in our origin rotation, then pick a new random rotation
// x +/- 90 degrees, y +/- 90 degrees and z +/- 90 degrees from our
// current rotation
float rotationVariance = 90;
var newRotation = new Vector3(_originalRotation.x + UnityEngine.Random.Range(rotationVariance * -1, rotationVariance), _originalRotation.y + UnityEngine.Random.Range(rotationVariance * -1, rotationVariance), _originalRotation.z);
_toRotation = Quaternion.FromToRotation(_originalRotation.eulerAngles, newRotation);
//UnityEngine.Random.rotation;
}
void Update()
{
if (_toRotation != null)
{
// slowly move to our new rotation over time
transform.rotation = Quaternion.Slerp(transform.rotation, _toRotation.Value, Mathf.Clamp01(Time.deltaTime * 10));
// until the difference in angle between our current rotation and
// our destination rotation is < 1
if (Quaternion.Angle(transform.rotation, _toRotation.Value) < 1)
{
_toRotation = null;
}
}
}
Here’s an ani gif showing the rotation. Each time it rotates again it’s after I have clicked it.
As you can see it’s only rotating on the z axis for some reason even though the z value should not be changing.