playMaker

Author Topic: Best Way to Check if the Character is in Mid-Air for 2D Game?  (Read 1585 times)

rechronicle

  • Full Member
  • ***
  • Posts: 119
  • Mystvaldia
    • Indie RPG Creator
Best Way to Check if the Character is in Mid-Air for 2D Game?
« on: January 04, 2019, 08:49:46 AM »
Happy new year 2019!

I was wondering if there's something similar to 'Is Grounded' for a 2D game? Or any way to work around it?
The 2D collision detection is good but I'm having a problem if the character is hugging the wall when jumping.

I want to use it to check the availability of Double Jump for now.

Thank you!

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #1 on: January 04, 2019, 10:21:10 AM »
This system is fiendish, because you’ll find out soon, that it needs to do much more than you think at first.

The basic (but eventually too simple) double jump part or basic ground check is easy to do. In the jump state where you apply force (or set velocity), simple add another button down, and you can jump mid-air. To get back to start, add a system global event ON COLLISION. But that’s not enough, and maybe not the best way to do this.

What if your character runs off a platform. Then you’re not grounded, either. What if, you bump with the head into a platform above? The collision would think you’re grounded, even when you’re not. What is on slopes or bumping into a wall?

This is what makes it fiendish. You likely need a more robust system. You have a few options. The basic principle is that a separate FSM checks all the time, sets the animator and isGrounded bool. Other FSM read that when necessary.

(1) You can make a child game object with a collider set as trigger that sticks out underneath (only) of your character. It could be a circle that is slightly smaller than the base circle collider, and slightly offset downwards, resulting in crescent at the base that can handle most cases. You can use on trigger, enter and stay to be quite precise whether there is ground under the feet.

(2) You can also constantly cast a overlap sphere (see method below), to check for ground underneath.

(3) Generally recommended by many is the use of several rays. Like, three (downwards, and diagonal to detect slopes). Switch on the debug line to see where the rays are cast and fiddle around how far, angles etc. You can dial that in before anything else. Make sure you use the correct ray version (raycast 2d), and use store did hit to flip a bool. If you use several rays, you need to collate them. For that, I use Bool Any True, that is, if any of the ground detection bools is true, then say that the character is grounded.

// stumbled upon old post, clarified the ray variant.
« Last Edit: August 16, 2019, 07:17:08 PM by Thore »

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #2 on: January 04, 2019, 10:31:04 AM »
Double jump, the simple version: in the jump state of mid air, add another get button down. With a more robust grounded system, see above, you check for isGrounded, and when jumping, you can set a isJumping to true. Then, in the airborne state, get button down, check if isJumping is true, to allow double jump. Reset the bools when returning to start state. No need to count jumps or anything like that, unless you plan for quadruple jumps eventually.

rechronicle

  • Full Member
  • ***
  • Posts: 119
  • Mystvaldia
    • Indie RPG Creator
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #3 on: January 07, 2019, 07:46:07 AM »
Hey there,
thanks for the detailed explanation! Really appreciate it!

I have tried methods 1 and 3 before but fail much. I got to understand it better now.

And I just tried method number 2. The only problem here is that I can't see the radius of the overlapping circle. Unlike Line Cast where I can see the yellow line to debug. Or am I missing something here?

ch1ky3n

  • Full Member
  • ***
  • Posts: 208
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #4 on: January 08, 2019, 09:30:13 AM »
You could always make a sprite that resembles you collider and set its alpha to fade. I change the color of my sprite colliders whenever it is interacting with other colliders. I used it to debug my colliders in-game until i got the script from asset store to display the colliders on screen.

alternatively you could also set the colliders to be always displayed on the physics 2d settings
« Last Edit: January 08, 2019, 09:36:09 AM by ch1ky3n »

rechronicle

  • Full Member
  • ***
  • Posts: 119
  • Mystvaldia
    • Indie RPG Creator
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #5 on: January 09, 2019, 09:39:07 PM »
Using the sprite to debug is a nice way too.

Thanks for sharing!