3rd Person Camera Controller [Global and Local Position problem]

Hey there,

i would love my Camera to follow my Player even hes rotating etc … ,

thats what i would do via. global position,

then if i move the mouse, it should clamp the camera at the x/y coordinates “local position” at +66 / -66 degrees,

at the moment it work but not all,

if i follow my followObject via. script , it doesnt let me do my local movement , i hope you can follow me guys,
if not, feel free to write what you dont get im not native english speaker :smile:

Code:

    void Update () {

        //GETS CONTROLLER INPUT
        float inputX = Input.GetAxis ("RightStickHorizontal");
        float inputZ = Input.GetAxis ("RightStickVertical");

        //MOUSE INPUT
        mouseX = Input.GetAxis ("Mouse X");
        mouseY = Input.GetAxis ("Mouse Y");

        //CONTROLLER + MOUSE INPUT (IN CASE SOMEONE USES CONTROLLER)
        finalInputX = inputX + mouseX;
        finalInputZ = inputZ + mouseY;

        //GETS THE Y AND X ROTATION TIMES THE SENSIVITY AND THE TIME(so it look good)
        rotY += finalInputX * inputSensitivity * Time.deltaTime;
        rotX += finalInputZ * inputSensitivity * Time.deltaTime;

        //RETURNS X (Clamping Works also while turning)
        rotX = Mathf.Clamp (rotX, -clampAngle, clampAngle);

        //NOT WORKING
        rotY = ClampAngle(rotY, -(CameraFollowObj.transform.position.y + clampAngle), CameraFollowObj.transform.position.y + clampAngle);

        //Vector3 eulerRotation = new Vector3(transform.eulerAngles.x, CameraFollowObj.transform.eulerAngles.y, transform.eulerAngles.z);

        Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
        transform.rotation = localRotation;
    }

    void LateUpdate () {
        CameraUpdater ();
    }

    void CameraUpdater() {
        // set the target object to follow
        Transform target = CameraFollowObj.transform;

        //move towards the game object that is the target
        float step = CameraMoveSpeed * Time.deltaTime;
        transform.position = Vector3.MoveTowards (transform.position, target.position, step);

        //Vector3 eulerRotation = new Vector3(transform.eulerAngles.x, CameraFollowObj.transform.eulerAngles.y, transform.eulerAngles.z);
        Vector3 eulerRotation = new Vector3(transform.eulerAngles.x, CameraFollowObj.transform.eulerAngles.y, transform.eulerAngles.z);
        transform.rotation = Quaternion.Euler(0 , eulerRotation.y, 0);
      
    }

    public float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F) { angle += 360F; }
        if (angle > 360F) { angle -= 360F; }
        return Mathf.Clamp(angle, min, max);
    }

if i remove

        Vector3 eulerRotation = new Vector3(transform.eulerAngles.x, CameraFollowObj.transform.eulerAngles.y, transform.eulerAngles.z);
        transform.rotation = Quaternion.Euler(0 , eulerRotation.y, 0);

then it does work BUT only on “global axis” … and my camera doesnt follow my character anymore

I’m not entirely sure what you mean with global vs local rotation, but from what i understand you want a rather normal character controller which also gets input from the camera rotation (as in walking in the direction you are looking). If so, then first and foremost i’d highly recomment watching the youtube tutorial series by Sebastian Lague on the topic, as he explaine pretty well what part of the code is responsible for what, and you should be able to go from there if you need to add anything (you may want to watch the previous episode about character movement as well):

Hope this helps!

1 Like

Thanks!

Thats what i have archieved at the moment by myself but i would like to “LIMIT” the rotation of the camera,

that the camera can only rotate at the back of the character, not all 360 degrees around the character, only 180,

imagine, the camera is behind the player, i would like to limit the rotation to 90 degrees to the left and 90 degrees to the right but i cant work it out x.x

And to add:

If i move and rotate with my player controller,
the camera doesnt rotate with me , it just looks at the character :S

I’m a bit short on time right now. Just wanted to quickly note that:

  • the character controller + third person camera he build do rotate the character when walking (dont when standing), so take a closer look at what he did and copy that. Iirc you reference the camera and add its rotation to your target rotation or something like that
  • he actually implemented a minMaxPitch in the video. It’s basically just clamping the value. If you need that on other axis as well, just do it like he did it for pitch. Like that you can then limit the camera to not rotate fully around the character.

Hope this helps.

1 Like

Thank you for helping me out!

The fix was to

    1. Make the Global Rotation of the Camera Rotate with the Player (so if i rotate with “W,A,S,D” the Camera Rotates with the Player)
    1. Make the Mouse Restriction depending on the Local rotation of the camerafollowobj.

at the end thank you very much!! :slight_smile: