Constraining Camera Angle when Looking At Character

This is the same issue I have been having and been trying unsuccessfully to solve on and off for quite a long time, and I am honestly quite annoyed that such a simple problem does not seem to have any simple solution. I am trying to make a third-person camera that orbits around the player character with the mouse, but that does not go any farther than straight up or straight down (ignoring cameras-in-walls issues for now). Fairly standard camera setup IMO, if you would like an example of what this would ideally look like, the first game that comes to my mind is Warframe.

Here is my code for the camera setup (Both scripts on the Camera object itself):

InputManager.cs

void Update () {
//Allows the camera to follow the player as they move
    Vector3 curTargetPosition = target.position;
    Vector3 moveDelta = curTargetPosition - oldTargetPosition;
    this.transform.position += moveDelta;
//Do the rotation calculations
    cam.RotVertical(Input.GetAxis("Mouse Y"));
    cam.RotHorizontal(Input.GetAxis("Mouse X"));
//Update the old position
    oldTargetPosition = curTargetPosition;
}

CameraOrbit.cs

public void RotHorizontal (float axis) {
        transform.RotateAround(target.position, Vector3.up, Time.deltaTime * axis * hspeed);
    }

    public void RotVertical (float axis) {
        transform.RotateAround(target.position, transform.TransformDirection(Vector3.left), Time.deltaTime * axis * vspeed);
    }

target is the player object, cam is a CameraOrbit component, and hspeed, vspeed, and oldTargetPosition are initialized as 100, 100, and the starting player position.

I have tried to use eulerAngles, localEulerAngles, Mathf.Clamp, putting the constraining in the RotVertical/Horizontal methods, Update, and LateUpdate, and every other solution I have seen suggested but none of them work. I think this may be primarily due to the fact that the values from the X rotation in eulerAngles (this is the rotation that I want to constrain) do not seem to be what they “should” be. This “makes sense” to me in that this most likely occurs because Unity is translating Quaternions to Euler and this doesn’t always produce results that make sense to our feeble human minds.

This code functions normally right now, it just makes the camera be upside down on the character if you go to far. I think this is all the code you would need to reproduce what I have (everything else is just initialization) but if you need more code to figure anything out, I can provide it.

What I would like to know is, how can I have the camera able to orbit around the character and be able to constrain it properly so it does not go above and then past the character to the point where the camera is upside down W.R.T the player orientation. If you can think of a solution that uses Euler angles that works for me, if not, I have a fairly decent understanding of Quaternions so I would be more than happy to implement it that way as well.

Thanks in advance to anyone who can provide insight.

There’s an example of this in the standard assets. I forget the exact name. The camera prefab comes with 3 parts (parent + 2 children),I remember. The rotation (and clamping parts) are in a script, whose name I also forget, sorry.
But you could take a look at the standard assets; can’t be that many in that section to check out.

Really? I can’t believe I’ve never seen that mentioned before. Ill take a look when I get home from work today. Thanks a ton

Cool. When I first started, the camera rotating around was a struggle for me, as well. With the prefab and the script, I got it all working, and over time did some modifications of my own. Hope it works out well for ya :slight_smile: