user9609349
user9609349

Reputation: 91

How to limit camera's vertical rotation Unity 3D

I would like to limit my vertical rotation of the camera so that it can't do a 360 spin. I have tried a lot of tutorials but nothing worked for me so i .

Please also check my code.

[RequireComponent(typeof(PlayerMoto))]

public class PlayerController: MonoBehaviour {

  public float speed = 5, sensetivity;
  private PlayerMoto motor;
  public GameObject hands;
  public camera cam;
  float lookUpMax = .6 f;

  void Start() {
    motor = GetComponent < PlayerMoto > ();
    cam = GetComponent < camera > ();
  }

  void Update() {
    //cam.transform.localEulerAngles = new Vector3(cam.transform.localEulerAngles.x, 0, 0);
    float _xmove = Input.GetAxis("Horizontal");
    float _zmove = Input.GetAxis("Vertical");
    Vector3 _moveHorizontal = transform.right * _xmove;
    Vector3 _movVertical = transform.forward * _zmove;
    Vector3 _velocity = (_moveHorizontal + _movVertical) * speed;
    motor.Move(_velocity);
    float _yRot = Input.GetAxis("Mouse X");
    Vector3 _rotation = new Vector3(0 f, _yRot, 0 f) * sensetivity;
    motor.Rotate(_rotation);
    float _xRot = Input.GetAxis("Mouse Y");
    Vector3 _cameraRotation = new Vector3(_xRot, 0 f, 0 f) * sensetivity;
    motor.RotateCamera(_cameraRotation);
    if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) {
      for (; speed <= 15; speed++) {
        speed = 15;
      }
    } else {
      speed = 10;
    }
  }
}

Thank you very much for your kind help. I really appreciate every single comment to try and help me in this amazing journey.

Upvotes: 0

Views: 1226

Answers (2)

Shubham Pandit
Shubham Pandit

Reputation: 47

Try this out

 private void Rotation()
    {
        x_axis += speed * Input.GetAxis("Mouse X"); // speed = 2f;

        y_axis -= speed * Input.GetAxis("Mouse Y");
        y_axis = Mathf.Clamp (y_axis, -45, 45); // limits vertical rotation

        transform.eulerAngles = new Vector3(y_axis, x_axis, 0.0f);
    }

Upvotes: 1

kefren
kefren

Reputation: 1102

If I understood your code, the part that moves the camera is:

float _xRot = Input.GetAxis("Mouse Y");
Vector3 _cameraRotation = new Vector3(_xRot, 0f, 0f) * sensetivity;

Then, your code calls this method on another class

motor.RotateCamera(_cameraRotation);

which probably (since the previous suggestion did not work) applies the rotation through

GameObject.Rotate(_cameraRotation);

In order to limit your camera’s rotation, we need to directly apply the rotation, and it cannot be clamped if the rotation is applied by adding a rotation to the existing rotation. So, let’s assume you can skip that call and directly apply the rotation (I don’t know if there will be side effects), and let’s assume your cam variable is your camera. If all these assumptions are correct, you can try:

float _xRot += Input.GetAxis("Mouse Y") * sensetivity;
xRot = Mathf.Clamp(_xRot, xMin, xMax);
cam.transform.rotation = Quaternion.Euler(_xRot, 0.0f, 0.0f);

Remember to comment out

//motor.RotateCamera(_cameraRotation);

You can declare

public float xMin;
public float xMax;

And experiment with values until you find your optimal ones.

I strongly suspect that the code I suggested will not solve all your problems, or it could add issues, because the actual transformations of both your player and the camera are applied by another script, that is not provided. In that case, you can provide us also that code, but I suggest you try writing your own code, that you can customize to your needs.

As for why clamping a rotation is not as easy as it seems, this post is interesting: Rotations, Quaternions and Euler Angles

Upvotes: 0

Related Questions