- Home /
OnCollisionEnter2D not calling
I have this code attached to my player
var canJump : boolean;
var isJumping : boolean;
var onGround : boolean;
function FixedUpdate(){
if (onGround == false && isJumping == false){
transform.Translate(Vector3.down * Time.deltaTime);
}
}
function OnCollisionEnter2D(collision : Collision2D){
if (collision.gameObject.tag == "Ground"){
onGround = true;
Debug.Log("Hit solid ground");
}
}
But the onGround variable isn't changing to true, i'm passing through the object tagged "ground" and "Hit solid ground" isn't getting logged. I tried attaching a 2D rigidbody to the player and whislt it did stop when it hit the ground, the onGround variable stayed as false. Both the ground and the player have a 2D Box Collider attached. what's going on?
Have you tried to add a Debug.Log before the "if" statement inside OnCollisionEnter2D to understand if any collision is detected? It could be that your ground's tag is misspelled or may be that there's some other collider between them.
Answer by SkaredCreations · Dec 14, 2014 at 05:00 PM
Anyway if you need to use physics collision I won't suggest to translate the transform, because this would break the physics. Instead I'd suggest to use a Rigidbody2D with gravity enabled (GravityScale=1, IsKinematic=false) and the engine will do the rest (jumping would AddForce Vector2.up to rigidbody2D in FixedUpdate).