- Home /
OnCollisionEnter2D/OnCollisionStay2D only detecting collision with the ground
I'm having issues detecting collision with an NPC. The NPC has a Rigidbody2D with Kinematic type set. Also a Box Collider 2D with isTrigger
set to true.
The Layer Collision Matrix looks good and both object's Z position are set to 0. But the only arguments passed to OnCollisionEnter2D
or OnCollisionStay2D
are the ground's collider.
If I turn isTrigger
off on the NPC's collider, then onCollisionEnter works, but I want the player to be able to walk passed the NPC without physically being stopped by the collider.
Already attempted the answers given here. I'm pretty new to Unity + C#, so thanks in advanced.
// Player Script
private void OnCollisionStay2D(Collision2D collision) {
// Only logs if we're colliding with the NPC's layer (which seems to work)
LayerMask NPCLayerMask = LayerMask.GetMask("NPC");
if(!myCollider.IsTouchingLayers(NPCLayerMask)) { return; }
// If this is uncommented, nothing ever logs
// if(collision.gameObject.tag != "NPC") { return; }
Debug.Log("@@ Collision= " + collision.gameObject.name) // Always "Ground"
}
Answer by deniswells59 · Jun 06, 2019 at 11:21 PM
Okay, I figured it out. I should be using OnTriggerStay2D
.
I don't regret putting the "noob" tag on this question at all.