Search code examples
c#unity-game-engine2d

OnCollisionEnter2D not being executed?


Hey I have this script on a prefab of a projectile , that is suppose to be destroyed when collide with something. Well my problem is that when it collides with something this method is never triggered and I don't know why. I even have that debug and it never appears on it . The projectile has rigibody(kinematic) and box colider ! The thing he is suppose to collide with has colider too.

public class ProjectileColision : MonoBehaviour {

public GameObject projectile;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
}

void OnCollisionEnter2D(Collision2D col)
{
    Debug.Log("executed");


    if (col.gameObject.tag == "Projectile")
    {
        Destroy(projectile,0);
    }
}
}

Solution

  • I think its because of kinematic

    A kinematic Rigidbody2D will only collide with a dynamic Rigidbody2D body type. The exception to this is if Rigidbody2D.useFullKinematicContacts is set to true in which case it will collide with all other Rigidbody2D body types.

    https://docs.unity3d.com/ScriptReference/RigidbodyType2D.Kinematic.html

    Try setting Body Type to Dynamic and see if OnCollisionEnter executes.

    OR

    Add a dynamic Rigidbody2D to the other collider