コード例 #1
0
    void OnHit(Vector3 point, Vector3 normal, Collider collider)
    {
        // damage
        if (areaOfDamage)
        {
            // area damage
            areaOfDamage.InflictDamageInArea(damage, point, hittableLayers, k_TriggerInteraction, m_ProjectileBase.owner);
        }
        else
        {
            // point damage
            Damageable damageable = collider.GetComponent <Damageable>();
            if (damageable)
            {
                damageable.InflictDamage(damage, false, m_ProjectileBase.owner);
            }

            // turn light on
            CrystalLight crystalLight = collider.GetComponent <CrystalLight>();
            if (crystalLight && _notFromEnemy)
            {
                if (crystalLight.IsToggleLaser())
                {
                    crystalLight.LightOn();
                }
            }
        }

        // impact vfx
        if (impactVFX)
        {
            GameObject impactVFXInstance = Instantiate(impactVFX, point + (normal * impactVFXSpawnOffset), Quaternion.LookRotation(normal));
            if (impactVFXLifetime > 0)
            {
                Destroy(impactVFXInstance.gameObject, impactVFXLifetime);
            }
        }

        // impact sfx
        if (impactSFXClip)
        {
            AudioUtility.CreateSFX(impactSFXClip, point, AudioUtility.AudioGroups.Impact, 1f, 3f);
        }

        // Self Destruct
        Destroy(this.gameObject);
    }
コード例 #2
0
    public void InflictDamageInArea(float damage, Vector3 center, LayerMask layers, QueryTriggerInteraction interaction, GameObject owner)
    {
        Dictionary <Health, Damageable> uniqueDamagedHealths = new Dictionary <Health, Damageable>();

        // Create a collection of unique health components that would be damaged in the area of effect (in order to avoid damaging a same entity multiple times)
        Collider[] affectedColliders = Physics.OverlapSphere(center, areaOfEffectDistance, layers, interaction);
        foreach (var coll in affectedColliders)
        {
            Damageable damageable = coll.GetComponent <Damageable>();
            if (damageable)
            {
                Health health = damageable.GetComponentInParent <Health>();
                if (health && !uniqueDamagedHealths.ContainsKey(health))
                {
                    uniqueDamagedHealths.Add(health, damageable);
                }
            }

            CrystalLight crystalLight = coll.GetComponent <CrystalLight>();
            if (crystalLight)
            {
                if (crystalLight.IsToggleLaser() && _notFromEnemy)
                {
                    crystalLight.LightOn();
                }
            }

            if (coll.CompareTag("Destructible"))
            {
                Debug.Log("Rocks shot");
                Destroy(coll.gameObject);
            }
        }

        // Apply damages with distance falloff
        foreach (Damageable uniqueDamageable in uniqueDamagedHealths.Values)
        {
            float distance = Vector3.Distance(uniqueDamageable.transform.position, transform.position);
            uniqueDamageable.InflictDamage(damage * damageRatioOverDistance.Evaluate(distance / areaOfEffectDistance), true, owner);
        }
    }