2D Camera Follow · Unity

Unity 2D Camera Follow and Screen Clamping

Today we are going to look at setting up a camera follow script with screen clamping.

We want the camera to follow our character throughout the scene.

We also want to be able to control the camera so that it doesn’t go off or show an edge of the game that we do not desire it to.

I’ve went ahead and created a tile platform that is much wider than our camera view to demonstrate camera follow and the clamping.

Here is the script used in the above video.


using UnityEngine;

using System.Collections;

public class FollowTarget: MonoBehaviour

{

//what we are following

public Transform target;

//zeros out the velocity

Vector3 velocity = Vector3.zero;

//time to follow target

public float smoothTime = .15f;

//enable and set maximum Y value

public bool YMaxEnabled = false;

public float YMaxValue = 0f;

//enable and set min Y value

public bool YMinEnabled = false;

public float YMinValue = 0f;

//enable and set X max vlaue

public bool XMaxEnabled = false;

public float XMaxValue = 0f;

//enable and set X min value

public bool XMinEnabled = false;

public float XMinValue = 0f;

void FixedUpdate()

{

//target position

Vector3 targetPos = target.position;

//vertical

if (YMinEnabled && YMaxEnabled)

//for the targets Y position we'll clamp that position between the Y min and the Y max

targetPos.y = Mathf.Clamp(target.position.y, YMinValue, YMaxValue);

else if (YMinEnabled)

targetPos.y = Mathf.Clamp(target.position.y, YMinValue, target.position.y);

else if (YMaxEnabled)

targetPos.y = Mathf.Clamp(target.position.y, target.position.y, YMaxValue);

//horizontal

if (XMinEnabled && XMaxEnabled)

targetPos.x = Mathf.Clamp(target.position.x, XMinValue, XMaxValue);

else if (XMinEnabled)

targetPos.x = Mathf.Clamp(target.position.x, XMinValue, targetPos.x);

else if (XMaxEnabled)

targetPos.x = Mathf.Clamp(target.position.x, target.position.x, XMaxValue);

//align the camera and the targets z position

targetPos.z = transform.position.z;

//using smooth damp we will gradually change the camera transform position to the target

position based on the cameras transforms velocity and our smooth time value

transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime);

}

}

Advertisements

One thought on “Unity 2D Camera Follow and Screen Clamping

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s