コード例 #1
0
    void Explode()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
        foreach (Collider nearbyObject in colliders)
        {
            Rigidbody rb = nearbyObject.GetComponent <Rigidbody>();
            if (rb != null)
            {
                rb.AddExplosionForce(force, transform.position, radius);
            }

            Destructible ds = nearbyObject.GetComponent <Destructible>();
            if (ds != null && (Random.Range(1, 100) < 40))
            {
                ds.Break();
            }

            PlayerHealth ph = nearbyObject.GetComponent <PlayerHealth>();
            if (ph != null)
            {
                ph.TakeDamage(20);
            }

            SC_NPCEnemy eh = nearbyObject.GetComponent <SC_NPCEnemy>();
            if (eh != null)
            {
                eh.ApplyDamage(100);
            }
        }
        foreach (GameObject gmObj in particleSystems)
        {
            Instantiate(gmObj, gameObject.transform.position, gameObject.transform.rotation);
            Destroy(gmObj, 5);
        }
    }
コード例 #2
0
    void Shot()
    {
        muzzleFlash.Play();
        GameObject cartrige = Instantiate(cartrigePrefab, cartrigeEjector.position, cartrigeEjector.rotation);

        Destroy(cartrige, 2f);
        Rigidbody rb = cartrige.GetComponent <Rigidbody>();

        rb.AddForce(cartrigeEjector.right * (cartrigeForce + Random.Range(0, 0.2f)), ForceMode.Impulse);
        rb.AddTorque(Random.Range(-15, 15), Random.Range(-15, 15), 0, ForceMode.Impulse);
        Shoot.pitch = Random.Range(1.3f, 1.5f);
        Shoot.Play();
        RaycastHit hit;

        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            Destructible target       = hit.transform.GetComponent <Destructible>();
            PlayerHealth targetPlayer = hit.transform.GetComponent <PlayerHealth>();    //Optimize******************************************************
            SC_NPCEnemy  targetEnemy  = hit.transform.GetComponent <SC_NPCEnemy>();
            ButtonScript button       = hit.transform.GetComponent <ButtonScript>();
            if (target != null)
            {
                target.TakeDamage(damage);
            }
            if (targetPlayer != null)
            {
                targetPlayer.TakeDamage(damage);
            }
            if (targetEnemy != null)
            {
                targetEnemy.ApplyDamage(damage);
            }
            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * impactForce);
            }
            if (button != null)
            {
                button.ClickButton();
            }

            Instantiate(impactEffect1, hit.point, Quaternion.LookRotation(hit.normal));
            Instantiate(impactEffect2, hit.point, Quaternion.LookRotation(hit.normal));
            Instantiate(impactEffect3, hit.point, Quaternion.LookRotation(hit.normal));
            GameObject bulletHole = Instantiate(bulletHolePrefab, hit.point, Quaternion.LookRotation(hit.normal));
            bulletHole.transform.parent = hit.transform;

            Collider[] colliders = Physics.OverlapSphere(hit.point, radius);
            foreach (Collider nearbyObject in colliders)
            {
                Rigidbody rd = nearbyObject.GetComponent <Rigidbody>();
                if (rd != null)
                {
                    rd.AddExplosionForce(force, hit.point, radius);
                }
            }
        }
    }