コード例 #1
0
    protected virtual void CheckAttackCollisions()
    {
        float   direction    = m_characterController.FacingDirection == EDirection.LEFT ? -1f : 1f;
        Vector2 attackOrigin = new Vector2(transform.position.x + (direction * (m_sr.size.x / 2f)),
                                           transform.position.y + m_sr.size.y / 2f);

        Vector2 boxOrigin = new Vector2(attackOrigin.x + (direction * (m_attackX / 2f)), attackOrigin.y);

        Collider2D[] collisions = Physics2D.OverlapBoxAll(boxOrigin, new Vector2(m_attackX, m_attackY), 0f, LayerMask.GetMask("HitBox"));

        Debug.Log("CheckAttackCollisions : " + collisions.Length);

        for (int i = 0; i < collisions.Length; i++)
        {
            Debug.Log("CheckAttackCollisions : " + TargetTag);
            Debug.Log("CheckAttackCollisions : " + collisions[i].tag);

            if (collisions[i].CompareTag(TargetTag))
            {
                HealthBehaviour hittable = collisions[i].GetComponentInParent <HealthBehaviour>();
                if (hittable != null && hittable.gameObject != gameObject)
                {
                    hittable.Hit(gameObject, m_damage);
                }
            }
        }
        Debug.Log("CheckAttackCollisions : END");
    }