I’ve been trying to write the logic for rotating an object that is nested in a parent object.
The parent object will be rotated around the Y axis (and possibly in other directions in future, so I don’t want to tie rotating the child object to using and world space coordinates, as it should rotate relative to the parent.)
The child object needs to rotate in 90 degree increments in X and Z depending on the view point. Basically there are turn left and turn right buttons, if looking straight on (on positive Z direction) the turn right will rotate the child 90 degrees around Z, if looking from the side (along the negative x direction) the turn right will rotate the child 90 degrees around x.
I’ve been trying to do this with euler angles but I think I’m running into gimble lock and think I need to be using quaternions.
Currently I have this, it appears to be working if the child begins at 0, 0, 0 rotation, but if it’s already been rotated it is behaving strangely and rotating in unintended directions
public void TurnRight(ViewPoint viewPoint, int rotations) {
switch (viewPoint) {
case ViewPoint.A:
RotateZ(rotations);
break;
case ViewPoint.B:
RotateX(rotations);
break;
case ViewPoint.C:
RotateZ(-rotations);
break;
case ViewPoint.D:
RotateX(-rotations);
break;
}
}
private void RotateZ(int value) {
transform.Rotate(new Vector3(0, 0, 90 * value));
}
private void RotateX(int value) {
transform.Rotate(new Vector3(90 * value, 0));
}