Making camera follow upwards

How do I make the camera follow my character upwards at a certain height, I want the horizontal camera to move upwards after I reach a bit under the top like in New Super Mario Bros. I also need the camera to go back down after the character get under the red line in the photo. I am using unity 5.6.6

I want the camera to follow after the character reaches the red line

So this is my current code, I;m only missing what I mentioned

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

    public GameObject target;
    public float followAhead;

    private Vector3 targetPosition;

    public float smoothing;

	// Use this for initialization
	void Start () {
		
	}

    // Update is called once per frame
    void Update() {
        targetPosition = new Vector3(target.transform.position.x, transform.position.y, transform.position.z);

        //Makes camera move ahead of player
        if (target.transform.localScale.x > 0f)
        {
            targetPosition = new Vector3(targetPosition.x + followAhead, targetPosition.y, targetPosition.z);
        } else {
            targetPosition = new Vector3(targetPosition.x - followAhead, targetPosition.y, targetPosition.z);
        }

        //transform.position = targetPosition;

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

hi,
if you want camera follows player constantly you can see this link:

but in if you want to follow the player only up the red line you have to store the first y position and use that after player moves down.
I’v changed your code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMovement : MonoBehaviour
{
    public float redLineY;
    public float firstCameraY;
    public GameObject target;

    public float followAhead;

    private Vector3 targetPosition;

    public float smoothing;

    private Vector3 targetOffset = new Vector3(0,0,0);

    // Update is called once per frame
    void Update()
    {
        //Makes camera move ahead of player
        if (target.transform.position.y > redLineY)
        {
            targetPosition = new Vector3(target.transform.position.x, target.transform.position.y, transform.position.z) + targetOffset;//you can change the offset 
        }
        else
        {
            targetPosition = new Vector3(target.transform.position.x, firstCameraY, transform.position.z);
        }

        //transform.position = targetPosition;

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

Thank you so much that works it’s following up the way I want beautifully but it doesn’t follow ahead in front of the character so I added something to the code but it won’t follow ahead the other way when I turn around. Sorry I’m kinda new, also the camera is perfect for my game, I love it so don’t worry too much about helping me fix it again :smiley:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraController : MonoBehaviour
 {
     public float redLineY;
     public float firstCameraY;
     public GameObject target;
 
     public float followAhead;
 
     private Vector3 targetPosition;
 
     public float smoothing;
 
     private Vector3 targetOffset = new Vector3(0,0,0);
 
     // Update is called once per frame
     void Update()
     {
         //Makes camera move ahead of player
         if (target.transform.position.y > redLineY)
         {
             targetPosition = new Vector3(target.transform.position.x + followAhead, target.transform.position.y, transform.position.z) + targetOffset;//you can change the offset 
         }
         else
         {
             targetPosition = new Vector3(target.transform.position.x - followAhead, firstCameraY, transform.position.z);
         }
 
         //transform.position = targetPosition;
 
         transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime);
     }
 }

how do you make it is that it only follows it upwards?