- Home /
Cannot rotate camera
I'm trying to implement a leaning mechanic, and I've got it to work on a cube, but it doesn't work on my camera. I think it has something to do with my first person controller script, but I can't seem to fix it.
The leaning script:
void Update () {
//Check right Input
if (Input.GetKey(LeanRight))
{
float angle = Mathf.MoveTowardsAngle(transform.localEulerAngles.z, -leanAngle, speed * Time.deltaTime);
transform.localEulerAngles = new Vector3(0, 0, angle);
}
//Check left Input
else if (Input.GetKey(LeanLeft))
{
float angle = Mathf.MoveTowardsAngle(transform.localEulerAngles.z, leanAngle, speed * Time.deltaTime);
transform.localEulerAngles = new Vector3(0, 0, angle);
}
//Lean back
else
{
float angle = Mathf.MoveTowardsAngle(transform.localEulerAngles.z, 0, speed * Time.deltaTime);
transform.localEulerAngles = new Vector3(0, 0, angle);
}
}
The part of the first person controller script that is at fault:
if (!lockMouseMovement)
{
//RotY
**rotY = Mathf.Clamp(rotY, -upDownRange, upDownRange);
mainCam.transform.localRotation = Quaternion.Euler(yVelocity, 0, 0);**
//RotX
transform.Rotate(0, xVelocity, 0);
}
I changed:
mainCam.transform.localRotation = Quaternion.Euler(yVelocity, 0, 0);
to this:
mainCam.transform.localRotation = Quaternion.Euler(yVelocity, 0, mainCam.transform.localRotation.z);
But that doesn't work. What should I put instead for me to be able to tilt the camera from another script?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612174253im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do you make a mouse sensitivity option? 0 Answers
Reload function in my script not working at all. No error messages in compiler. 4 Answers
Move gameobject pivot 6 Answers