- Home /
localRotation rotates by 180°
Hello! Im currently making an FPS and Im currently trying to keep the moment of my jump, but i want to be able to look around freely. I have an object (FPSController) which is the object that controls the movement and a child object (CameraRotation) which has the camera as a child object, so i can rotate my camera with the CameraRotation object. I can now look around the y-axis, but somehow i cant get the rotation of the x axis going:
else if(!isGrounded)
{
fpsCam.transform.localEulerAngles = new Vector3(y, 0, 0);
var xRot = Input.GetAxisRaw("Mouse X") * Time.deltaTime * lookSpeed;
fpsCam.transform.localRotation = new Quaternion(0,xRot,0,0);
}
With this code, my camera rotates by 180° everytime i move my mouse to the left or to the right. What I did could be complete nonsense, but Im still new to Unity so please excuse my sloppy coding. I dont expect you to provide code or anything, just simple instructions on how to fix the problem are more than enough.
Answer by JVene · Oct 22, 2018 at 05:30 PM
There is a lot one could say about what is wrong here, but let's start with the last line because that leads to an understanding that fixes everything else.
It appears you're familiar with Euler angles (your first line in the code block), but when you fashion a Quaternion (in the last line), you're fashioning the Quaternion with 4 parameters, and they are not Euler angles. Nothing in the constructor of a Quaternion will make much sense if you're not really familiar with the mathematics of quaternions (the subject of math beyond that of Unity). A quaternion (and thus Unity's Quaternion class) represents a complex number, like the imaginary numbers taught in algebra whereby the square root of a negative number becomes possible. The complex numbers taught in algebra are generally related to a 2 dimensional representation (like the imaginary numbers which have two values, an imaginary part and a real part). Quaternions, however, are complex numbers in 3 dimensions. None of the 4 parameters (x,y,z,w) have any meaning relative to rotations in Euler angles. This is the problem you're having. You are providing a value you intend to interpret as an angle in degrees, but the y parameter to the Quaternion constructor has no relationship to such an angle in the y axis.
To fix that, and therefore propagate that upwards to the rest of your code, look up the static function Quaternion.Euler, which forms a correct Quaternion object given Euler angles in 3 axis, as you're expecting. You could also consider using the eulerAngles property of localRotation as an alternative approach, or at the least in your translation between any quaternion and an orientation in 3 axis.
Hello! Thanks for the answer! Sadly, that only partly fixed my problem. $$anonymous$$y camera does not rotate by 180° anymore, which is a good thing, but now i cant really move my camera on the x-axis anymore. It looks like its being forced back to the rotation of the parent object. I understand that child objects inherit the position and rotation of the parent object, but i was hoping i could use localRotation, but, for some reason, it only works on the y-axis. Do you perhaps have any idea why that is the case? Thanks again for the help.
I'd need to see your updated code, because in the example you've posted you only request the X axis from the mouse.
Oh yeah my bad:
else if(!isGrounded)
{
var xRot = Input.GetAxisRaw("$$anonymous$$ouse X") * Time.deltaTime * lookSpeed;
fpsCam.transform.localRotation = Quaternion.Euler(y,xRot,0);
}
Your answer
Follow this Question
Related Questions
MouseLook won't work in Windows (Parallels) 3 Answers
Switching to the player camera 1 Answer
how to make my bones follow the camera(FPS) 0 Answers
Problems with Dani's Karlson FPS Movement Controller 1 Answer
FPS 3D camera view problem 0 Answers