Hello, i was looking out for a reliable way to check for grounded status of my 2d sidescroller player, i tried ray cast solution but that doesn’t seems to be 100% reliable so i came up with another approach which seems to be a reliable way to me but not sure about its efficiency and want to see if anyone ever tried anything like that
Okay, so the concept i’ve used is polling, the character basically emits a circle around itself (invisible), lets call it “detectionArea” with some radius, all the objects tagged as “Ground” and that lie within this circle execute a script only once, lets call this script “DetectionScript”
which basically is part of ground and not the player and perform the following task
checks if there is collision with any object with tag “Player”,
if collision exists, it will inform player by setting some boolean value to true
and then script will shutdown itself
so this makes it necessary for player to keep checking for nearby items at every frame, and execute this script on these items, but i wonder if this is a good strategy ?
It sounds like you perhaps have a script then on every ground tile? This then communicates with the script on the player.
Have a look at the unity docs for Physics2D.OverlapCircle.
Consider perhaps setting a boolean on the player (isGrounded) that returns true if it detects the ground layer within the area of the circle. This I think mimics what you are trying to do in a less complicated way.
However, this is not without its problems also. It may not suit your character shape and you may need to play around with placement. For instance, if you make it bigger than the player, you might find it triggers when the player is at a wall but not on the ground.
As another alternative, what if you had an edge collider at the player’s feet that set that same boolean to true if the collider entered the ground layer?
Yea, I’ve now implemented a different strategy, just a CircleCollider2D that covers player’s lower body from all 3 sides (left, bottom and right) , it seems like a good enough option to check for collision from every possible way and works so far, better and less complicated than the previous technique, although it was reliable one, but i think having script on every ground tile is an overkill. Thanks