playMaker

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

rechronicle

  • Playmaker Newbie
  • *
  • Posts: 47
  • Medievaliant
    • View Profile
    • Indie RPG Creator
Best Way to Check if the Character is in Mid-Air for 2D Game?
« on: January 04, 2019, 06: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: 265
    • View Profile
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #1 on: January 04, 2019, 08: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 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 collison, 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. Put them all in one state, fire them only once, and when any hits, go to new state to flip the isGrounded bool to true. If none hits, on finish, go to a state where the isGrounded is flicked to false. Then wire each through a wait state with a tiny wait time (like 0.1 real time), to return to start. It will tick away super fast, but rays are effecient, and should tell you exactly what’s going on. Use a variable on the wait time, which you can dial in later.
« Last Edit: January 04, 2019, 08:24:01 AM by Thore »

Thore

  • Sr. Member
  • ****
  • Posts: 265
    • View Profile
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #2 on: January 04, 2019, 08: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

  • Playmaker Newbie
  • *
  • Posts: 47
  • Medievaliant
    • View Profile
    • Indie RPG Creator
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #3 on: January 07, 2019, 05: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

  • Junior Playmaker
  • **
  • Posts: 71
    • View Profile
Re: Best Way to Check if the Character is in Mid-Air for 2D Game?
« Reply #4 on: January 08, 2019, 07: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, 07:36:09 AM by ch1ky3n »

rechronicle

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

Thanks for sharing!