Quaternion.RotateTowards for camera positions is buggy
I am trying to have set camera positions that the user can scroll between using two keys. I can switch the positions fine but rotating doesn't work. I have the position correct but not this.
private Quaternion[] Rotations;
private Vector3[] Positions;
Starting with two arrays. Obviously the rotations are Quaternion so that is what I am using for this scenario. I have only 5 positions to switch between so:
Rotations = new Quaternion[5];
Positions = new Vector3[5];
I initialize our array and set the initial positions.
Rotations [0] = transform.rotation;
Positions [0] = transform.position;
As an example here are our first two different positions. All the rest are extremely similar and inputting these values manually into the transform component seems to work fine.
Positions [1] = new Vector3 (11.67f, 4.52f, -5f);
Rotations [1] = new Quaternion (25, -90, 0, 0);
Then when they are switched we check if our position is not correct according the value inside the array on the current position index. If it is not then rotate and move towards these positions:
if (transform.position != Positions [CurrentCamIndex]) {
transform.position = Vector3.MoveTowards (transform.position, Positions [CurrentCamIndex], 1);
transform.rotation = Quaternion.RotateTowards (transform.rotation, Rotations [CurrentCamIndex], 1);
}
But as soon as I run this I get some weird rotations:
As you can clearly see the transform component in the inspector contains some values I don't even have here. Is there anything I am missing or anything I am doing incorrectly?
Answer by Owen-Reynolds · Dec 30, 2016 at 04:08 PM
Don't use new Quaternion(
. It gives junk results. Use Quaternion.Euler(
instead.
Inside, quaternions use complex non-degree math. new Quaternion
and rotation.x
are there for the few people who understand it. For everyone else, Quaternion.Euler
and rotation.eulerAngles.x
translate it into degrees for us (My longer explanation is at taxesforcatses-dot-com in the move/rotate section (the first section about rotations, near the bottom.)