Hi
I’m trying to make a 2D platformer, and a feature I want is one-way collisions. Basically this means that a creature (player, enemy, powerup, whatever) can pass through a platform from below without any interference, but when it collides from above the platform is rock solid.
if you don’t know what i mean, look at this video.
At 1:05 he jumps though the grass but then he stands safely on top of it.
Here is what it looks like in my project:
The capsule is the player, and the sphere will be an enemy.
There is a trigger slightly lower than the platform and it is a child object of the platform. The idea is to alter something about the platform before the player touches the collider.
So here I jump, enter the trigger, which lets me pass the collider and once I leave the trigger, the collider is restored to its default and becomes solid.
The obvoius thing to do would be to deactivate the collider somehow. However, this would also make the enemy fall right through it. If I had a separate collider for enemies and players, it would prevent bouncing enemies (such as the flying turtles from Mario) from passing the platform.
So instead of altering the collider, I woud have to alter the object.
Then I remembered a feature from Torque: Collision groups and layers.
What does this mean? Each object has a layer (for rendering order) and a group. Any object could be made to interact only with specific groups and layers. So I could for example have the first ten groups only for stuff in the foreground, the last ten for collisions in the background and only a handful of groups for stuff in between, and nothing would interfere with each other.
So here is my idea: Once the player, or any other object with such an ability, enters the trigger, its collision group is set to a certain group. The platform will never be able to have any collision with that group (by default, and nothing about it should be changed), so it should just ignore the object. Once the object leaves the trigger its group is reset to something normal again. So if I jumped onto the platform i can stand on it, and if I jumped back down I will safely land on the platform below, even if it is one-way as well.
It is necessary to be able to only make specific platforms one-way, as there are actual ceilings supposed to exist as well.
The big question is of course whether there is such a feature as collision groups and how to change them.
The trigger itself works, I use a pretty simple script and get my response:
function OnTriggerEnter (jumper: Collider) {
//set jumper's layer to something that can pass through the platform
Debug.Log ("ping");
}
function OnTriggerExit (jumper: Collider) {
//reset jumper's layer to something that the platform collides with
Debug.Log ("pong");
}
