Limit vertical rotation of Camera
Hello, I am new at Unity and C# and I am trying to create a basic FPS controller w/ a limited rotation about the x-axis (vertical rotation).
public float mouseSensitivity = 3.0f; //Mouse sensitivity.
public float viewRange = 60.0f;
float rotVertical = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Camera
float rotHorizontal = Input.GetAxisRaw("Mouse X") * mouseSensitivity;
transform.Rotate ( 0, rotHorizontal, 0);
rotVertical = Input.GetAxisRaw("Mouse Y") * mouseSensitivity;
rotVertical = Mathf.Clamp ( rotVertical, -viewRange, viewRange);
Camera.main.transform.localRotation *= Quaternion.Euler( -rotVertical, 0, 0);
However, I am still able to rotate 360 degrees vertically. Any help would be appreciated!
Answer by MrMeows · Oct 25, 2015 at 04:56 AM
Try clamping it after you apply the rotation.
Camera.main.transform.localEulerAngles = new Vector3( Mathf.Clamp( Camera.main.transform.localEulerAngles.x, -viewRange, viewRange), 0, 0);
By the way, Camera.main is a rather costly operation in terms of framerate. I recommend you use the following.
Transform cameraTransform;
void Start () {
cameraTransform = Camera.main.transform;
}
Then, replace all other instances of Camera.main.transform with cameraTransform. Ctrl+H might be helpful.
$$anonymous$$uch thanks! I have since replaced Camera.main.transform with cameraTransform. The clamp seems to work for the lower angle ( when i tilt the camera down to face the floor), however, it doesn't work when i tilt the camera up towards the sky. It will not go past a certain angle but ins$$anonymous$$d stutter at a smaller angle (which I'm sure is not the max value for my viewRange).
I replaced
Camera.main.transform.localEulerAngles = new Vector3( $$anonymous$$athf.Clamp( Camera.main.transform.localEulerAngles.x, -viewRange, viewRange), 0, 0);
with
Camera.main.transform.localEulerAngles = new Vector3( $$anonymous$$athf.Clamp( Camera.main.transform.localEulerAngles.x, -60, 60), 0, 0);
and the same problem occurs, the lower angle clamp works fine but now tilting upwards past a certain angle will reset the camera to face the floor.
Would you have any idea on how to solve this?
I have never used $$anonymous$$athf.Clamp, so I do not quite know how it works. I would try using if statements.
if(cameraTransform.localEulerAngles.x > viewRange)
{
cameraTransform.localEulerAngles = new Vector3( viewRange, 0, 0);
} else
{
if(cameraTransform.localEulerAngles.x < -viewRange)
{
cameraTransform.localEulerAngles = new Vector3( -viewRange, 0, 0);
}
}
Ah, the if statement seems to work out fine! Thank you very much for your help!
Answer by isopoly23 · Sep 17, 2018 at 06:04 PM
public float SpeedH = 10f;
public float SpeedV = 10f;
private float yaw = 0f;
private float pitch = 0f;
private float minPitch = -30f;
private float maxPitch = 60f;
void Update()
{
CameraRotate();
}
void CameraRotate() {
yaw += Input.GetAxis("Mouse X") * SpeedH;
pitch -= Input.GetAxis("Mouse Y") * SpeedV;
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
transform.eulerAngles = new Vector3(pitch, yaw, 0f);
}
This is a good method if you allow rotation on the y-axis as well. The accepted answer will always resets it back to 0. This also gives better control over the rotation speed.
Answer by ecreators · Apr 15, 2018 at 08:47 AM
I needed the logic to camera tilting angle (axis x) in inspector.
I use this:
// .. in void Update() in an if statement, only when left mouse is pressed if( Input.GetMouseButton(0)) { ... }
// vertically mouse y delta to last frame
var fY = Input.GetAxis(InputAxis.MOUSE_Y);
var v = gcProfile.CameraTurnSpeedVertically * fY;
// make rotation
var rotation = new Vector3(-v, 0, 0);
var tt = target.transform;
tt.Rotate(rotation, Space.Self);
// limits for angle in tilt x axis, as seen in inspector
const int bottom = 45;
const int top = -90;
// convert transform to values, as seen in inspector
var eulerTiltX = TransformUtils.GetInspectorRotation(tt).x;
Debug.Log("local euler tilt (x): " + eulerTiltX + " <- " + fY);
// x (inspector angle for tilt angle)
// as long as try to rotate camera vertically in fY direction and the angle in inspector reaches limit ...
if (eulerTiltX > bottom) // && fY < 0 -- not really nessessary
{
// then: set inspector value to bottom boundary
TransformUtils.SetInspectorRotation(tt, new Vector3(bottom, 0, 0));
}
else if (eulerTiltX < top) // && fY > 0 -- not really nessessary
{
TransformUtils.SetInspectorRotation(tt, new Vector3(top, 0, 0));
}
Answer by ammdhillon · Nov 10, 2020 at 01:35 PM
That's what I did and clamping works perfectly.
private float cameraPanMax = 20f; // 20 degree
private float cameraPanMin = 340f; // -20 degree (360 - 20)
// Update() starts here
float moveHorizontal = CrossPlatformInputManager.GetAxisRaw("Horizontal");
float moveVertical = CrossPlatformInputManager.GetAxisRaw("Vertical");
if (moveVertical != 0)
{
Vector3 rotateDir = new Vector3(-moveVertical, 0f, 0f);
CameraManager.activeCam.Rotate(rotateDir * rotSpeed * Time.deltaTime);
float currentAngle = CameraManager.activeCam.localEulerAngles.x;
bool isHigherThanRange = currentAngle > cameraPanMax && currentAngle < 50f;
bool isLowerThanRange = currentAngle < cameraPanMin && currentAngle > 300f;
if (isHigherThanRange)
{
CameraManager.activeCam.localEulerAngles = new Vector3(Mathf.Clamp(currentAngle, 0f, cameraPanMax), 0, 0);
}
if (isLowerThanRange)
{
CameraManager.activeCam.localEulerAngles = new Vector3(Mathf.Clamp(currentAngle, cameraPanMin, cameraPanMin), 0, 0);
}
}
Your answer
Follow this Question
Related Questions
How to clamp transform.Rotate values? 0 Answers
my fps player rotation problem 0 Answers
Is this a good way to Clamp a Rotation? 2 Answers
Rotation before 0 and after 360 0 Answers