3 ways to do a Ground Check in Unity
Why Stack Overflow recommends it
Stack Overflow users link it when someone's Unity 2D character can jump infinitely or gets stuck on walls and needs a reliable ground or wall check.
Recommended by 4 people across 4 different months, 2020 to 2022.
What it is
A Unity 2D tutorial from the Code Monkey channel that walks through three ways to detect whether a character is standing on the ground, a basic need for platformers. It compares approaches such as raycasts and box casts, with project files offered separately.
What people said
Recommends rob1997 · 2020-04 · Stack Overflow · on “JumpCount resets immediately after jumping 2D platformer”
Does it work as expected when you lower hitDistance? and for slope detection it's better to use BoxCast instead of raycast [this video] this is a very good tutorial on how to do it. Good luck and please green tick me if I solved your problem!
Recommends Harry · 2020-10 · Stack Overflow · on “Why does this not stop my character from jumping unlimited times?”
It is likely that the collider is hitting Obstacle as it makes its first jump. OnCollisionEnter is very unreliable when checking if an object is beside/on another object. This can be checked if you use Debug.Log and check to see how much times it randomly activates OnCollisionEnter . To reliably check if an object is beside another object use raycasts [LINK] or boxcasts [LINK] instead. This is some code that should work that uses raycasts : using UnityEngine; public class PlayerCollision : MonoBehaviour { public Rigidbody rb; public Collider collider; public float extraCheckHeight = 0.1f; float upForce = 200f; bool isGrounded(){ Physics2D.Raycast(collider.bounds.center, Vector3.down, collide
Recommends SushiWaUmai · 2020-11 · Stack Overflow · on “i am making a ball rolling game and i added jumping but for some reason i can jump infinite times”
You can jump infinitely because you are not checking if the ball is on the ground. There are several ways to solve this problem. I would recommend you to watch this [LINK] video. You could also watch this [this video] if you want to know about alternative ways of checking for ground.
Recommends Toll Gnoll · 2022-12 · Stack Overflow · on “Stop building momentum when running into a wall. (2D Top-Down)”
I have 2 suggestions, either one should work: Do a ground check. Platformers often perform ground checks to check that the player is on the ground. By doing a box cast, you can check if your character is adjacent to a wall in the direction of their movement, and if they are, do not let them move (or gain speed) in that direction. Here is a video for how to do a ground check. I would recommend the box cast method here, since it would give you the most accurate results: [this video] Use the rb.velocity of the object in a direction divided by your character's maximum speed in place of your valueX and valueY. So, use: rb.velocity.x / runSpeed instead of valueX and the same for y. This would take into
Cites Andulf Games - Anders Bjerin · 2021-04 · Stack Overflow · on “How to make player stop infinite jump in unity 2d”
Yes, you do need some sort of ground check. There are a lot of good tutorials online, for example, [this video] ("3 ways to do a Ground Check in Unity"). To implement the simple "raycast down check", you need to assign the ground/platforms a layermask that is not the same as the player's layer. In this example, I have added a new layer named "Ground". [LINK] You can then use this simple code to do the test. BoxCollider2D boxColliderPlayer; int layerMaskGround; float heightTestPlayer; void Start() { rb = GetComponent<Rigidbody2D>(); // Get the player's collider so we can calculate the height of the character. boxColliderPlayer = GetComponent<BoxCollider2D>(); // We do the height test from the cent
Cites Owain · 2022-01 · Stack Overflow · on “Is there an easy solution to my collision issue?”
I just want to say firstly, you're extremely kind for dedicating so much attention to this problem, I genuinely had no idea that this "simple first project" of mine would bring up so many complications and fear my little fun project might be far to beyond my current scope - I've only been learning for 2/3 months. I was looking at this video and thinking about how I could implement something similar, [this video] But I'm also thinking that maybe I should continue learning C# and Unity before trying to take my project any further.