- Home /
 
2d grounded check problem?
Hi, I'm doing a 2D game and to try and stop the play from spamming jump. I did a grounded check, only problem now is that the player can only jump once. The collision between player and ground is not being deducted. here's my code:
 public class movment : MonoBehaviour 
 {
 public float speed = 3.0f;
 public float jumpSpeed = 20.0f;
 public bool grounded = true;
     
     void Update () 
     { 
         Vector3 x = Input.GetAxis ("Horizontal") * transform.right * Time.deltaTime * speed;
         transform.Translate (x);
 
         if(Input.GetButtonDown("Jump"))
         {
             Jump();
         }
     }
 
     void Jump()
     {
         if (grounded == true) 
         {
             rigidbody2D.AddForce (Vector3.up * jumpSpeed);
             grounded = false;
         }
     }
 
     void onCollisionEnter2D(Collision hit)
     {
         grounded = true;
         Debug.Log ("I'm standing on the ground.");
     }
 }
 
               Thanks in advance.
This won't prevent the player from jumping in the air when it falls off the ground. Add a trigger collider and use OnCollisionStay2D is better. Also, here's a couple ways to do ground check using Raycast2D and OverlapCircle2D.
Answer by Hamdullahshah · Feb 26, 2014 at 10:06 AM
Correct implementation is
  void OnCollisionEnter2D(Collision2D coll) {
 
 }
 
              thank you, I implemented the correction noted. but my problem still precisest I can only preform the 1st jump only,no more. any other advice or a page to look at to put me in the right direction would be greatly appreciated.
Did the function get called means debug log is printed ? Remember one thing more one of the bodies must have a rigid body to get called the "OnCollisionEnter2D"
Answer by Qhex · Feb 26, 2014 at 02:47 PM
nevermind ... found my mistake... when I did the changed noted above I forgot to change hit to coll so it didn't work, but now everything works just fine. thanks you for assistance.
Answer by Flint Silver · Feb 26, 2014 at 06:32 PM
It's not a good idea to make possible jump everytime you get a collision. (remove "grounded = false" at line 23 i think that it's the problem you could jump only one time) So, if you add a second collider trigger on your surface and add at this gameObject with this trigger area the example code below:
  void OnTriggerStay2D(Collision2D trigger)
     {
         if (trigger.tag == "player" )
         {
         trigger.gameObject.GetComponent<NameOfTheScriptAttachedToThePlayer>().jump = true;
         }
     }
 
               or you can do it from the script of the player.
 void OnTriggerStay2D(Collision2D trigger)
 {
       if (trigger.tag == "ground")
       {
           jump = true
       }
 }
 
               Or a second possible way it's to add a groundCheck (see this video)
Or another possible solution is to use OnCollisionEnter2D similar to OnTriggerStay2D Function I wrote before.
Thank you on your effort to try and help me, the advice from Hamdullahshah solved my problem.
Your answer
 
             Follow this Question
Related Questions
Jumping when not grounded 2 Answers
How do i incorporate a double jump ingame? 1 Answer
Incremental jump while pressing Jump button 0 Answers
Check if 2D Player is grounded with Physics2D.Linecast 1 Answer
Question about Physics2D.Linecast 1 Answer