# 2d camera follow

Hey so to begin let me explain what the camera will need to do.

My game is in 2d using c#

In my game the player will be able to move the usual left and right but the main movement will be down. I want the camera to follow the player allowing for a limited amount of movement before it follows.

Essentially when player moves to the camera should stay until it reaches an invisible border allowing small movements before it follows.

The main effect if for when player is falling it will keep track of the player but if player slows his fall rate you should see the player move upwards slightly.

I have tried various tutorials online and none seem to be able to do what I need. I not sure if I am just wording it badly so I can’t find the correct methods but I have got stuck

A method of doing this is creating a CameraFollow script. The script should have a way to calculate distance to an object on each axis and a method to smoothly move to new coordinates (both have a lot of tutorials).
Set the script up so that the camera is always a set number of units in the Z-plane from your player (assuming a standard 2d setup). When the distance from the camera in a direction hits a value (square root of the sum of the Z-offset squared and the distance before the camera moves squared) in the direction; call the smooth movement function repeatedly move to the player’s location, offset by the Z-offset; until the distance from the camera to the player is the Z-offset.

If that doesn’t make sense, let me know and I’ll reword it / write a sample script

This is what I use for my 2D camera. I actually put an empty game object on my player and tag it “CameraTarget”. (that way you can have the camera follow more above/below/to the side of the player as you want. If it doesn’t have the tag then it won’t work. You DO NOT child the camera to the player. You can play with the settings until you get something pretty close to what you want.

``````using UnityEngine;
using System.Collections;

{
public float xMargin = 1.0f;
public float yMargin = 1.0f;

public float xSmooth = 10.0f;
public float ySmooth = 10.0f;

public Vector2 maxXAndY;
public Vector2 minXAndY;

public Transform cameraTarget;

// Use this for initialization
void Awake ()
{
cameraTarget = GameObject.FindGameObjectWithTag("CameraTarget").transform;
}

bool CheckXMargin()
{
return Mathf.Abs (transform.position.x - cameraTarget.position.x) > xMargin;
}

bool CheckYMargin()
{
return Mathf.Abs (transform.position.y - cameraTarget.position.y) > yMargin;
}

// Update is called once per frame
void FixedUpdate ()
{
TrackPlayer();
}

void TrackPlayer()
{
float targetX = transform.position.x;
float targetY = transform.position.y;

if(CheckXMargin())
{
targetX = Mathf.Lerp (transform.position.x, cameraTarget.position.x, xSmooth * Time.deltaTime);
}

if(CheckYMargin())
{
targetY = Mathf.Lerp (transform.position.y, cameraTarget.position.y, ySmooth * Time.deltaTime);
}

targetX = Mathf.Clamp (targetX, minXAndY.x, maxXAndY.x);
targetY = Mathf.Clamp (targetY, minXAndY.y, maxXAndY.y);

transform.position = new Vector3(targetX, targetY, transform.position.z);
}
}
``````

Take a look at this tutorial. I think this is pretty much what you want.

So summarize what he did, you basically create an area that the player can run around without moving the camera. But when the player reaches the edge of said area you start centering the camera on the player.