コード例 #1
0
    public void Shoot()
    {
        if (currentAmmo > 0 && !weaponSwitching.reloading)
        {
            currentAmmo--;
            timer = 0f;
            gunAudio.Stop();
            gunAudio.Play();
            gunLight.enabled = true;

            gunParticles.Stop();
            gunParticles.Play();

            gunLine.enabled = true;
            gunLine.SetPosition(0, transform.position);

            shootRay.origin    = transform.position;
            shootRay.direction = transform.forward;

            if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
            {
                Debug.Log(shootHit);
                EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth>();
                if (enemyHealth != null && shootHit.point != null)
                {
                    enemyHealth.TakeDamage(damagePerShot, shootHit.point);
                }
                else
                {
                    Explosive explosive = shootHit.collider.GetComponent <Explosive>();
                    if (explosive != null && shootHit.point != null)
                    {
                        explosive.TakeDamage(damagePerShot);
                    }
                }


                gunLine.SetPosition(1, shootHit.point);
            }
            else
            {
                gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
            }
        }
        else if (!weaponSwitching.reloading)
        {
            Reload();
        }
    }