Advice on implementing metroidvania camera

I’m working on putting together a simple metroidvania to try and keep my programming skills… well, not sharp, but not completely rusty. I have a pretty good camera script that centers on the player as long as they aren’t too close to the edge of the room.

It is, however, currently missing two crucial features that I have no idea how to implement. The first is that it needs the ability to move from room to room, preferably in a smooth animation that takes about 2 seconds.

The second is a bit more complex. I’d prefer it to stay slightly in front of the player so they can actually see where they’re going, since in a platformer, that’s kind of important. I tried setting up a node in front of the player and setting the camera to follow that, but that ended up causing problems with the boundaries and only working when the player was facing right. If possible, I’d prefer to handle this from within the camera script itself.

To elaborate on exactly what I’m going for, here’s my understanding of the way Super Metroid’s camera handles the X axis. The screen is 16 blocks wide, and assuming the camera is unconstrained by the walls, a running Samus ends up with 10 blocks in front of her and 6 blocks behind her. Additionally, when running towards the boundary of a room, the camera stops following Samus when she is 10 blocks from the edge. When running away from the boundary, the camera starts following her at 6 blocks from the edge. Should Samus change directions, it takes 5 blocks for the camera to travel the additional four blocks forward - in other words, it moves at a ratio of 1.8 to 1 until it reaches a maximum distance from Samus.
I don’t know of a way to measure movement along the Y axis, but I’d assume it works with similar numbers.

I’m working in C#, but if you know a solution that for some reason requires Javascript I shouldn’t have any trouble switching over what I already have.

The code so far:

using UnityEngine;
using System.Collections;

public class MetroCam : MonoBehaviour {

public Transform target;
public float smoothing = 8f;
public Collider boundaries;
public int screenHeight = 16;

float roomY;
float minX;
float maxX;
float minY;
float maxY;
int moveStart = 0;
float smooth;

Vector3 offset;
Vector3 targetCamPos;

void Start()
	// Set up camera - options for both orthographic and 2.5D
	camera.orthographicSize = (screenHeight / 2);
	camera.fieldOfView = 20;
	targetCamPos.z = -6 * camera.orthographicSize;
	smooth = smoothing;
	offset = transform.position - target.position;

	Bounds roomBounds = boundaries.bounds;

	float vertExtent = screenHeight / 2;
	float horzExtent = vertExtent * Screen.width / Screen.height;
	roomY = roomBounds.size.y;

	minX = roomBounds.min.x + horzExtent;
	maxX = roomBounds.max.x - horzExtent;
	minY = roomBounds.min.y + vertExtent;
	maxY = roomBounds.max.y - vertExtent;

void FixedUpdate()
	bool inXBounds = target.position.x >= minX && target.position.x <= maxX;
	bool inYBounds = target.position.y >= minY && target.position.y <= maxY;
	bool lockVert = roomY <= screenHeight;

	// X axis follow
	// TODO: Lock x axis for rooms that aren't as wide as the screen
		targetCamPos.x = target.position.x + offset.x;

	// Y axis follow
		targetCamPos.y = screenHeight / 2 + offset.y;
	else if(inYBounds)
		targetCamPos.y = target.position.y + offset.y;

	// Force camera into starting position
	if(!lockVert && moveStart == 0)
		smooth = Mathf.Infinity;
		targetCamPos.y = screenHeight / 2 + offset.y;
		moveStart = 1;

	transform.position = Vector3.Lerp (transform.position, targetCamPos, smooth * Time.deltaTime);
	if(smooth != smoothing)
		smooth = smoothing;


Have you tried this?