Classic Platforming Camera

Hi Unity Community! I’m developing a classic style 3D platformer, but I’d like to add a detail to the camera system. In most 3D platformers, when the player moves or jumps/double jumps on their own, the camera’s height position stays stationary. Lets say the player bounces on a mushroom and kind of goes off the screen limits. Then, the camera will follow the height of the player until he is grounded or close to the ground.

Main Camera.cs

void LateUpdate () {
        if (target) {
            x += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;

            xSmooth = Mathf.SmoothDamp (xSmooth, x, ref xVelocity, smoothTime);
            ySmooth = Mathf.SmoothDamp (ySmooth, y, ref yVelocity, smoothTime);

            ySmooth = ClampAngle (ySmooth, yMinLimit, yMaxLimit);

            Quaternion rotation = Quaternion.Euler (ySmooth, xSmooth, 0);

            // posSmooth = Vector3.SmoothDamp (posSmooth, target.position, ref posVelocity, smoothTime);
            posSmooth = Vector3.SmoothDamp (posSmooth, new Vector3(target.position.x, yPos ,target.position.z), ref posVelocity, smoothTime);
            transform.rotation = rotation;
            transform.position = rotation * new Vector3 (xOffset, yOffset, -distance) + posSmooth;


            RaycastHit hit;
            if (Physics.Linecast (target.position, transform.position, out hit, layermask)) {
                float tempDistance = Vector3.Distance(target.position,hit.point);
                Vector3 position = rotation * new Vector3(xOffset, yOffset, -tempDistance) + posSmooth;
                transform.position = position;
            }
        } 
    }

What I have here is a segment of the camera controls that orbits around the player and follows it smoothly. I commented out the line that follows the player and the height even when he jumps. But below that I have the new line of code that follows the target’s X and Z position, and sets its Y as 3.

How can I manipulate the Y value in such a way that when the player is high enough from ANY surface below him, that the Y smoothly transitions from 3 to the desired target.position.y for example?

Thanks for any help! :slight_smile:

If all your heights and drops will always be within the view of the camera, one simple way is to allow the camera change of height to only happen when the player touches a surface which can be stepped on.

I assume you’re talking about the camera they used in the Mario? This is a bit more difficult in 3D than it is in 2D because you can’t just use the screen resolution to calculate when the camera should “unlock” from the fixed Y position. However, with that being said, you can accomplish the task by calculating the height of the screen in world space with Camera.ScreenPointToRay and Ray.GetPoint(Z distance from camera to player). Once you know the height of the screen in world space you can simply check if the player’s bounding box is going to exceed that height and “unlock” your camera’s Y position.