- Home /
Y rotation of the Camera doesn't clamp
Hi there! So I am making a third person shooter and i have a small issue with my aiming controls. When I press the right click mouse button it goes into aiming as wanted, but for some reason the rotation on the Y axis of the camera doesnt clamp and i can rotate the camera 360 degrees which I don't want.
Here is the script:
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;
mouseY = Mathf.Clamp(mouseY, -25f, 30f);
cameraTransform.Rotate(mouseY * Vector3.right, Space.Self);
}
if (Input.GetKeyUp(KeyCode.Mouse1))
{
isShooting = false;
}
NOTE: This is not the full script!
Your post is not is not displaying it as code. Insert a couple of line breaks, click on the code icon in the toolbar (101010) then paste your code.
Answer by Ady_M · Jan 06, 2019 at 11:35 AM
First of all, you need to use clamp on the code that's working with Rotate (Vector.up) since that's Yaw, not the other one.
Secondly, your code is currently clamping how fast you can rotate, not how far.
On MouseButtonDown(1) (meaning only once) you need to save your current lookAt in a separate property of the script, for example lookAtOnAimStart, so you can later limit how far you can yaw.
I just noticed that you're rotating the camera directly without applying the value of lookAt to it. What are you using the lookAt variable for?
The lookat variable is for the player and as I said I am using it in the other part of the code which is irrelevant for my issue right now. I will try the thing you said and then I will give you updates. Thanks for the help :)!
I see. Because I like the idea of managing your own lookAt so that you're not working only with the camera's transform.
You're welcome :) I can update my answer when you've done that. Happy coding.