コード例 #1
0
    /// <summary>
    /// Determines what direction the hit came from.
    /// </summary>
    /// <param name="collision">Reference to self for extension.</param>
    /// <returns>Direction2D the hit came from.</returns>
    public static Direction2D GetDirectionHitCameFrom(this Collision2D collision)
    {
        // Determine the center point of the hits
        Vector2 contactsCenter = collision.GetContactsCenterPoint();

        // Get the difference from the contacts center the center of the
        // transform of the object that got hit
        Transform thisTrans  = collision.otherCollider.gameObject.transform;
        Vector2   pos2D      = new Vector2(thisTrans.position.x, thisTrans.position.y);
        Vector2   rawHitDiff = pos2D - contactsCenter;

        // Get the direction from the difference
        return(rawHitDiff.ToDirection2D());
    }