Hi,
Thank you in advance for any assistance.
I am using the code below to make a cube rotate around all 3 axes following the position of the mouse when the right button is dragged. When the vertical rotation, around the x-axis, determined by the variable “yDeg” gets to 90 or 270 degrees it stops rotating. Swapping things out, guessing and checking, it only seems to have an issue when I use Mathf.Cos. Mathf.Sin seems to work fine, but of course doesn’t accomplish what I need.
It looks like it might be because of a change that occurs when the x axis rotation gets to 90. At 90 the y axis and z axis rotations suddenly adjust 180 degrees. Simply adding 180 degrees in that instance doesn’t seem to change the result. How do I deal with this issue?
private int speed = 12;
private float friction = .3f;
private float lerpSpeed = 10.5f;
private float xDeg;
private float yDeg;
private float zDeg;
private Quaternion fromRotation;
private Quaternion toRotation;
void Update ()
{
if(Input.GetMouseButton(1))
{
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * Mathf.Cos (transform.eulerAngles.y * Mathf.PI / 180) * speed * friction;
zDeg += Input.GetAxis("Mouse Y") * Mathf.Sin (transform.eulerAngles.y * Mathf.PI / 180) * speed * friction;
toRotation = Quaternion.Euler(yDeg,xDeg,zDeg);
}
fromRotation = transform.rotation;
transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
}
Is there an issue with Mathf.Cos or is there something else I’m doing wrong?
Thank you.