- Home /
Difficulty making a complex camera rotation
I'm creating a turn based game set in space in which two teams of players are on opposite sides of a three-dimensional playing field. The white team and black team begin the game with the same orientation, except that the white team is rotated 180 on the Y axis and also rotated 180 degrees on the X axis (meaning the two teams are facing each other and one team is upside down).
To make play even between turns, I need the camera to perform a complex rotation so that both players can control their own team from the same perspective on their turn. My goal is to have either player, at the beginning of his own turn, facing toward the right side of the screen with his head up toward the top and his feet down toward the bottom. My goal is to make a smooth camera rotation from (0,0,0) to (180, 0, 180) or (0, 180, 180), and then make it go back when the turn changes again.
I am not having a problem changing the position of the camera so that it points at the correct team, but I am having trouble with the rotation.
In attempting to solve this problem, I have created a focal point around which the camera orbits. When a turn change begins, the variable slerpOnBlack or slerpOnWhite turns to true. As long as this variable is true, one of the following two methods will fire on every update:
void SlerpCameraBlack(){
if (new Vector3(CameraFocus.transform.rotation.x, CameraFocus.transform.rotation.y, CameraFocus.transform.rotation.z) != new Vector3(0, 0, 0)) {
CameraFocus.transform.rotation = Quaternion.Slerp (CameraFocus.transform.rotation, Quaternion.Euler(new Vector3(0, 0, 0)), (10*Time.deltaTime));
} else {
SlerpOnBlack = false;
}
}
void SlerpCameraWhite(){
if (new Vector3(CameraFocus.transform.rotation.x, CameraFocus.transform.rotation.y, CameraFocus.transform.rotation.z) != new Vector3(0, 180, 180)) {
CameraFocus.transform.rotation = Quaternion.Slerp (CameraFocus.transform.rotation, Quaternion.Euler(new Vector3(0, 180, 180)), (10*Time.deltaTime));
} else {
SlerpOnWhite = false;
}
}
Use of these functions results in inconsistent results which I do not understand, and which lead me to believe that I must not understand clearly how to use quaternions.
Can anyone help me resolve this?
Answer by JustinTheSwift · Jul 05, 2018 at 12:09 AM
Evidently this was a rounding problem. My camera was slerping to almost the position it was supposed to reach, but wasn't actually reaching it. As such, the SlerpOn booleans never turned to false. This caused the camera to get stuck between the black and white positions. I solved the problem by rounding my numbers and forcing blackSlerpOn to turn false when whiteSlerpOn is true and vice versa.
Answer by hectorux · Jul 04, 2018 at 11:44 PM
You are comparing a x,y,z of a Quaternion instad of the Euler angles of your camera, try changing in the comparison the "rotation" to "eulerAngles"
Whether or not your solution might have helped I can't say, but I've come up with a solution without it. Thanks for helping anyway.
Your answer
![](https://koobas.hobune.stream/wayback/20220612170046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tilt first person camera's z axis while turning 0 Answers
Is transform.rotation in Quaternions? 2 Answers
How to fix flipping Euler anlges? 0 Answers
Addition Quaternion 0 Answers