How do I stop the engine from thinking it is still grounded when in the air?
Hello, I have a pretty complicated question even though it is actually a simple issue. I am trying to program a jump set up and I use ontrigger enter, stay, and exit to determine if the player can jump. I tried before with raycasts but it was giving us way too many weird behavior problems and each fix kept bloating the size of our code and giving more weird behavior problems so I eventually told the team to forget the raycasts and use pure physics, which work very nice.
Now the intended behavior is that when the player is grounded, the jump button adds force to the rigid body allowing it to jump, which that is what happens. Now there are two tags the player can jump off from, Floor and Enemies. Floor is static but Enemies are game objects that are navmesh agents and use a navmesh for their pathfinding to chase down the player. Enemies cannot jump so they stay on the ground or wherever the navmesh allows them to navigate to.
The bug occurs when jumping off of enemies. Occasionally, for some reason when the colliders exit, they don't show false for isGrounded, they stay true instead and it allows the player to continuously jump and it constantly adds force since if u hold the button, it adds that amount of force to the jump constantly. This does not occur with the Floor unless Floor is a vertical surface which we have taken great pains to ensure we have no vertical surface marked Floor. It only happens with the enemies even tho both use the same code since the tags are referenced in the same methods. I have added a height limit so you cant go too high, it actually forces the player back down by using a velocity change to negative and it turns isGrounded to false. However, its a cheap workaround and I would rather we eliminate this weird "flying bug" completely so that our jump mechanics can work as intended.
So tl;dr, the player flies continuously upward off enemies until hitting the height limit because for some reason isGrounded is still flagged true upon exiting the colliders even tho the code shows it is supposed to be false on exit. Does anyone have any ideas or suggestion as to why this is happening? Thanks.
Answer by TarwaterPV · Oct 03, 2016 at 07:34 PM
Thanks for the reply. I was able to fix it by adding a timer for how long the player can stay in the air. Once they exceed that time limit, they are forced back down by an add force call to the rigid body. I was only able to set this up because after very careful observation of what behaviors were going on, I discovered that isGrounded was working correctly. Therefore adding a timer did the trick. Thanks for the suggestions though.
Answer by AurimasBlazulionis · Oct 01, 2016 at 09:19 AM
Ok, what sets the isGrounded? Do you use OnCollisionEnter/Exit? Well, then there just count the collisions on specific tag. When you hit a floor, do something like floors++
, the same with the enemies. And when you get OnCollisionExit, then just do floors--
and the same with enemies. And if both are 0, then you are not touching anything.
But could you tell what is wrong with raycasts? They work just fine. Here is a line of code I use if (Physics.Raycast (transform.position, Vector3.down, controller.height / 2 + controller.skinWidth + 0.02f, layerMask))
. My movement system uses character controller. And I know that my character pivot point is in the middle of the object. So I take object height (in this case CharacterController height), divide it by 2, add skin width of the character controller and then add a small number just to check a little bit further than the controller ends. I use this calculation for max distance, and the layerMask is used to check the layers (not tags) to be used in the raycast. It is a one line if statement, works all the time and I do not understand what kind of problem you have.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093745im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player keeps bumping when walking over to another sprite 1 Answer
Raycast can't detect items by player's feet 2 Answers
Player does not jump? 0 Answers
rigid.velocity.x = 0 for unknow reason 0 Answers
Question about MapCollider2D 0 Answers