Setting a camera X Rotation cap
So I'm trying to limit the X rotation of the camera so it can't go too far up and too far down, but with the code I currently have, nothing happens. Any ideas? - cameraTransform = the camera's Transform component - xhighcap = the max x can go, 45 - xlowcap = the minimum x can go, -25
Here is the code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraLock : MonoBehaviour {
// Variables and components
public float xlowcap;
public float xhighcap;
public Transform cameraTransform;
// Update is called once per frame
void Update () {
// Checks if Camera X rotation is over the caps
if (cameraTransform.rotation.x >= xhighcap)
{
// Sets the camera X rotation back to the cap.
cameraTransform.rotation = Quaternion.Euler(xhighcap, cameraTransform.rotation.y, cameraTransform.rotation.z);
}
if (cameraTransform.rotation.x <= xlowcap)
{
// Sets the camera X rotation back to the cap.
cameraTransform.rotation = Quaternion.Euler(xlowcap, cameraTransform.rotation.y, cameraTransform.rotation.z);
}
}
}
When I run this, It's like I never even put the script on in the first place, no errors. no effect. Please help! Thanks
Answer by Cleroth · Aug 09, 2017 at 02:45 PM
Use cameraTransform.eulerAngles.x
, not cameraTransform.x
(that gets the Quaternion component).
Thank you so much! but how do I actually set the cap, because it checks correctly but I cant actually make it go back to the normal x rotation, how do I edit only one value of the rotation?
Your answer
Follow this Question
Related Questions
My Player rotates on the Z axis, but not the X axis. Why? 0 Answers
Look rotation reset back to center (lerp) 0 Answers
Rotating Camera around Player's X-axis while rotating Player around its Y-axis (using mouse input) 0 Answers
Camera Quaternion.RotateTowards without rolling/banking 1 Answer
rotating camera jumping around y axis? 0 Answers