Applying two independent rotations
I´m doing a racing game where the player can perform tricks in the air. The tricks consist of local rotations around the transform´s x, y and z axis. For simpler tricks, the player will just do one complete rotation a single axis, but for more complicated tricks he may do more than one rotation around 2 or more axis at the same time. This is where my problem lies, for rotations around a single axis everything works fine, but when I try to rotate around 2 axis at the same time I get weird behaviours.
This is a code snipet that calculates the rotation in every axis:
if (currentTrick > -1){
for (int i=0; i<3; i++){
if (currentTrickTurns[i] < trickTurns[i]){
if (trickTurnSpeed[i] > 0f){
if (trickRotation[i] + trickTurnSpeed[i] < 360f) trickRotation[i] += trickTurnSpeed[i];
else{
trickRotation[i] = 0f;
currentTrickTurns[i]++;
}
}else if (trickTurnSpeed[i] < 0f){
if (trickRotation[i] + trickTurnSpeed[i] > 0f) trickRotation[i] += trickTurnSpeed[i];
else{
trickRotation[i] = 360f;
currentTrickTurns[i]++;
}
}
}else{
trickTurnSpeed[i] = 0f;
trickFinished[i] = true;
}
}
}
if (trickFinished[0] && trickFinished[1] && trickFinished[2]) TrickEnd();
This calculates the current rotation in degrees in each angle and the number of turns, setting the speed to 0 when the trick is completed.
For applying the rotations I´ve tried many things with different results.
Using transform.Rotate:
transform.Rotate(new Vector3(trickTurnSpeed[axisX], 0f, 0f));
transform.Rotate(new Vector3(0f, trickTurnSpeed[axisY], 0f));
transform.Rotate(new Vector3(0f, 0f, trickTurnSpeed[axisZ]));
The player rotates around all axis at the same time. That is, instead of turning around X, then Y and then Z, the resulting rotation is like the player rotated around Vector3(trickTurnSpeed[axisX], trickTurnSpeed[axisY], trickTurnSpeed[axisZ]).
Using quaternions:
qTrickRotationX = Quaternion.AngleAxis(trickRotation[axisX], Vector3.right);//transform.right);
qTrickRotationY = Quaternion.AngleAxis(trickRotation[axisY], Vector3.up);//transform.up);
qTrickRotationZ = Quaternion.AngleAxis(trickRotation[axisZ], Vector3.forward);//transform.forward)
transform.localRotation = qTrickRotationX*qTrickRotationY*qTrickRotationZ;
I get erratic turns this way.
Using eulerAngles:
transform.eulerAngles = newRot + new Vector3(trickRotation[0], trickRotation[1], trickRotation[2]);
I get the same as using transform.Rotate.
Using transform.RotateAround:
transform.RotateAround(transform.position, transform.right, trickTurnSpeed[axisX]);
transform.RotateAround(transform.position, transform.up, trickTurnSpeed[axisY]);
transform.RotateAround(transform.position, transform.forward, trickTurnSpeed[axisZ]);
Again, I get wrong rotations.
I´ve tried many more things, all with undesired results. Please help me, I´m running out of ideas :(
Your answer
Follow this Question
Related Questions
Rotate Camera in Y axis without visibly changing angle in Z axis 2 Answers
transform.rotation interferes with transform.Rotate 1 Answer
How do I move my 2d sprite left and right but also rotate in- place! 1 Answer
Rotating transform around axis specified by another object in world 1 Answer
How do I rotate a game object around a custom pivot? 1 Answer