- Home /
How to make sliding room transitions like in Mega Man?
Hey everyone, so in Mega Man 2, when the player moves between rooms, the camera smoothly transitions to the view of the next room while the player freezes and moves ever so slightly into the next room (reference: https://www.youtube.com/watch?v=B5fyahaKCm4). I have the camera for the game already working, where there is a collider covering each room and if the player moves to another room the camera snaps to be viewing that room. I think there is something called Lerp that I could use but I wouldn't know how to use it. Any help would be appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MegaManCamera : MonoBehaviour
{
[Header("Set in Inspector")]
public Transform target;
public GameObject camBoundsParent;
[Tooltip("The distance from the center of the camera to the side of the frame.")]
public float camHalfW = 8;
[Tooltip("The distance from the center of the camera to the top or bottom of the frame.")]
public float camHalfH = 7.5f;
public float camOffsetZ = -10;
[Tooltip("Must be between 0 & 1. Determines size of area in middle of screen that will not force scroll of camera.")]
public float deadZoneSize = 0.5f; // What border area must the target enter to force a scroll of the camera
[Header("Set Dynamically")]
public Collider currColld;
private Collider[] camBounds;
// Use this for initialization
void Awake()
{
// Get all of the camBounds colliders
camBounds = camBoundsParent.GetComponentsInChildren<Collider>();
currColld = null;
}
void FixedUpdate()
{
// Get the position of the target
Vector3 tPos = target.position;
// Determine whether the target is no longer still within the same Collider
if (currColld == null || !currColld.bounds.Contains(tPos))
{
// Find the Collider that the target IS within
currColld = null;
foreach (Collider colld in camBounds)
{
if (colld.bounds.Contains(tPos))
{
// If we find a collider, then set currColld and break out of the foreach loop
currColld = colld;
break;
}
}
}
// First, align the camera with the target (with a Z offset, of course)
tPos.z += camOffsetZ;
// Make sure that the Camera only moves if the target is outside the middle of the screen
Vector3 cPos = transform.position;
float deadZoneX = camHalfW * deadZoneSize;
float deadZoneY = camHalfH * deadZoneSize;
if (Mathf.Abs(tPos.x - cPos.x) > deadZoneX)
{
if (tPos.x > cPos.x)
{
cPos.x = tPos.x - deadZoneX;
}
else
{
cPos.x = tPos.x + deadZoneX;
}
}
if (Mathf.Abs(tPos.y - cPos.y) > deadZoneY)
{
if (tPos.y > cPos.y)
{
cPos.y = tPos.y - deadZoneY;
}
else
{
cPos.y = tPos.y + deadZoneY;
}
}
// if the currColld is not null, limit movement to within the Collider
if (currColld != null)
{
// Account for camW and camH when judging the size of the bounds
Vector3 min = currColld.bounds.min;
Vector3 max = currColld.bounds.max;
min.x += camHalfW;
min.y += camHalfH;
max.x -= camHalfW;
max.y -= camHalfH;
cPos.x = Mathf.Clamp(cPos.x, min.x, max.x);
cPos.y = Mathf.Clamp(cPos.y, min.y, max.y);
}
transform.position = cPos;
}
}
you have not follow up on your previous questions or marked any correct. If users take the time to help you. you should take the time to follow up.
I did leave a comment on the other post of $$anonymous$$e that you responded to and I didn't accept the answer because it didn't work for my situation. Thanks for responding though.
Answer by highpockets · Apr 02, 2019 at 06:20 PM
Lerp can do that indeed. A linear interpolation is exactly what you need:
Vector3 camStartPos;
Vector3 camEndPos;
float lerpTime = 0.0f;
bool triggerTransition = false;
void LateUpdate(){
if(triggerTransition){
Camera.main.transform.position = Vector3.Lerp(camStartPos, camEndPos, lerpTime);
if(lerpTime >= 1.0f){
triggerTransition = false;
}else{
lerpTime = lerpTime + Time.deltaTime;
}
}
}
void OnTriggerEnter(Collider other){
if(other.gameObject.tag == “Player”){
triggerTransition = true;
camStartPos = Camera.main.transform.position;
lerpTime = 0.0f;
}
}
Didn’t test it, but should do the trick.