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 CanCo · Nov 23, 2013 at 12:36 AM · 2drigidbodygravity

Rigidbody2D Grounded

I just want to know how to check if a Rigidbody2D is grounded, I tried velocity.y == 0 but that is buggy sometimes.

(The rigidbody is falling slowly)

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 Spinnernicholas · Dec 03, 2013 at 12:37 AM 0
Share

knock, knock. How goes it?

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Spinnernicholas · Nov 26, 2013 at 10:15 PM

Presumably, you would want to be able to jump off of everything you can stand on. I suggest using the collision normal vector so you don't have to do special things like having an isFloor attribute.

 GameObject groundedOn = null;
 bool isGrounded = false;
 
 function Update(){
   if(isGrounded)
   {
     //do something
   }
 }
 
 function OnCollisionEnter2D(theCollision : Collision){
   if(theCollision.gameObject.rigidBody.isKinematic) //Only want kinematic rigidbodies
   {
     foreach(ContactPoint2D contact in theCollision.contacts)
     {
       if(contact.normal.y > someThreshold)
       {
         isGrounded = true;
         groundedOn = theCollision.gameObject;
         break;
       }
     }
   }
 }
 
 function OnCollisionExit2D(theCollision : Collision){
   if(theCollision.gameObject == groundedOn)
   {
     groundedOn = null;
     isGrounded = false;
   }
 }
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 Spinnernicholas · Dec 04, 2013 at 07:05 PM 0
Share

I just found out that you have to use OnCollisionEnter2D, OnCollisionExit2D, etc.

avatar image CanCo · Dec 07, 2013 at 05:00 AM 0
Share

Thank you for pointing out that it needed OnCollisionEnter2D ins$$anonymous$$d of OnCollisionEnter.

avatar image
1

Answer by locrian05 · Feb 24, 2014 at 05:45 AM

A more simpler approach. Instead of using collision, I used OnTriggerStay2D. Make sure your ground or platform has 2 collider2d. check is trigger on the 2nd collider.

 //playerScript.js

 static var onGround : boolean; //static, so other script can access this
 var jump : KeyCode;
  
  
 function Update () 
 {
    if(onGround && Input.GetKeyDown(jump)) {
       rigidbody2D.velocity.y = 5;
       onGround = false;
    }
 }
  


 //groundScript.js // attached to any ground gameObject    

 function OnTriggerStay2D (hitInfo: Collider2D)
 {
    if(hitInfo.name == "player") {
       playerScript.onGround = true;
    }
 }
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 malekbakeer · Nov 23, 2013 at 04:58 AM

 var isgrounded : boolean = true;


 function Update()
 {
     if(isgrounded == true)
     {
         //Do your action Here...
     }
 }
  
 //make sure u replace "floor" with your gameobject name.on which player is standing


 function OnCollisionEnter(theCollision : Collision){

     if(theCollision.gameObject.name == "floor")
     {
         isgrounded = true;
     }
 }
  
 //consider when character is jumping .. it will exit collision.


 function OnCollisionExit(theCollision : Collision){

     if(theCollision.gameObject.name == "floor")
     {
         isgrounded = false;
     }
 }
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 rxninja · Nov 25, 2013 at 11:48 PM

Malekbakeer's solution will work if all of your possible collisions are below you, but it will also set isGrounded to true when you hit the ceiling.

You should either split your world geometry or your character into a set of child objects. For instance, you have your parent Player object, but then you'd have a child for feet and a child for body/head. Each child would have a collider (circle colliders work well for feet and box colliders are good for bodies, which is how the 4.3 2D demo project does it). From there you could do a collision check on the feet and see if they're hitting the ground, to which your code would work properly since your head is always going to hit the ceiling and prevent your feet from doing so.

The other way to do it is to split the geometry into different colliders with children for walls/sides, floors, and ceilings. This gets complicated with irregular geometry, however, and I recommend sticking to the split player instead of a split world.

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

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

20 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

Related Questions

[Complex] How to move the player similar to character controller while also applying rigidbody physics vectors 1 Answer

Instantiated prefab changes gravitational speed when dragged? 0 Answers

Trying to reverse planetary gravity 1 Answer

jump script : 2D 1 Answer

Horizontal gravity on One gameobject 2 Answers


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