1
\$\begingroup\$

In terms of a simple 2D platformer, I was wondering if, over the years, a right way has emerged as far as determining whether or not a strolling character has reached the edge of the platform it is walking on.

My searches haven't produced an answer for this question and there does not seem to be a consensus about what the best way is, but I would like to believe that there is a best way considering the classic nature of the 2D platformer.

Working with Godot 3.0.6 Mono, my current idea is two trace two rays from either side of a character's collision box downward, where a difference in the length (assuming flat platforms) between those two rays before collision means the character is partially over an edge. However, I feel like this can get intense if lot's of characters are being managed every frame. What are some other ways of approaching this? Any other suggestions are appreciated.

\$\endgroup\$
1
\$\begingroup\$

The "easy cheesy" way is just to use colliders. Obviously, that method is... quite terrible. Evolving from that, other than a regular capsule collider for the character, you can add a small collider at the feet of your character (this one should be trigger / overlap collider). Still... not very good.

The best way that I have personally seen in action is pretty much what you described. Use rays, and depending on how precise you need it to be, you can use just one (that is pretty much just detecting whether you are standing on the ground or not), two from each side or even bigger number in regular intervals. A good idea is to use "skin width", aka starting the ray a bit "inside" the character. (in this case, your starting point would be few cm above the floor). If you use the rays directly from the bottom, in some engines the detection might be a bit wonky.

The "multiple rays" method is also good for detecting slopes, and even for regular collisions. Maybe they fixed it in the meantime, but I remember that Unity had a big problem with colliders, for example your character could be stuck to the wall if you jumped and moved towards the wall at the same time. Using rays fixed the problem for me :)

Edit: First, thank you for the upvote. Second, while writing the answer I totally forgot the main reason I wanted to answer:

If you are scared about the optimization, you can use a couple of tricks. First, only use this method with the main character. Make all the NPC's use "just" gravity, or a less precise method (maybe just two rays for the NPCs, and 5 for the main char?)

Also, you can only check it whenever the character has moved. If you don't include stuff like breakable floor, there should be no issue with it at all. That will drastically reduce the number of calls!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.