- Home /
Character Controller - rotation changes z axis
Hello, I am new to Unity and just trying to implement character controller. I do have a problem with move movement - where I am rotating vertical movement around x-axis and horizontal around y-axis. What I do not understand though is that also z-axis rotation is applied.
Here is my code:
private void Rotate()
{
rotationX += rotateUD * SensitivityX;
rotationY += rotateLR * SensitivityY;
Quaternion quaternionX = Quaternion.AngleAxis(rotationX, Vector3.left);
Quaternion quaternionY = Quaternion.AngleAxis(rotationY, Vector3.up);
transform.localRotation = originalRotation * quaternionX * quaternionY;
Debug.Log(quaternionY);
}
private void GetInputs()
{
moveLR = Input.GetAxis("Horizontal");
moveFB = Input.GetAxis("Vertical");
rotateUD = Input.GetAxis("Mouse Y");
rotateLR = Input.GetAxis("Mouse X");
}
If I debug quaterninX
and quaternionY
, both have z vector as 0. Therefore I am really confused. Thanks for hint.
Answer by gordiri · Jan 25, 2018 at 12:16 PM
I think problew is the way how you rotate. originalRotation may have non zero z rotation and when you multiply you are getting non zero z as result. I suggest you to use Transform.Rotate( ) instead its much easier
I checked that during initialization, the originalRotation
has value "(0.0, 0.0, 0.0, 1.0)". I will try Transform.Rotate().
I really do not understand. Even transform.Rotate(new Vector3(rotationX, rotationY, 0));
is giving me rotation along z-axis. Before I move with a mouse, z-rotation is zero.
It's the same result - z-axis rotation is still changing :(. Basically, whenever I remove one axis - either x or y, then z rotation is not applied. It's applied only, when x and y rotations are used.
Answer by Izmail360 · Jan 26, 2018 at 07:27 AM
I made a progress, but it's still unclear to me. I have two coded. One works and second does not.
Working code (very simmilar to posted above):
rotationX += Input.GetAxis("Mouse Y") * MovementSpeed;
rotationY += Input.GetAxis("Mouse X") * MovementSpeed;
Quaternion quaternionX = Quaternion.AngleAxis(rotationX, Vector3.left);
Quaternion quaternionY = Quaternion.AngleAxis(rotationY, Vector3.up);
transform.localRotation = originalRotation * quaternionY * quaternionX;
Bad code (rotates also around z-axis):
rotationX = Input.GetAxis("Mouse Y") * MovementSpeed;
rotationY = Input.GetAxis("Mouse X") * MovementSpeed;
transform.Rotate(rotationX, rotationY, 0);
Can somebody explan the problem? It looks like I am missign some king of very basic.
Your answer
Follow this Question
Related Questions
Colliders Not Working 1 Answer
Why is my character not flipping? 2 Answers
Why is my character controller cutting to an overview? 0 Answers
Code for - Rigidbody vs Character Controller 0 Answers
Framerate independent Movement 2 Answers