Mobile Game Progression
Creating One way Platform
One of the more unique functions that 2D games have are One way platforms, where the player is able to jump through a designated platform with a collider from the bottom and still activated the collider once on top of the platform. This function is useful when working with scene that clustered together.
How to Setup a One Way Platform:
First we are going to create a designated object to be our platform. In my case I am going to be using and Edge Collider since I want the platform to be invisible, but this ability can also be applied to Box Collider or any other collider.
Next we are going to add a Platform Effector 2D component to our platform object. Make sure to check on “Use One way”, and in the Collider 2D component also have the “Used By Effector” checkbox turn on. Automatically you will see a dome shape object within your platform, this is an indication that the platform is now ready.
In fact once that dome shape appears the platform is already fully functioning and you can test it out in game. If your player isn’t landing on the platform correctly, it could mean that the Tag or Layer option isn’t set to the “Ground” Layer.
But we are going to go one step further and script in a function for our player to jump down of the one way platform.
Scripting the Player to Jump Down from Platform:
There are various methods in making the player jump down from the platform and my method isn’t the only way. What I am going to do is basically turn off the platform edge collider to let the player fall through once a certain set of conditions has been met.
In the Player script, we are going to create two new variables, a Box Collider for the player and a private game object to identify the current platform.
Next we are going to create two new On Collision Enter and Exit methods. On the Enter method we are going to do an compare tag function which only checks if the Player has collided with the One Way Platform object and if the player does, we are going to assign the current platform as the “Current One Way Platform” game object. When the player exits the platform, we are going to set the “current platform” to be null.
Finally we are going to set up a Coroutine which turns on and off the Edge Collider of the platform. We will give ourselves a timer of 2.5 seconds before the Edge collider turns back on.
To Finish off the entire function, we are going to trigger the Coroutine whenever we press a certain button while our player is on top of the platform.
Now whenever our player is on top of the platform and we press the conditional button, our player will drop.