- Home /
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)
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;
}
}
I just found out that you have to use OnCollisionEnter2D, OnCollisionExit2D, etc.
Thank you for pointing out that it needed OnCollisionEnter2D ins$$anonymous$$d of OnCollisionEnter.
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;
}
}
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;
}
}
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.
Your answer
Follow this Question
Related Questions
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