Help with camera clamps

Updated on February 1, 2018 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
11 on January 30, 2018

Was wondering if anyone could help me figure out clamping camera rotations i can’t seem to find anything anywhere. I can move and look around fine but there is currently no way of stopping the rotation on the X axis of my camera and because of that you can continuously do backflips and front flips. Not really something I’d like in this game. Any help is much appreciated.

Code Below:

using UnityEngine;

[RequireComponent(typeof(PlayerMoter))]
public class PlayerController : MonoBehaviour {

    [SerializeField]
    private float speed = 1000f;
    [SerializeField]
    private float sense = 3f;

    private PlayerMoter motor;
    private Vector3 cameraRotation = Vector3.zero;
    private float xRot=0f;

    void Start ()
    {
        motor = GetComponent<PlayerMoter> ();
        Cursor.lockState = CursorLockMode.Locked;
    }
    void FixedUpdate()
    {
        if (Input.GetKey (“escape”)) {
            Cursor.lockState = CursorLockMode.None;
        }
        //calc movement
        float xMovement = Input.GetAxisRaw(“Horizontal”);
        float zMovement = Input.GetAxisRaw(“Vertical”);

        Vector3 moveHorizontal = transform.right * xMovement;
        Vector3 moveVertical = transform.forward * zMovement;
        //finale movement
        Vector3 velocity = (moveHorizontal + moveVertical).normalized * speed;

        //moves
        motor.Move (velocity);

        //Calculate player rotation
        float yRot = Input.GetAxisRaw (“Mouse X”);
        Vector3 rotation = new Vector3 (0f, yRot, 0f) * sense;
        //rotates player
        motor.Rotate (rotation);
        //Calculate rotation
        xRot = Input.GetAxisRaw (“Mouse Y”);
        //stops rotaion
        cameraRotation.x = Mathf.Clamp(cameraRotation.x, -90f, 90f);
        cameraRotation = new Vector3 (xRot, 0f, 0f) * sense;
        //rotates
        motor.RotateCamera (-cameraRotation);
    }

}
  • Liked by
Reply
9 on January 30, 2018

Your code isn’t clamping the rotation because right after clamping you set the cameraRotation to a new Vector.
Try breaking the vector up into the 3 parts (x, y, z). Set all of them to be clamped (or not), and then set the rotation.

on January 30, 2018
on January 30, 2018

xRot  = Input.GetAxisRaw (“Mouse Y”);

rotX = xRot * sense;

rotX = Mathf.Clamp (rotX,-90,90);

cameraRotation = new Vector3 (rotX , 0f, 0f)

Tried this but still did nothing

 

on January 30, 2018

Can I see the entire script again with the new code?

 

on January 31, 2018
PlayerController:
 
using UnityEngine;

[RequireComponent(typeof(PlayerMoter))]
public class PlayerController : MonoBehaviour {

    [SerializeField]
    private float speed = 1000f;
    [SerializeField]
    private float sense = 3f;

    private PlayerMoter motor;
    private Vector3 cameraRotation = Vector3.zero;
    private float xRot=0f;
    private float rotX = 0f;

    void Start ()
    {
        motor = GetComponent<PlayerMoter> ();
        Cursor.lockState = CursorLockMode.Locked;
    }
    void FixedUpdate()
    {
        if (Input.GetKey (“escape”)) {
            Cursor.lockState = CursorLockMode.None;
        }
        //calc movement
        float xMovement = Input.GetAxisRaw(“Horizontal”);
        float zMovement = Input.GetAxisRaw(“Vertical”);

        Vector3 moveHorizontal = transform.right * xMovement;
        Vector3 moveVertical = transform.forward * zMovement;
        //finale movement
        Vector3 velocity = (moveHorizontal + moveVertical).normalized * speed;

        //moves
        motor.Move (velocity);

        //Calculate player rotation
        float yRot = Input.GetAxisRaw (“Mouse X”);
        Vector3 rotation = new Vector3 (0f, yRot, 0f) * sense;

        //rotates player
        motor.Rotate (rotation);

        //Calculate rotation
        xRot = Input.GetAxisRaw (“Mouse Y”);
        rotX = xRot * sense;
        rotX = Mathf.Clamp (rotX,-90,90);
        cameraRotation = new Vector3 (rotX, 0f, 0f);

        //rotates
        motor.RotateCamera (-cameraRotation);
    }

}
 
PlayerMotor:
 
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMoter : MonoBehaviour {

    [SerializeField]
    private Camera cam;

    private Vector3 velocity = Vector3.zero;
    private Rigidbody rb;
    private Vector3 rotation = Vector3.zero;
    private Vector3 cameraRotation = Vector3.zero;
    void Start ()
    {
        rb = GetComponent<Rigidbody> ();
    }

    public void Move (Vector3 _velocity)
    {
        velocity = _velocity;
    }
    public void Rotate (Vector3 _rotation)
    {
        rotation = _rotation;
    }
    public void RotateCamera (Vector3 _cameraRotation)
    {
        cameraRotation = _cameraRotation;
    }

    void FixedUpdate ()
    {
        PerformMovement ();
        PerformRotation ();
    }

    void PerformMovement()
    {
        if (velocity != Vector3.zero) {
            rb.MovePosition (transform.position + velocity*Time.fixedDeltaTime);
        }
    }

    void PerformRotation()
    {
        rb.MoveRotation (rb.rotation*Quaternion.Euler (rotation));
        if (cam != null) {
            cam.transform.Rotate (cameraRotation);
        }

    }

}
on January 31, 2018

Ah. I see. It’s a similar problem. You’re clamping the value correctly, but in the PlayerMotor, where the actual rotation is happening, there is no check. Try adding these lines just before cam.transform.Rotate in PlayerMotor

float xRot = cam.transform.localRotation.x + cameraRotation.x;
cameraRotation.x *= ( xRot > 90f || xRot < -90f ) ? 0f : 1f;

You can actually remove the clamps from PlayerController. All they’re doing is limiting your turning speed now.

 

edit: added code tags. Sorry, I did the original post from my phone.

on January 31, 2018

It still can over rotate

 

 

 

on January 31, 2018

If you change the 90f and -90f to 180f and -180f respectively, does that still restrict all rotation around the x axis?

 

on January 31, 2018

the reason it was restricting was because i accidentally removed

 xRot = Input.GetAxisRaw (“Mouse Y”);

 

but there is still over rotation even after i add it back

 

on February 1, 2018

You’re right. I made a mistake with the code snip I suggested. it should be

float xRot = cam.transform.localEulerAngles.x + cameraRotation.x;
cameraRotation.x *= ( xRot > 90f || xRot < -90f ) ? 0f : 1f;

localRotation is a quaternion, so localRotation.x would never be larger than 1 or less than 0. That’s why there was no change in behavior.

However this will still not act like you want.

I’m sorry, but at the moment I do not have a solution for you. I’ve gotten the camera to no rotate past 90 -90, but there is stuttering and sometimes the camera can still flip.

I can suggest that maybe you could try directly setting the localEulerAngles opposed to using the Rotate method.

Show more replies
  • Liked by
Reply
Cancel
0 on January 30, 2018

will do

 

  • Liked by
Reply
Cancel