Question about coll.contacts

Hey guys,

I’m trying to build a damage handling system for my spaceship game, and I need a way of determining what part of the ship is getting hit. I was hoping to do it by getting the specific point of the collision. Here is the relevant portion of my code so far:

private void OnCollisionEnter2D(Collision2D coll)
    {
        foreach (ContactPoint2D missileHit in coll.contacts)
        {
            Vector2 hitPoint = missileHit.point;
            Debug.Log(hitPoint.x + " and " + hitPoint.y);
        }
    }

Now the coordinates this is returning are in world space, which isn’t very useful to me, as the spaceship is never just going to be sitting at the origin point of the scene.

So is there a way for me to get this information in a way that is RELATIVE to the collider? That way I could, for example, determine that if the Y value is negative, then I know the rear half of the ship has been hit, or if it’s positive, then the front half of the ship has been hit, etc.

Awesome! That worked. Thanks.

Here is the code for my solution, if anyone is trying to do something similar:

private void OnCollisionEnter2D(Collision2D coll)
    {
        foreach (ContactPoint2D missileHit in coll.contacts)
        {
            Vector2 hitPoint = missileHit.point;
            Vector2 shipPivot = transform.position;
            Debug.Log("Hitpoint = " + hitPoint.x + ", " + hitPoint.y);
            Debug.Log("ShipPivot = " + shipPivot.x + ", " + shipPivot.y);

            if (hitPoint.y > shipPivot.y)
                Debug.Log("Front of ship has been hit!!!");
            else
                Debug.Log("Back of ship has been hit!!!");
        }
    }