コード例 #1
0
    void FlyAndCheckForCollisions()
    {
        this.transform.Translate(new Vector2(speed * Time.deltaTime, 0));
        RaycastHit2D bulletFlightRaycast = Physics2D.Raycast(this.transform.position, direction, 0.2f, hitMask);

        Physics2D.Linecast(this.transform.position, direction * Time.deltaTime * 0.2f, hitMask);
        if (bulletFlightRaycast.collider != null)
        {
            Debug.Log("Bullet hit something: " + bulletFlightRaycast.collider.gameObject.name);
            if (isLethal)
            {
                IDamageable damageableEntity = bulletFlightRaycast.collider.GetComponent <IDamageable>();
                if (damageableEntity != null)
                {
                    damageableEntity.GetDamaged();
                }
            }
            else
            {
                IKnockable knockoutableEntity = bulletFlightRaycast.collider.GetComponent <IKnockable>();
                if (knockoutableEntity != null)
                {
                    knockoutableEntity.GetKnockedOut(knockOutDuration);
                }
            }

            DestroyImmediate(gameObject);
        }
    }
コード例 #2
0
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (_transform.IsChildOf(other.transform))
        {
            return;
        }
        if (_transform.parent == other.transform.parent & _transform.parent != null)
        {
            return;
        }

        // aggro radius colliders
        if (other.gameObject.layer == 15)
        {
            return;
        }
        // terrain colliders
        if (other.gameObject.layer == 16)
        {
            Die();
        }

        IDamageable <float> targetDmg = other.transform.GetComponentInChildren <IDamageable <float> >();

        if (targetDmg != null)
        {
            targetDmg.Damage(Damage);
        }

        if (!Pierce)
        {
            IKnockable knockableObject = other.transform.GetComponentInChildren <IKnockable>();
            if (knockableObject != null & Knockback)
            {
                knockableObject.Knockback(Direction * new Vector2(0.5f, 0.5f));
            }

            Die();
        }
    }