Collider2D vs Collision2D

2019-06-02 10:21发布

问题:

After doing some video tutorials on Youtube, I recognized that these two Classes look similar and repetitive.

-Collision2D-

This method called when an incoming collider makes contact with this object's collider (2D physics only)

 void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "Enemy")
            coll.gameObject.SendMessage("ApplyDamage", 10);

    }

-Collider2D-

This method called when another collider makes contact with this object

 void OnTriggerEnter2D(Collider2D other) {
    Destroy (other.gameObject);
}

Both methods have same purpose and look the same. What is the difference?

回答1:

Collision2D contains the info about the collision, while Collider2D is a component of the colliding object.

http://docs.unity3d.com/ScriptReference/Collision2D.html http://docs.unity3d.com/ScriptReference/Collider2D.html

As you can see, the purpose is similar, but not the same.