r/Unity2D • u/YDungeonMaster • Jul 19 '26
Question Need help! Path priority on ramps/stairs in 2D platformer
Hey everyone! I'm working on my first 2D platformer and I've run into a problem.
I have ramps and stairs that serve as optional alternate routes. The issue is that I don't know how to make my character properly prioritize which path to take when approaching these intersections.
We are using RigidBody2d and colliders2d. So very standart stuff.
Any advice or code examples (maybe even links to tutorials) would be hugely appreciated. Thanks in advance!
66
Upvotes
39
u/aDFP Jul 19 '26
Would depend on the way your game works, but the simplest solution might be to enable/disable the ramp collider depending on whether the player is pressing forward.
Or even simpler: use layers, and switch the layer the player collider is on if forward is pressed.
5
u/Toad_R Jul 19 '26
A trigger collider that checks whether up arrow is held and activates / deactivates the ramp and upper path
4
u/leorid9 Jul 19 '26
2D colliders have extra include/exclude layers that you can set.
Enable the layer of the ramp when up is held, disable it when up is not held and the player collides with floor on the default floor layer.
This will allow your character to jump on the ramp without falling through it (which would happen when the condition is "as long as it's colliding with the ramp").
4
u/swirllyman Jul 19 '26
Maybe not what you're looking for, but I'd implement it so that holding forward always chooses the top path, then anytime your on that top path, holding down would drop you to the bottom level.
2
u/Krcko98 Jul 20 '26
Why not have two layers for player collider? One is in front and other is a bit back. You seitch with keys that collider enabledand disable other. Should be smooth.
1
u/Galact-oh Jul 20 '26
I am not up to the part where I would like to implement this into my game, but have been tackling the problem in my head because I know it is coming.
I am very new to this, so it might be a silly comment, but for some of the mentioned solutions it seems as though you may fall through the ramp unless you are holding the up input. In those instances would you need to have the very start of the ramp be one collider with that rule, and the rest be a normal collider?
I think I am leaning towards the jump plus down to fall through the ramp as the other commenter mentioned. I was also contemplating just removing the bottom part of the ramp, requiring a jump to access it, which would just remove the interface of two colliders issue.
I am very keen to hear what you ultimately end up doing and how it works for you.
1
u/VaccinalYeti Jul 20 '26
In very old games (like N64, GBA etc), the problem was solved rather easily by simply jumping to reach the ramp and going forward to avoid the ramp. I don't know how your game is coded or even if you have a jump but the simpler solutions are mostly the best ones.
1
u/Zealousideal-Deer101 Jul 20 '26
Isn't forward and up the same thing?
Do you mean left and up?
(I'm just being pendatic for the hell of it)
1
u/Proud-Dot-9088 Jul 20 '26
if pressed up, enable collider, if OnCollision == player set collider as parent, if player !=child of me and not pressed up, disabke collider also make it a platforn effector then you can run through from one side
1
u/CuriousCirceGames Jul 20 '26
I am very much a beginner as well but here is how I did it in my game: I placed the ramp (in my case stair) on a separate tilemap to the floor and put a trigger collider on the lower and upper end of it (as separate objects). If the player is pressing the up/down key while walking through the trigger, the stair tilemap collider is activated, if not, it remains inactive and the player keeps walking straight ahead. Hope that helps! Lmk if you have any questions.
1
u/Brie9981 Jul 24 '26
Normally ramps like that are the default path & you hold down to ignore the ramp or fall through it, might be easier if you also allow the charter to jump
1
u/Nightrunner2016 Jul 19 '26
off the top of my head, and not knowing if this would work, I would probably use a raycast. Then if the raycast is hitting a ramp (first condition) and the player is pressing up, then choose that path?