Dampened camera with constraints

This has probably already been posted but I’ve spent some time searching and elaborating so I thought I’d post the question anyway.

The setup is simple, only Y and X dimensions are used in the game. Player is an airplane chasing another airplane and the camera sees this from the side. A normal platformer.

What I want to do is make the camera go up and down depending on if the player moves up and down. I also want to give the camera a height constraint which makes it not move above or below that point. To the player it should feel as if the camera is struggling more and more, slowing down more and more when it starts to reach the constraint points. I don’t know what to call it but I’m sure you’ve all played a game that implements this.

What I can’t figure out is how to apply this constraint. I can’t just make the camera follow the player since he would then be in the center of the picture all the time and not get the signal that he’s going too far when he’s slowly disappearing out of picture.

What I’m thinking is applying some kind of dampening formula but I can’t come up with a good solution.

Any ideas or suggestions?

Thanks for reading!

You should look at the Lerp function, it does pretty much what you want. There is a feature that will stop it ever getting to the final constraint. (ease-out)

There should be plenty of code examples for smoothcamera/smoothfollow.

Thanks for the reply.

Maybe I reinvented the wheel, but here’s what I came up with:

using UnityEngine;
using System.Collections;

public class CameraSmoothFollow : MonoBehaviour
{
    //Follows the player smoothly a bit.
    //Hits a threshold and then dampens the movement until it no longer moves

    //Use the distance from the current point to the constraint point as a percentage modifier to the movement.
    float distance;
    float constraint;
    

    public float maxY = 12;
    public float originPoint = 0;
    public float minY = -12;
    public float damping = 0.5f;
    //Get script
    //Not used in the current solution
    InterpolateTowardsPoint interpolate;

    //Target
    public GameObject target;

    //Old transform
    private Vector3 oldTransform;

    void Start()
    {
        GameObject utilityObject = GameObject.Find("UtilityObject");
        interpolate = utilityObject.GetComponent(typeof(InterpolateTowardsPoint)) as InterpolateTowardsPoint;
        oldTransform = transform.position;
    }
    void Update()
    {

        if (target != null)
	    {
            //Determine if it's going up or down.
            if (target.transform.position.y > oldTransform.y)
            {
                //we're going upwards
                //check distance to constraint
                distance = maxY - transform.position.y;
                //calculate constraint
                constraint = (distance / maxY);
                //Debug.Log("Constraint: " + constraint + "Distance: " + distance);
                //check targets position
                Vector3 targetPosition = new Vector3(oldTransform.x,(target.transform.position.y * constraint),oldTransform.z);
                
                //Lerp towards that position
                transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * damping);

            }
            if (target.transform.position.y < oldTransform.y)
            {
                //player going down
                //Check distance
                distance = minY - transform.position.y;
                //calculate constraint
                constraint = (distance / minY);
                //check target position
                Vector3 targetPosition = new Vector3(oldTransform.x, (target.transform.position.y * constraint), oldTransform.z);

                //Lerp
                transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * damping);
            }
	    }

    }
}

It works as it should but it ain’t pretty. Any suggestions on how to make it better?

Thanks for reading