playMaker

Author Topic: [SOLVED] Checking If Character Is Grounded?  (Read 2436 times)

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
[SOLVED] Checking If Character Is Grounded?
« on: July 19, 2019, 09:22:23 AM »
Hey there!

I'm making a 3rd person character controller and I need to check if the character is grounded so I can play the falling animations.

Right now I have a raycast going down from the base of the character a short distance to detect the ground collider which is being checked by the Bool Test action. Ideally, once the raycast looses contact with the ground, it will send the event to start falling. However, as soon as the raycast starts, it detects False even if the character is on the ground and I can see the raycast passing through the ground.

I found this thread Topic: Raycast ground detection goes wrong which applies exactly to my situation since I'm also using a capsule collider for my controller.

After reading that, I tried offsetting the From Position vector of the raycast so it's not potentially touching the collider but changing those values don't seem to work as I expect and didn't move the raycast at all.

Another thing I tried was setting the player to the Ignore Raycast layer but the raycast was still reading False. I also tried putting the character on it's own layer and using that in the Layer Mask array field then checking Invert Mask.

Am I going about this correctly or should I try something else to detect if the character is grounded or not?

Any help is appreciated! Thanks!
« Last Edit: July 19, 2019, 01:27:26 PM by acornbringer »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Checking If Character Is Grounded?
« Reply #1 on: July 19, 2019, 10:32:03 AM »
Hi.

I use 3 raycasts and a bool any true (left center and right from players feet)
in a single state/fsm and check every frame.

all world objects have a layer set Ground / walls / object
then with layer mask you can select what to look for.

also i use a short distance, so not from the center (using from position will offset start position of the raycast)

if i need to know if hes grounded i use "get fsm bool" on another fsm or "Fsm Bool Test" (Ecosystem)

here's a sample image of the fsm :

https://i.imgur.com/GwSssx9.png

THe raycast + version also can store the layer name if needed

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Checking If Character Is Grounded?
« Reply #2 on: July 19, 2019, 12:59:57 PM »
Hi djaydino,

It seems like you and I are doing relatively the same thing so I re-created my setup on an empty game object just to test and found that it works just as expected. After a bit more playing around I found that when the racast start point is exactly on the collider for the ground, it will go through and show as not hitting anything.

I feel like I should be able to offset the raycast with From Position, but I can't get it to work. No mater what I enter in there, it always starts at the 0, 0, 0 point of my character.

Is there some way I can fix this?

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Checking If Character Is Grounded?
« Reply #3 on: July 19, 2019, 01:27:02 PM »
Okay I found out the trouble:

The from position in this raycast (not raycast2D) only uses a world-space vector or an object so moving the values around wouldn't do anything for me.

Instead I need to specify an object to start the raycast from so I made an empty object inside my character setup and just raised that a little above the ground. That way it doesn't start off intersecting with the ground and turning up false.

When I use a capsule collider and a rigged body with gravity, it must push the raycast start point right on top of the box collider of the ground to the point where it will miss the collider. When I was using a character controller setup instead of a capsule collider, it was working but I need to do it this way now so I'm glad the solution presented itself.

Thanks for the help!