- Home /
How to clamp Y rotation of camera?
Hi! So, i am trying to make a third person shooter and i have the movement/ camera script done, but i have one little problem. When i press right-click to aim, i cant clamp the y rotation of the camera and i can rotate the camera 360 degrees. Also the values are weird, when the camera goes up the x descends, and when the camera goes down the x value gets bigger.
Here is the script:
private const float Y_ANGLE_MIN = 0f; private const float Y_ANGLE_MAX = 50f;
public Transform lookAt;
public Transform cameraTransform;
public Transform shootPoint;
public float rotationSpeed;
public bool isShooting;
private Camera cam;
private float distance = 5f;
private float currX = 0f;
private float currY = 0f;
private float mouseX = 0f;
private float mouseY = 0f;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
cameraTransform = transform;
cam = Camera.main;
}
private void Update()
{
currX += Input.GetAxis("Mouse X") * rotationSpeed;
currY += Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
currY = Mathf.Clamp(currY, Y_ANGLE_MIN, Y_ANGLE_MAX);
if(Input.GetKey(KeyCode.Mouse1))
{
if(isShooting == false)
{
cameraTransform.rotation = Quaternion.Euler(0, lookAt.rotation.eulerAngles.y, 0);
}
isShooting = true;
cameraTransform.position = shootPoint.position;
mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
cameraTransform.Rotate(mouseX * Vector3.up, Space.World);
lookAt.Rotate(mouseX * Vector3.up);
mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
cameraTransform.Rotate(mouseY * Vector3.right, Space.Self);
}
if(Input.GetKeyUp(KeyCode.Mouse1))
{
isShooting = false;
}
}
private void FixedUpdate()
{
if (isShooting == false)
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currY, currX, 0);
cameraTransform.position = lookAt.position + rotation * dir;
cameraTransform.LookAt(lookAt.position);
}
}
I would be glad if someone could help me out :)!
Answer by JxWolfe · Jan 05, 2019 at 08:30 PM
public Transform lookAt;
public Transform cameraTransform;
public Transform shootPoint;
public float rotationSpeed;
public float lockAngle = 45;
public bool isShooting;
private Camera cam;
private float distance = 5f;
private float currX = 0f;
private float currY = 0f;
private float mouseX = 0f;
private float mouseY = 0f;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
cameraTransform = transform;
cam = Camera.main;
}
private void Update()
{
currX += Input.GetAxis("Mouse X") * rotationSpeed;
currY += Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
currY = Mathf.Clamp(currY, Y_ANGLE_MIN, Y_ANGLE_MAX);
if(Input.GetKey(KeyCode.Mouse1))
{
if(isShooting == false)
{
cameraTransform.rotation = Quaternion.Euler(0, lookAt.rotation.eulerAngles.y, 0);
}
isShooting = true;
cameraTransform.position = shootPoint.position;
mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
cameraTransform.Rotate(mouseX * Vector3.up, Space.World);
lookAt.Rotate(mouseX * Vector3.up);
mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
cameraTransform.Rotate(mouseY * Vector3.right, Space.Self);
}
if(Input.GetKeyUp(KeyCode.Mouse1))
{
isShooting = false;
}
}
private void FixedUpdate()
{
if (isShooting == false)
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currY, currX, 0);
cameraTransform.position = lookAt.position + rotation * dir;
cameraTransform.LookAt(lookAt.position);
cameraTransform.eulerAngles = new Vector3(cameraTransform.eulerAngles.x,lockAngle,cameraTransform.eulerAngles.z);
}
}
that should the camera's y rotation...
I am not really sure what you meant by that. Could you please be more explicit?
whoops... basically this:
cameraTransform.eulerAngles = new Vector3(cameraTransform.eulerAngles.x,lockAngle,cameraTransform.eulerAngles.z);
should set the camera's y rotation to lockAngle no matter what you do. Sorry about that
Your answer
Follow this Question
Related Questions
How to make the player rotate to where an object is being thrown? 1 Answer
How do I rotate player's camera to peak around objects. (first person shooter leaning) 1 Answer
I want the camera, which is following my spaceship, to rotate to wherever the cursor is looking 0 Answers
Unity Rotate Around and Orbit Question 0 Answers
Third-person Mouse Look Script? 0 Answers