I’m designing a 2D top down game and, basically, I want a camera that follows the character when he is in the middle of the level, but as he approaches the edge of the level, the camera essentially collides with the edge of the level and stops following the character along that axis.
Here is an example of what I want from Zelda: Link to the Past: The Legend of Zelda: A Link to the Past 100% Walkthrough Part 1 - Zelda's Rescue - YouTube
Notice how the camera follows Link, but stops as he approaches the edges of the dungeon and the room exits.
I tried to get this working for about 2 or 3 hours last night, between working through code and browsing for youtube videos.
The closest I could find to what I want is in this video: Creating 2D Games in Unity 4.5 #15 - Camera Controller - YouTube
and here is the code:
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
public Transform target;
public Vector2
margin,
smoothing;
public BoxCollider2D bounds;
private Vector3
_min,
_max;
public bool isFollowing = true;
private Camera myCam;
public void cameraMovement(){
var x = transform.position.x;
var y = transform.position.y;
if(isFollowing){
if(Mathf.Abs(x - target.position.x) > margin.x){
x = Mathf.Lerp(x, target.position.x, smoothing.x * Time.deltaTime);
}
if(Mathf.Abs(y - target.position.y) > margin.y){
y = Mathf.Lerp(y, target.position.y, smoothing.y * Time.deltaTime);
}
}
var cameraHalfWidth = GetComponent<Camera>().orthographicSize * ((float)Screen.width / Screen.height);
x = Mathf.Clamp(x, _min.x + cameraHalfWidth, _max - cameraHalfWidth);
y = Mathf.Clamp(y, _min.y + myCam.orthographicSize, _max.y - myCam.orthographicSize);
transform.position = new Vector3(x, y, transform.position.z);
}
// Use this for initialization
void Start () {
_min = bounds.bounds.min;
_max = bounds.bounds.max;
myCam.orthographicSize = 4.5f;
}
// Update is called once per frame
void Update () {
cameraMovement();
}
}
The problem with all of the code and videos I have found so far is that the ones that successfully cause the camera to collide with the edge of the level also include a feature called “smoothing” where the camera continues moving for a few frames past the player when he stops, and then snaps back to the player or stays off center. I HATE this feature and do not want anything to do with it.
I’m hoping someone here knows how to script the camera to behave like it does in the Zelda video, or can point me down the right path to figure this problem out. Thanks a bunch for reading the wall of text, and let’s solve this!
EDIT: I solved this problem (awhile ago actually, forgot to update :P).
Here is the code:
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour {
public GameObject player;
public Transform highWall;
public Transform lowWall;
public Transform rightWall;
public Transform leftWall;
private float yMax;
private float yMin;
private float xMax;
private float xMin;
// Use this for initialization
void Start(){
}
// Update is called once per frame
void Update(){
yMax = highWall.transform.position.y;
yMin = lowWall.transform.position.y;
xMax = rightWall.transform.position.x;
xMin = leftWall.transform.position.x;
//if within the bounds, camera locks onto player
if(player.transform.position.y < yMax && player.transform.position.y > yMin){
transform.position = new Vector3(player.transform.position.x, player.transform.position.y, -110.0f);
}
//if player is above/below the y axis binders, camera locks to player on xAxis and stays stationary
//on yAxis
if(player.transform.position.y > yMax){
transform.position = new Vector3(player.transform.position.x, yMax, -110.0f);
}else if(player.transform.position.y < yMin){
transform.position = new Vector3(player.transform.position.x, yMin, -110.0f);
}
//if player is right/left of the xAxis binders, camera locks to player on yAxis and stays stationary
//on xAxis
if(player.transform.position.x > xMax){
transform.position = new Vector3(xMax, player.transform.position.y, -110.0f);
}else if(player.transform.position.x < xMin){
transform.position = new Vector3(xMin, player.transform.position.y, -110.0f);
}
//if player is above the yAxis binder, and to the right of the xAxis, the camera stays stationary
if(player.transform.position.y > yMax && player.transform.position.x > xMax){
transform.position = new Vector3(xMax, yMax, -110.0f);
}
//if player is above the yAxis binder, and to the left of the xAxis, the camera stays stationary
if(player.transform.position.y > yMax && player.transform.position.x < xMin){
transform.position = new Vector3(xMin, yMax, -110.0f);
}
//if player is below the yAxis binder, and to the right of the xAxis, the camera stays stationary
if(player.transform.position.y < yMin && player.transform.position.x > xMax){
transform.position = new Vector3(xMax, yMin, -110.0f);
}
//if player is below the yAxis binder, and to the left of the xAxis, the camera stays stationary
if(player.transform.position.y < yMin && player.transform.position.x < xMin){
transform.position = new Vector3(xMin, yMin, -110.0f);
}
}
}
I can answer any questions about the code if people ask, but it’s a fairly straightforward solution which attaches trigger colliders to the camera and if the character moves past the colliders, the camera stops moving on that axis.