Tilt camera on movement
i have a problem. i want that if i move left or right, the camera slightly tilts left and right (on z axis).
now this works perfectly fine if i dont use my mouseLook but somehow together they wont work cause mouselook rotations are somehow overwriting my tilting. can someone give me a hint to how to pull this off? thx in advance
void Start()
{
initialRot = transform.localRotation;
// lock cursor
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
CameraRot();
// get mouse axis
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
// rotate player around y axis
player.Rotate(Vector3.up * mouseX);
// clamp cam rotation
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, topMaxRot, bottomMaxRot);
// rotate camera around x axis
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
void CameraRot()
{
float rotZ = -Input.GetAxis("Horizontal") * rotAmount;
Quaternion finalRot = Quaternion.Euler(0, 0, rotZ);
transform.localRotation = Quaternion.Lerp(initialRot, finalRot, smoothRotAmount);
}
Answer by ThePolygators · Feb 16, 2020 at 07:21 AM
i actually just figured it out. i pretty much clamped the z axis rotation in this line transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
so incase anyone wondering i just made that the z axis is not 0 rather than just the rotation of the current zaxis. thats weird to explain but here is the fixed line
Vector3 v = transform.rotation.eulerAngles;
transform.localRotation = Quaternion.Euler(xRotation, 0, v.z);
It worked for me. Thanks a million! Do change the line that says Quaternion finalRot = Quaternion.Euler(0, 0, rotZ);
to Quaternion finalRot = Quaternion.Euler(xRotation, 0, rotZ);
or else you cannot look up.
Answer by anon7998 · Feb 16, 2020 at 07:33 AM
This usually has to do with at what point do you call the move part of the script and the camera position part of the script. I would take a look at your script to make sure that the camera looks at, or sits at the right point both when its moving and when its stationary.
Your answer
![](https://koobas.hobune.stream/wayback/20220612223416im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Recognize when ever camera looks up and turns back down 0 Answers
Camera Quaternion.RotateTowards without rolling/banking 1 Answer
Object Rotate with Camera 2 Answers
Camera rotation with mouse cursor 0 Answers
Rotating Camera on click and drag 0 Answers