Mathf.Clamp not working, and how to keep z to 0?
Hi, I am currently making code on unity C# and I am encountering an error where I cannot clamp xRotation to -90 and 90 degrees, meaning I am allowed to look up and down with no limits, which I do not want. I'm not really an expert programmer and I've been following Brackey's tutorials (The video I was following https://www.youtube.com/watch?v=_QajrabyTJc&t=232s) but this isn't working and I am not sure why.
//Moving the player and camera dependant on the mouse x and mouse y
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
float xRotation = 0f;
//Clamping xRotation
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -9f, 9f);
//Transforming the camera and player on the mouse x
cam.transform.Rotate(xRotation, mouseX, 0f);
player.transform.Rotate(0f, mouseX, 0f);
That is the area where I am encountering this problem. I would also appreciate if you could tell me how to keep the z rotation to 0, I don't want my camera to be slanted.
Many thanks for any help :)
Answer by misher · Aug 28, 2021 at 07:54 AM
You are clamping the maximum amount of rotation at once (when mouse has moved) and do nothing actually to limit the camera or player rotation. What you need to do is to check your rotations after you have applied changes and maybe revert angles back to some desired limits
Your answer
Follow this Question
Related Questions
Third Person Camera - Issues to clamp rotation 0 Answers
Rotation before 0 and after 360 0 Answers
how do i Limit camera rotation on Y-Axis 1 Answer
Clamp transform.rotation? 0 Answers
Clamp transform.rotation? 0 Answers