Example #1
0
    void FixedUpdate()
    {
        Vector2 scale        = m_DamagerTransform.lossyScale;
        Vector2 facingOffset = Vector2.Scale(offset, scale);

        CurrentFramePoint = (Vector2)transform.position + facingOffset;
        float len = 3;

        if (!m_CanDamage)
        {
            return;
        }

        //if (offsetBasedOnSpriteFacing && spriteRenderer != null && spriteRenderer.flipX != m_SpriteOriginallyFlipped)
        //    facingOffset = new Vector2(-offset.x * scale.x, offset.y * scale.y);

        Vector2 scaledSize = Vector2.Scale(size, scale);

        //点A,B分别是攻击范围指示框的左下和右上点
        Vector2 pointA = (Vector2)m_DamagerTransform.position + facingOffset - scaledSize * 0.5f;
        Vector2 pointB = pointA + scaledSize;


        int hitCount = Physics2D.OverlapArea(pointA, pointB, m_AttackContactFilter, m_AttackOverlapResults);

        for (int i = 0; i < hitCount; i++)
        {
            Transform trans = m_AttackOverlapResults[i].transform;
            if (!IsCanHit(trans))
            {
                continue;
            }
            AddHitInfo(trans);

            m_LastHit = m_AttackOverlapResults[i];
            //m_LastHit.GetContacts(AttackPoints); // 这个方法获得碰撞点始终有误差

            TakeDamageable damageable = m_LastHit.GetComponent <TakeDamageable>();
            // hit is Missing
            if (damageable && !damageable.isHit(damageable.DodgeRate))
            {
                OnHitMissing.Invoke();
                damageable.OnMissingAttack.Invoke();
            }

            if (damageable)
            {
                hitPoint = getPoint(CurrentFramePoint, (CurrentFramePoint - LastFramePoint).normalized, len, hittableLayers);
                damageable.TakeDamage(this, TakeAttackBuffs, ignoreInvincibility);
                status.HP += status.AttackDamageNum * status.BloodsuckingRate; // 吸血
                OnDamageableHit.Invoke(this, damageable);
                if (disableDamageAfterHit)
                {
                    DisableDamage();
                }
            }
            else
            {
                OnNonDamageableHit.Invoke(this);
            }
        }
    }