- Home /
2D Physics and Jump-Through Platforms
I need to set up jump-through platforms that will support characters landing on them from above but will allow them to jump through them from below. There was a fairly elegant solution in Unity Answers, but Physics2D for some unknown reason lacks the IgnoreCollision function. Is there another solution, aside from completely rebuilding all of my objects to use standard physics and colliders rather than the 2D versions?
Answer by toxicFork · Jan 16, 2014 at 10:44 PM
Physics2D has IgnoreLayerCollision, which is not the best solution but it could work if you switch objects to different layers based on whether player is above or below the platform
Answer by locrian05 · Feb 22, 2014 at 12:39 PM
This is a simple script that I came up with, without using complicated scripting.
//This script is attached to the player
var jumpPower : float; //assign an integer for jump power
var jumpKey : KeyCode; //assign jump button here
var downKey : KeyCode; //assign down button here
var box : BoxCollider2D; //assign the BoxCollider2D of your player and set it to is trigger
function Update ()
{
//Jump
if(Input.GetKeyDown(jumpKey))
{
rigidbody2D.velocity.y = jumpPower;
}
// If you want your object to go down on your platform
// Similar to the Contra game platform feature.
if(Input.GetKeyDown(downKey))
{
box.isTrigger = true;
}
}
// have a seperated gameobject that has a boxcollider2d make sure is trigger is checked
// in my case I named it Trigger and parent it to the platform
// and make sure it is just a little under your platform.
// your platform should have a boxcollider2D is trigger off
// detects trigger and makes your players boxcollider2D isTrigger true which disables
// collision and make it pass through your platform
function OnTriggerEnter2D (hitInfo: Collider2D)
{
if(hitInfo.name == "Trigger")
{
box.isTrigger = true;
}
}
// Exits your Trigger and set your player Collider2D.isTrigger false, which makes it collide
// to your platform again.
function OnTriggerExit2D (hitInfo: Collider2D)
{
if(hitInfo.name == "Trigger")
{
box.isTrigger = false;
}
}
While its a great idea, this will cause enemies or other players to fall through the platform too.