How can i get the camera’s y axis to stay clamped? I know this code will allow the camera to flip upside down, but i’m not sure how to clamp it correctly.
void HandleInput() {
float horizontal = Input.GetAxis ("Mouse X") * mouseSensitivity;
float vertical = -Input.GetAxis ("Mouse Y") * mouseSensitivity;
vertical = Mathf.Clamp (vertical, -80, 80);
transform.Rotate (0, horizontal, 0);
Camera.main.transform.Rotate (vertical, 0, 0);
You’re using the camera’s current rotation as the only thing storing the camera’s rotation. I know that sounds sensible, but I’ve always had much better results keeping my own rotX and rotY variables.
Well, OK, I guess you’re pretty safe here with your horizontal (rotY) rotation here. But for the vertical, make your own rotX, which is the rotation angle around the X axis. Use Mathf.Clamp to limit this to a reasonable range, e.g. (-85, 85). Then just set Camera.main.transform.localRotation = Quaternion.Euler(rotX, 0, 0).
I did trying using this method before, but now the camera jitters on any Mouse Y movement and wants to stay flat at zero degrees.
void HandleInput() {
float horizontal = Input.GetAxis ("Mouse X") * mouseSensitivity;
float vertical = -Input.GetAxis ("Mouse Y") * mouseSensitivity;
vertical = Mathf.Clamp (vertical, -80, 80);
transform.Rotate (0, horizontal, 0);
Camera.main.transform.localRotation = Quaternion.Euler(vertical, 0, 0);
That’s because you’re setting the vertical to the input value, instead of adding it. “vertical” needs to be a property, not a local variable. And then line 4 should be
vertical -= Input.GetAxis("Mouse Y") * mouseSensitivity;
Setting both horizontal and vertical as global variables fixed it. Thank you very much.
I’m not sure how making the variables global fixed the problem but you should most likely store instance values like JoeStrout mentioned.
Example:
private float xSensitivity = 1.0f;
private float ySensitivity = 1.0f;
private float xClamp = 360.0f;
private float yClamp = 90.0f;
private Vector2 mouseInput = Vector2.zero;
void Update()
{
this.UpdateMouseInput();
this.RotateCamera();
}
private void UpdateMouseInput()
{
this.mouseInput.x += Input.GetAxis("Mouse X") * this.xSensitivity;
if (this.mouseInput.x < -0.0f) this.mouseInput.x += this.xClamp;
else if (this.mouseInput.x > this.xClamp) this.mouseInput.x -= this.xClamp;
this.mouseInput.y -= Input.GetAxis("Mouse Y") * this.ySensitivity;
if (this.mouseInput.y < -this.yClamp) this.mouseInput.y = -this.yClamp;
else if (this.mouseInput.y > this.yClamp) this.mouseInput.y = this.yClamp;
}
private void RotateCamera()
{
this.transform.rotation = Quaternion.Euler(this.mouseInput.y, this.mouseInput.x, 0.0f);
}
current_Mouse_Look = new Vector2(
Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse Y"));
look_Angles.x+= current_Mouse_Look.x * mouseSensitivity * (invert ? 1f : -1f);
this is not working for me it says this:
The name mouseSensitivity does not exist in the current context.
can someone please give a solution.
itsmerishabofficial:
current_Mouse_Look = new Vector2(
Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse Y"));
look_Angles.x+= current_Mouse_Look.x * mouseSensitivity * (invert ? 1f : -1f);
this is not working for me it says this:
The name mouseSensitivity does not exist in the current context.
can someone please give a solution.
mouseSensitivity is not a built-in thing. It is a property you would add to your own script.