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