public static void AreaDamageThings(Vector3 location, float radius, float damage)
    {
        var objectsInRange = (Collider[])Physics.OverlapSphere(location, radius);

        foreach (Collider col in objectsInRange)
        {
            DamageableThing thing = col.GetComponent <DamageableThing>();
            if (thing != null)
            {
                var thingCollider = thing.GetComponent <Collider>();

                // test if thing is exposed to blast, or behind cover:
                RaycastHit hit;
                var        exposed = false;
                Debug.DrawRay(location, (thingCollider.transform.position - location), Color.blue, 200);
                if (Physics.Raycast(location, (thingCollider.transform.position - location), out hit, radius, 1))
                {
                    exposed = (hit.collider == thingCollider);
                }

                if (exposed)
                {
                    // Damage Enemy! with a linear falloff of damage amount
                    float proximity = (location - thing.transform.position).magnitude;
                    float effect    = 1 - (proximity / radius);
                    thing.Damage((damage * effect));
                }
            }
        }
    }
Exemple #2
0
    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            // check ammo first

            if (inventory.UseAmmo(1))
            {
                nextFire = Time.time + weapon.fireRate;

                StartCoroutine(ShotEffect());

                Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

                RaycastHit hit;

                //laserLine.SetPosition (0, weapon.gunEnd.position);
                if (weapon.gunFireParticle)
                {
                    Instantiate(weapon.gunFireParticle, weapon.gunEnd.position, Quaternion.identity);
                }

                if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weapon.weaponRange))
                {
                    if (weapon.targetShotParticle)
                    {
                        Instantiate(weapon.targetShotParticle, hit.point, Quaternion.identity);
                    }
                    //laserLine.SetPosition (1, hit.point);
                    DamageableThing health = hit.collider.GetComponent <DamageableThing>();
                    if (health != null)
                    {
                        health.Damage(weapon.gunDamage);
                        if (hit.rigidbody != null)
                        {
                            hit.rigidbody.AddForce(-hit.normal * weapon.hitForce);
                        }
                    }
                    var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                    sphere.transform.position = hit.point;
                    //var newSphere = (GameObject)Instantiate(Resources.Load("SphereCol"), , Quaternion.Euler(0,0,0));
                    sphere.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    Destroy(sphere.GetComponent <Collider>());
                    Destroy(sphere, 0.1f);
                }
                else
                {
                    //laserLine.SetPosition (1, rayOrigin + (fpsCam.transform.forward * weapon.weaponRange));
                }
                if (ShotFired != null)
                {
                    ShotFired();
                }
            }
        }
    }