Question by
EL74 · Mar 07 at 09:08 AM ·
camera rotatetiltcamera rotationtilting
How to tilt 1st person camera's Z axis when moving left and right?
Hi everyone,
I'm still somewhat new to unity and was trying to create a 1st person controller. Basically, I wanted to tilt the camera's z axis whenever the player would be pressing A or D (similar to Quake's camera tilt). I've encountered an issue with my mouse script, which is locking the camera's z axis.
Here's the very basic tilting script:
[Header("Peaking")]
public Transform PeakLeft;
public Transform PeakRight;
public Transform IdlePeak;
public float lerpTime = 0.05f;
void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.position = Vector3.Lerp(transform.position, PeakLeft.position, lerpTime);
transform.rotation = Quaternion.Lerp(transform.rotation, PeakLeft.rotation, lerpTime);
}
else
{
transform.position = Vector3.Lerp(transform.position, IdlePeak.position, lerpTime);
transform.rotation = Quaternion.Lerp(transform.rotation, IdlePeak.rotation, lerpTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.position = Vector3.Lerp(transform.position, PeakRight.position, lerpTime);
transform.rotation = Quaternion.Lerp(transform.rotation, PeakRight.rotation, lerpTime);
}
else
{
transform.position = Vector3.Lerp(transform.position, IdlePeak.position, lerpTime);
transform.rotation = Quaternion.Lerp(transform.rotation, IdlePeak.rotation, lerpTime);
}
}
And here's my the mouse script (it's inside of the general character controller script):
private void MouseLook()
{
rotationX -= Input.GetAxis("Mouse Y") * lookSpeedY;
rotationX = Mathf.Clamp(rotationX, -upperLook, lowerLook);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeedX, 0);
}
Whenever I disable the Quaternion.Euler lines in MouseLook(), the tilt script works perfectly fine, so I assumed it's because the z axis is getting locked at 0. I'm not entirely sure what should I do. Is there a way I could modify my mouse script to make the tilting work? Thanks.
Comment