Simple one-way platform in Unity

This is a simple trick to create a Mario-style platform where it’s open when you jump from below but you will collide with it when you’re above the platform. The good thing about this solution is that it’s very simple and clear what it’s doing, and takes like 10 seconds to set up. It is not perfect and if your game relies a lot on this type of platforms I would suggest a more advanced solution. Something where you take into account what direction is the character moving towards etc.

  1. Create the platform.
  2. Duplicate the platform and child this one to the platform. Call the child AboveTrigger.
  3. On AboveTrigger, make sure that under the Collider “Is Trigger” is set to false.
  4. Move the AboveTrigger up about the height of the platform and a little extra room so they’re not touching (or down if you want a top-open block). When the player is touching this trigger, the platform will turn its collider on – otherwise it’s always off.
  5. Attach the component at the bottom of the post to AboveTrigger.
  6. (Optional) Scale the AboveTrigger down by 5% or so to improve the character entering from the edges of the block – this depends on the game and should always be tested and tweaked to fit your character and controls.
Simple one-way platform
Setting the platform up in editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using UnityEngine;
using System.Collections;
 
public class OneWayPlatform : MonoBehaviour {
 
    void OnTriggerEnter(Collider player)
    {
        transform.parent.collider.isTrigger = false;
    }
 
    void OnTriggerExit(Collider player)
    {
        transform.parent.collider.isTrigger = true;
    }
 
    // Use this for initialization
    void Start () {
        transform.parent.collider.isTrigger = true;
    }
 
}
using UnityEngine;
using System.Collections;

public class OneWayPlatform : MonoBehaviour {

    void OnTriggerEnter(Collider player)
    {
        transform.parent.collider.isTrigger = false;
    }

    void OnTriggerExit(Collider player)
    {
        transform.parent.collider.isTrigger = true;
    }

	// Use this for initialization
	void Start () {
        transform.parent.collider.isTrigger = true;
	}

}
  • Jesse Comb

    This seems effective, but how would you handle it with multiple players / entities potentially passing through? I am trying to do one-way platforms as well and having limited success with a flat mesh collider facing upwards that only checks for collision on the normal – It works more or less but I’m getting some weird “snapping” when a collider travels upwards through it.

    • admin

      What I did was to change the logic away from using a trigger and instead change the collision layermask for the players. The simplest way is to check in the Update function of each player if you’re jumping or traveling upwards (for example compare previous frames and current position and see if the y is changed). If you are then change the layermask of that character to ignore the platforms layer that you can jump through. And once you’re not traveling upwards anymore – re-enable the collisions.

    • laurihosio

      What I did was to change the logic away from using a trigger and instead change the collision layermask for the players. The simplest way is to check in the Update function of each player if you’re jumping or traveling upwards (for example compare previous frames and current position and see if the y is changed). If you are then change the layermask of that character to ignore the platforms layer that you can jump through. And once you’re not traveling upwards anymore – re-enable the collisions.

    • Andy

      I think make two collision maps – one for each player. Colliders tagged as Player1Only for Player1 and Player2Only for Player2. The art remains the same. Player1 on an upward vel can pass through any Player1Only colliders. Then use the system above.

  • http://www.adventuresintuesdays.com/ Bloodvork

    So what happens if they get almost all the way through the platform but not entirely and hit the trigger? Do they get stuck in the platform collider or shot out?