# Camera Border Follow

Hi all,

I am working on a orthographic camera and am trying to make it like the original Super Mario Brothers camera.

So far i have done the following:

Stopped the camera from moving up when Mario jumps, set the borders for the x axis so that the camera will stop scrolling when it reaches the left or right edge of the map and the camera also follows Mario around on the x axis left and right.

My problem is i don’t want the camera to scroll to the left, what i need is for the edge border that i have set for the left hand side of the map to move forward when Mario moves to the right and then when Mario moves back to the left i need the camera to stop moving.

Here is my code thus far.

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

public class CameraScript : MonoBehaviour {

//Object to look at and follow.
public Transform target;

//Border for width.
public float borderX = 1.6F;

//Border for height.
public float borderY = 0.11F;

//Stops camera from moving past the border to the left.
public float minimum = 0.0F;

//Stops camera from moving past the border to the right.
public float maximum = 0.0F;

//Time for camera dampen.
float  smoothTime = 0.2F;

//Mario's transform.
private Transform marioTransform ;

private Vector2 velocity;

public bool useSmoothing = false;

void Start()
{
marioTransform = transform;
}

void Update()
{
Vector2 newPos2D = Vector2.zero;

if (useSmoothing)
{
newPos2D.x = Mathf.SmoothDamp(marioTransform.position.x, target.position.x + borderX, ref velocity.x, smoothTime);
}

Vector3 newPos = new Vector3(newPos2D.x, newPos2D.y, transform.position.z);

transform.position = Vector3.Slerp(transform.position, newPos, Time.time);

//Set the minimum and maximum vales for the camera movement on the x axis.
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minimum, maximum), transform.position.y, transform.position.z);
}

public void LateUpdate()
{
//Stop camera from moving in the y axis when mario jumps.
Camera.main.transform.position = new Vector3(marioTransform.position.x, borderY, transform.position.z);
}

}
``````

The minimum value is for the border to the left and the maximum value is for the border to the right.

I need the minimum value to increment upwards when Mario moves to the right and when Mario moves to the left i need the incrementation to stop and the camera to remain where it is until Mario moves to the right again.

Thanks for reading and any help would be appreciated.

Here is my camera script which does similar operations:

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace namespaace{
class EndlessLevelCameraController : MonoBehaviour {
//public float speed = 2f;
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
public Transform rightEndMarker = null;
public Transform leftEndMarker = null;
public Transform topEndMarker = null;
public Transform bottomEndMarker = null;
public bool useFixedUpdate = false;
public bool pinCamera = false;

private float leftWallXWorld = 0;
private float leftWallXScreen = 0;
private float rightWallXWorld = 0;
private float rightWallXScreen = 0;
private float topWallYWorld = 0;
private float topWallYScreen = 0;
private float bottomWallYWorld = 0;
private float bottomWallYScreen = 0;

void Start() {

}
void updateWalls() {

leftWallXWorld = leftEndMarker.position.x;
leftWallXScreen = camera.WorldToScreenPoint(leftEndMarker.position).x;

rightWallXWorld = rightEndMarker.position.x - Screen.width;
rightWallXScreen = camera.WorldToScreenPoint(rightEndMarker.position).x - Screen.width;

topWallYWorld = topEndMarker.position.y - Screen.height;
topWallYScreen = camera.WorldToScreenPoint(topEndMarker.position).y - Screen.height;

bottomWallYWorld = bottomEndMarker.position.y;
bottomWallYScreen = camera.WorldToScreenPoint(bottomEndMarker.position).y;
}

void LateUpdate() {
if (!useFixedUpdate)
updateCameraPosition();
}

void FixedUpdate() {
if (useFixedUpdate)
updateCameraPosition();
}
// Update is called once per frame
void updateCameraPosition() {
updateWalls();
if (PreferencesManager.CURRENT_PLAYER.transform) {

if (!pinCamera) {
Vector3 delta = PreferencesManager.CURRENT_PLAYER.transform.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = (transform.position + delta);
//Vector3 destinationWorld = camera.ScreenToWorldPoint(destination);
if (destination.x > rightWallXScreen) {
destination = new Vector3(rightWallXScreen, destination.y, 0);
}
if (destination.x < leftWallXScreen) {
destination = new Vector3(leftWallXScreen, destination.y, 0);
}
if (destination.y > topWallYScreen) {
destination = new Vector3(destination.x, topWallYScreen, 0);
}
if (destination.y < bottomWallYScreen) {
destination = new Vector3(destination.x, bottomWallYScreen, 0);
}
// here to always set the Z
destination = new Vector3(destination.x, destination.y, transform.position.z);

transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}

//Logger.logTest("step1");
float playerX = PreferencesManager.CURRENT_PLAYER.transform.position.x;
float playerY = PreferencesManager.CURRENT_PLAYER.transform.position.y;
if ((Math.Abs(playerX - leftEndMarker.position.x) < .5 && !PreferencesManager.getPlayerController().MOVING_RIGHT) ||
(Math.Abs(playerX - rightEndMarker.position.x) < .5 && PreferencesManager.getPlayerController().MOVING_RIGHT) ||
(Math.Abs(playerY - topEndMarker.position.y) < .6 && PreferencesManager.getPlayerController().MOVING_UP)) {
// || (Math.Abs(playerY - bottomEndMarker.position.x) < .5 && !PreferencesManager.getPlayerController().MOVING_UP)) {

PreferencesManager.getPlayerController().MOVE_ALLOWED = false;
//Logger.logTest("false");
} else {
PreferencesManager.getPlayerController().MOVE_ALLOWED = true;
//Logger.logTest("true");
}

}
}
}
}
``````