Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Qhex · Feb 26, 2014 at 08:58 AM · 2dphysicsjumpgrounded

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image casd82 · Oct 08, 2016 at 09:25 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hamdullahshah · Feb 26, 2014 at 10:06 AM

Correct implementation is

  void OnCollisionEnter2D(Collision2D coll) {
 
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Qhex · Feb 26, 2014 at 01:06 PM 0
Share

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.

avatar image Hamdullahshah · Feb 26, 2014 at 01:56 PM 0
Share

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"

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Qhex · Feb 27, 2014 at 01:48 PM 0
Share

Thank you on your effort to try and help me, the advice from Hamdullahshah solved my problem.

avatar image Flint Silver · Feb 27, 2014 at 06:45 PM 0
Share

no problem :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges