# 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);
}
``````

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 in case anyone wondering I just made that the `z` axis is not `0` rather than just the rotation of the current `z` axis. That’s weird to explain but here is the fixed line

``````Vector3 v = transform.rotation.eulerAngles;
transform.localRotation = Quaternion.Euler(xRotation, 0, v.z);
``````

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.

@ThePolygators

Change a bit your post. It doesnt work.

I found this to be working well with this setup:
[198457-снимок.png|198457]

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCam : MonoBehaviour
{

public float sensativityX, sensativityY;
public Transform player;
public Transform cameraPosition;
public Transform orientation;

float xRotation;
float yRotation;

public float _tiltAmount = 5;
public float _rotationSpeed = 0.5f;

void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}

// Update is called once per frame
void Update()
{
Tilt();
transform.position = cameraPosition.position;

float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensativityX;
float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensativityY;

xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);

yRotation += mouseX;

Vector3 v = transform.rotation.eulerAngles;
transform.localRotation = Quaternion.Euler(xRotation, yRotation, v.z);
orientation.rotation = Quaternion.Euler(0, yRotation, 0);

}

public void Tilt()
{
float rotZ = -Input.GetAxis("Horizontal") * _tiltAmount;

Quaternion finalRot = Quaternion.Euler(xRotation, yRotation, rotZ);
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, finalRot, _rotationSpeed);
}

}
``````

it works yes, but i found that i get more leaning with lower frame rate, i.e if i get 15 to 20 degrees while rotating at 60fps, i get 5 to 8 degrees at 450fps along with stuttering as if it tries to get back to a value of 0.