Flash and Unity development

Simple One-Way Platforms in Unity 2D

Posted by robotheadgames on August 26, 2014

That’s right… 2D, not 3D…

I’ve been recently revisiting my mouse (the rodent, not the controller) platformer and I wanted to try out Unity’s relatively new 2D tools and see how they compared to Flash.

Note: I still like Flash, I’m quite comfortable with it as a 2D game dev tool and with AS3 but Unity has grown on me for game development.

Anyway, working from the 2D example project, all was well, except for one thing. One-way platforms. Again, you don’t get them out of the box. In 3D, as noted in the earlier post, this is easy enough to do by using a plane as a mesh collider. Unity could have made this easy in 2D by including the option of a one-way edge collider but they didn’t, edge colliders are two-way only. It seems to be a major oversight as one-way platforms are a major part of many old school 2D games. And they are an essential part of my mouse game. The entire game feels different without them.

There seems to be a good bit of demand for this feature and I found a number of solutions online. One, they have implemented a Physics2D.IgnoreCollision function in Unity 4.5 which seems to be most helpful. However, the simplest solution seems to be this one and it even works in Unity 4.3:

By Sudhir Attri. (Thanks dude!) Basically you make an empty GameObject as a child of your platform, give it a box collider that sits right below your platform and make it a trigger that turns the platform’s collider on and off. (Dead simple with their script.) That way your character can jump up through the platform and land on top of it. Success!

Except there is a problem. This only works once per game. Let’s say my mouse falls down from a higher platform and has to go back up. But he can’t, as the one-way platforms he’s passed through are no longer one-way and are blocking his way. This will not do.

Here is the solution I came up with. It’s simple: 

Basically you want a large trigger that turns its parent platform one-way every time your character walks below the platform. So do everything as before. I’ve called the trigger object platformTrigger, here it is in the hierarchy:

Trigger Hierarchy

Resize and position the trigger’s BoxCollider like this, so it overlaps the bottom of the platform a bit (more on this later) and hangs down far enough that the character will walk into it when walking under the platform:

Trigger BoxCollider placement

Still using the example’s game assets.

Set the trigger’s materials, trigger and script properties to this in the Inspector:

(Platform being its parent platform.) Updated 8/29 with correct settings. Check Oneway OFF.

(Platform being its parent platform.)
Updated 8/29 with correct settings. Check Oneway OFF.

Finally, use this script (C#, JavaScript follows):

using UnityEngine;
using System.Collections;

public class PlatformTrigger : MonoBehaviour {
public BoxCollider2D platform;
public bool oneWay = false;

// Use this for initialization
void Start () {


// Update is called once per frame
void Update ()
platform.enabled = !oneWay;

void OnTriggerEnter2D(Collider2D other)
oneWay = true;

void OnTriggerStay2D(Collider2D other)
oneWay = true;

void OnTriggerExit2D(Collider2D other)
oneWay = false;

Now in JavaScript…

#pragma strict
var platform : BoxCollider2D;
var oneway : boolean;
function Start () {

function Update () {
if (oneway)

if (!oneway)

function OnTriggerEnter2D(other: Collider2D) {
oneway = true;

function OnTriggerStay2D(other: Collider2D) {
oneway = true;

function OnTriggerExit2D(other: Collider2D) {
oneway = false;

That’s it!

*The reason for placing the BoxCollider this way… If it’s too high, the character will pass through the platform from above if he comes at an angle to the sides. This won’t do. If the BoxCollider is too low, he will noticeably slow down as he comes up through the platform and won’t jump high over it. That *could* be OK, as it gives him the effect of “snapping to” the platform’s surface and you may want that. I don’t want it for my game. So the way I did it, there may be some barely (or not even) noticeable slowdown (which is OK) but he jumps just as high.

There will be more on the unnamed mouse game as it progresses. My main project is the beard brawler and I hope to post some news on it when ready.


2 Responses to “Simple One-Way Platforms in Unity 2D”

  1. […] Simple One-Way Platforms in Unity 2D […]

  2. Cool post, but it could work better if you just use Physics2D.IgnoreCollision with the Player instead of disable the whole platform, for other Character (such as Enemy) might be walking on top of it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: