- Home /
Question by
JamesHanson1 · May 18, 2020 at 08:53 AM ·
camerafpscamera-movementclamped rotation
how to clamp my FPS camera,How to do clamp my FPS camera
Hi, I have been trying to make an Aim Trainer in Unity and among all of my other problems I am trying to clamp the camera view so that the player is only able to look at the one wall that the targets will pop up on while still being able to move somewhat up and down. So far I have this code but I am new to unity and have no clue how to clamp the view at all so any help would be amazing
public class PlayerCameraController : MonoBehaviour
{
[SerializeField] private float sensitivity;
[SerializeField] private float smoothing;
private GameObject player;
private Vector2 smoothedVelocity;
private Vector2 currentPos;
private void Start()
{
player = transform.parent.gameObject;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
RotateCam();
}
private void RotateCam()
{
Vector2 inputValues = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
//scaling input down
inputValues = Vector2.Scale(inputValues, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputValues.x, 1f / smoothing);
smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputValues.y, 1f / smoothing);
currentPos += smoothedVelocity;
transform.localRotation = Quaternion.AngleAxis(-currentPos.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(currentPos.x, player.transform.up);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612234621im_/https://answers.unity.com/themes/thub/images/avi.jpg)