- Home /
Rotation changing in untouched axis
Somehow, when changing in x and y axis, z axis is also changed. I can't sleep because of this, I really cant understand the problem.
I tried the Transform.Rotate method and also by calculating the resulting quaternions to check if it was a bug with the method or something but the result is the same.
Vector3 newHeadRotationDelta = Vector3.zero;
newHeadRotationDelta += Vector3.up * xInput * mouseSensibility * Time.deltaTime;
newHeadRotationDelta += Vector3.left * yInput * mouseSensibility * Time.deltaTime;
var newQuat = headObj.localRotation * Quaternion.Euler(newHeadRotationDelta);
headObj.localRotation = newQuat;
If someone can help me figure it out, I'd be much appreciated, this is driving me crazy.
Thanks
EDIT: Added more code for clarity
Answer by JustAbhi · Apr 10, 2020 at 05:35 AM
There is a very easy answer to your question. First make your camera in the scene a child of your player. Instead of changing the rotation of your player in both the axes just change Vertical rotation of your camera and the horizontal rotation of your Player separately.like:-
void Update()
{
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
Cursor.lockState = CursorLockMode.None;
transform.position = offset + plr.position;
float y = Input.GetAxis("Mouse Y") * mousesensitivity * Time.deltaTime;
float x = Input.GetAxis("Mouse X") * mousesensitivity * Time.deltaTime;
xRotation -= y;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, x, 0f);
plr.Rotate(Vector3.up * x);
}
The resulting rotation is the same if I rotate separately the left and up vector or sum them and I dont want to just change the camera, I'm not even talking about cameras. I'm already just changing the localRotation of the object and any parent of the gameobject is rotating, so it cant be caused by a gameobject up in the hierarchy. I'll edit to add more code for clarity.
Your answer
Follow this Question
Related Questions
Rotate Object Around Local X Axis , then Local Y Axis, then Local Z Axis (like a turtle) 0 Answers
How to turn a character frame independently? 1 Answer
Why is this rotation not performed as expected? 1 Answer
Why does the direction of the axis changes behaviour in Quaternion.AngleAxis? 0 Answers
How can I make it only rotate at X axis? 0 Answers