2.5D Phase1 Framework: Moving Platforms

Gabriel Perez
2 min readJan 1, 2022

With having an elevator system going, I can reuse the logic for Moving Platforms!

2.5D Phase 1 Framework Challenge

As before, with my Elevator System challenge, I want the moving platform to move from point A to B non-stop.

I pretty much need the same logic behind the Elevator script. To keep it Modularized and not have duplicate code, I want to add an enum to state what kind of moving platform it is.

What I’ll Need?

In the elevator script, I want to add an enum to state what type of moving platform it is. I will use a switch statement to determine the type in Update.

Getting Started

I renamed the Elevator Script to “MovingPlatform.” I also added an enum above the class name.

public enum MovingPlatformType { None, Elevator, Platform }

I also want to serialize the field, initialize, and declare the enum in the MovingPlatform class so that I can have access to it in the inspector. It will allow me to select what type of platform it is.

[SerializeField] private MovingPlatformType _type;

For the switch statement, I added:

switch (_type)
{
case MovingPlatformType.Elevator:
if (transform.position == _pointA.position)
StartCoroutine(WaitBeforeSwitchingRoutine(true));
else if (transform.position == _pointB.position)
StartCoroutine(WaitBeforeSwitchingRoutine(false));
break;
case MovingPlatformType.Platform:
if (transform.position == _pointA.position)
_switch = true;
else if (transform.position == _pointB.position)
_switch = false;
break;
default:
print(“Please Select a type for the moving platform!”);
break;
}

If it’s an elevator, I want there to be a wait time before moving to the next point.

If it’s a regular moving platform, I want it to switch to the next point immediately.

And by default, I want Unity to send me a message via console that I forgot to select a type!

Now that I have modularized the script, I can set the type for the elevator prefab to type elevator and the moving platform prefab to type platform. And it’s working as intended!

Gabriel

--

--

Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.