- Home /
How to Limit Z Axis on my FirstPersonCamera?
I've found other answers, but they are simply not working with my code. I'm a beginner, so I'm probably just not doing it right. Here is my code:
using UnityEngine; using System.Collections; public class FirstPersonCamera : MonoBehaviour {
public bool lockCursor;
public float mouseSensitivity = 10;
public Vector2 pitchMinMax = new Vector2 (-27, 50);
public float rotationSmoothTime = .12f;
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
float yaw;
float pitch;
void Start() {
if (lockCursor) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void LateUpdate () {
yaw += Input.GetAxis ("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp (pitch, pitchMinMax.x, pitchMinMax.y);
currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.eulerAngles = currentRotation;
}
}
I've always found spherical coordinates to be a pretty straightforward solution to these kinds of problems. What about your code isn't working btw? Have you tried printing out your pitch
value to make sure it's where you expect?
edit: Also, angles aren't as intuitive to clamp as you might think. You want your maximum to be +50 here, but +50 is equivalent to-310 as far as degrees are concerned. Your clamp will convert -310 to -27.
Do you get better behavior with this?
pitch = $$anonymous$$athf.Clamp (pitch % 360 - 180, pitch$$anonymous$$in$$anonymous$$ax.x, pitch$$anonymous$$in$$anonymous$$ax.y);
$$anonymous$$y code is working fine, I'm just wanting to limit the z axis rotation, because in my first person camera, my character model is clipping when you turn left/right far enough. Right now, you can turn 360 degrees and I'm wanting to limit it to around 90, but I'm not sure how to implement that.
Answer by Senodric · Mar 31, 2018 at 03:24 PM
Plese be more specific about your question. What you mean by Limiting your Z axis? It's the Z rotation, Z position?
I apologize. I’m wanting to limit the rotation on the z axis, because right now on my first person camera, I’m able to rotate 360 degrees and my character model is clipping.
To limit the rotaion on the z axis you cold do the same thing you did to clamp the x axis (pitch). Just create a float roll to store the z value and them use:
float roll;
roll = $$anonymous$$athf.Clamp(roll, $$anonymous$$Value, maxValue);
currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (pitch, yaw, roll), ref rotationSmoothVelocity, rotationSmoothTime);
I'm getting errors saying "$$anonymous$$Value and maxValue don't exist in the current context". Should I create ints for them? Or is that where I add the numbers? I added -90 and 90 and nothing happened
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Camera doesnt allow me to change its position / rotation (help) 1 Answer
How to Stop Camera From Rotating when x,y,and z is equal to 90 degrees 1 Answer
How to apply a deadzone to camera for mouse position on Unity's default FPC? 0 Answers