Ejemplo n.º 1
0
    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));
                }
            }
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        Vector3    rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
        RaycastHit hit;
        var        x = GameObject.Find("HealthObj");

        if (x)
        {
            Destroy(x.gameObject);
        }
        if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, viewRange))
        {
            // determine if shootable to draw health text
            DamageableThing health = hit.collider.GetComponent <DamageableThing>();
            if (health != null)
            {
                var hObj = new GameObject("HealthObj");
                hObj.AddComponent <TextMesh>();
                TextMesh textMeshComponent = hObj.GetComponent(typeof(TextMesh)) as TextMesh;
                textMeshComponent.text  = System.Math.Round(health.currentHealth, 0).ToString() + "/" + System.Math.Round(health.totalHealth, 0).ToString();
                hObj.transform.parent   = hit.collider.transform;
                hObj.transform.position = hit.transform.position;
                hObj.transform.LookAt(2 * hObj.transform.position - fpsCam.transform.position);
            }
        }
    }
Ejemplo n.º 3
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();
                }
            }
        }
    }
Ejemplo n.º 4
0
 void Start()
 {
     motor            = GetComponent <PlayerMotor>();
     Cursor.visible   = false;
     Cursor.lockState = CursorLockMode.Locked;
     rigidBody        = GetComponent <Rigidbody>();
     dmg                       = GetComponent <DamageableThing>();
     dmg.TookDamage           += new DamageableThing.TookDamageEventHandler(TookDamage);
     inventory                 = GetComponent <PlayerInventory>();
     inventory.WeaponEquipped += new PlayerInventory.WeaponEquippedEventHandler(WeaponEquipped);
     inventory.AmmoAdded      += new PlayerInventory.AmmoAddedEventHandler(AmmoAdded);
     EquipDefaultGun();
     UpdateHealth();
     UpdateAmmo();
 }