Horizontal 2D Camera

Hello, I am using script for camera following from Unity Asset Store “Platformer”, but I have problem with it. I want the camera to move only horizontally. Without vertical motion during the jump. How can I do it? Here’s code:

using UnityEngine;
using System.Collections;

public class Camera2DFollow : MonoBehaviour {
	
	public Transform target;
	public float damping = 1;
	public float lookAheadFactor = 3;
	public float lookAheadReturnSpeed = 0.5f;
	public float lookAheadMoveThreshold = 0.1f;
	
	float offsetZ;
	Vector3 lastTargetPosition;
	Vector3 currentVelocity;
	Vector3 lookAheadPos;
	
	// Use this for initialization
	void Start () {
		lastTargetPosition = target.position;
		offsetZ = (transform.position - target.position).z;
		transform.parent = null;
	}
	
	// Update is called once per frame
	void Update () {
		
		// only update lookahead pos if accelerating or changed direction
		float xMoveDelta = (target.position - lastTargetPosition).x;

	    bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;

		if (updateLookAheadTarget) {
			lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
		} else {
			lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);	
		}
		
		Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
		Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
		
		transform.position = newPos;
		
		lastTargetPosition = target.position;		
	}
}

You don’t have to use that much of complicated script. I have used following code for satisfaction

public class CameraFollow : MonoBehaviour
{
	public Transform target;

	void Update ()
	{
		Vector3 position = transform.position;
		position.x = target.transform.position.x + 5f;
		position.y = target.transform.position.y;
		transform.position = position;
	}
}

Only this code work for my 2d game.

It’s working:

public class CameraFollow : MonoBehaviour
{
    public Transform target;
 
    void Update ()
    {
        Vector3 position = transform.position;
        position.x = target.transform.position.x + 5f;
        transform.position = position;
    }
}

Thanks!