2D platformers, ladders, collisions, oh my!

Hey everybody! I picked up the free version of Unity about a month ago and have been doing a bunch of mini projects to get used to the software (and to scripting with it). I’m not a stranger to programming, but I’ve yet to really get used to “how Unity thinks” or, in this case, how to use certain aspects of Unity’s collision code to get what I want done.

Right now I’m working on a simple 2D platformer. It’s similar to the Jumper games or Super Meat Boy but also will incorporate the necessity to collect all of the ___ (insert goodie name here) to access the end of the level. I’ve got the movement / jumping and all that working well. I put in ladders and those are working fine thus far with one snag: the top of the ladder. What I want is for the player to treat the top of the ladder as though it is solid ground (so he can run across a normal platform and ladder top without a hitch). If on top of a ladder and the player presses down, the character should start climbing down. Simple, right? :wink:

Ladders are basically triggers that allow for vertical movement (and also disable gravity for the player while he’s climbing).

Is there a way to determine that a ladder trigger is under the character controller? In a perfect world, the code would be something like:

if (LadderIsUnderPlayer canClimb == false)
{
controller.isGrounded = true;
}

Just an idea, but how about creating a new prefab made solely of two cubes. Turn both of the renderers off for them so they are not visible at all and position them in a way that resembles an extremely wide exclamation point that matches the width of the ladder. Give them both BoxCollider components and check the “Is Trigger” option in the BoxCollider controller for both of them. Then write a script for the top cube where OnTriggerEnter makes the lower cubes IsTrigger set to false turning it solid and OnTriggerExit setting it to true so you can pass through it when not standing on top.

This might present issues with NPCs or other players (if you have multiplayer support) colliding with the cube when you are standing on it instead of passing through but there should be a way to have them ignore certain colliders. I am fairly new with Unity so there could be a better way to do this, but that is how I would approach it. Not too much coding necessary and it should be a very reusable component.

Thanks for the input!

I thought about this awhile back, but I believe it to cause issues with enemy ai and whatnot (colliding with the top box when its active). Furthermore, what happens if the player jumps from the side, for example, onto the top of the ladder when the collision box isn’t set to be a trigger? He’ll collide with the side of the ladder and fall straight down, right?

I tried creating a plane (with a mesh collider component attached) which I placed atop the ladder. That worked great for keeping the player from falling down into the ladder, but unfortunately has issues when he climbs up through it. While colliding with the plane, the player can only move upward, not to the sides or downward.

A really cheap way to fix it is to not give the player the option of grabbing a ladder (automatically setting the player’s isClimbing variable to true when he collides with the trigger). Then I’d just make the top of the ladder trigger a little higher than the adjoining platforms. That way, the player can just run through without issue.

I’d rather not have to do that tho.

Thats the beauty of testing! You know what happens with the what-ifs :stuck_out_tongue:

What if the player jumps for the side and collides with the collision box if it isn’t set to be a trigger?

Well, by default it should be set to be a trigger so they should not collide with the box and the OnTriggerLeave event should set the collider box back to a trigger which lets the player pass through again. This should make the lower box solid only when the player’s bounding box is intersecting with the upper box.

As for your issue you experienced with the plane, you may want the script that turns the lower box solid to only make the lower box solid when your canClimb is set to false. That way the player will not collide with it until they have dismounted the ladder.

Hopefully this helps you out. I am working on a side-scrolling game also and this is my first major project in Unity. If anyone out there has any suggestion that puts mine to shame, go for it.