How can you move the camera one way along the Y axis but not the other?

I need the camera in my game to follow my player up along the Y axis but not follow the player when they move down the Y axis.

This is my current camera script:

    [SerializeField]
    private float xMax;

    [SerializeField]
    private float yMax;

    [SerializeField]
    private float xMin;

    [SerializeField]
    private float yMin;

    private Transform target;


    void Start ()
    {
        target = GameObject.Find ("Player").transform;
    }
   

    void LateUpdate ()
    {
        transform.position = new Vector3(Mathf.Clamp (target.position.x,xMin,xMax), Mathf.Clamp(target.position.y,yMin,yMax), transform.position.z);                                 
    }

Any help would be appreciated.

You mean the camera is only allowed to go up?

Indeed :slight_smile:

You could set yMin to transform.position.y every frame for example :slight_smile:

Or, in the y component, you can do

Mathf.Max(
    Mathf.Clamp(target.position.y,yMin,yMax), 
    transform.position.y
)

instead, which will be similar to doing this:

var wantedX = ...;
var wantedY = Mathf.Clamp(target.position.y,yMin,yMax);
var wantedZ = ...;
if(wantedY < transform.position.y) {
    // prevent wantedY from being less than transform.position.y
    wantedY = transform.position.y;
}
transform.position = new Vector3(wantedX, wantedY, wantedZ);
1 Like

Create a variable to store the maximum height reached for the player/camera.

float maxHeight;//maximum height reached

Now in late update save the highest value for y

float currentHeight = target.position.y;
if (currentHeight > maxHeight){
   maxHeight = currentHeight;
}

Then use that value for the y axis of the new camera position

Mathf.Clamp (target.position.y,maxHeight,yMax);
2 Likes

Thankyou very much <3