示例#1
0
    public void Shoot()
    {
        if (nextShotTime < Time.time && !inReload)
        {
            nextShotTime = Time.time + msBetweenShots / 1000;
            gunEffects.Activate();

            Collider[] initialCollisions = Physics.OverlapSphere(barrel.position, .3f, layerMask, QueryTriggerInteraction.Ignore);

            if (initialCollisions.Length == 0)
            {
                // оружие не в обьекте
                Bullet newBullet;

                if (barrel.transform.childCount > 0)
                {
                    foreach (Transform child in barrel.transform)
                    {
                        newBullet = Instantiate(bullet, child.position, child.rotation * Quaternion.Euler(90f, 0f, 0f)) as Bullet;
                        newBullet.SetSpeed(bulletSpeed);
                        TakeShot(child.position, child.forward);
                    }
                }
                else
                {
                    TakeShot(barrel.position, barrel.forward);
                    newBullet = Instantiate(bullet, barrel.position, barrel.rotation * Quaternion.Euler(90f, 0f, 0f)) as Bullet;
                    newBullet.SetSpeed(bulletSpeed);
                }
            }
            else
            {
                Ray cameraRay = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0));
                if (playerGun)
                {
                    if (barrel.transform.childCount > 0)
                    {
                        foreach (Transform child in barrel.transform)
                        {
                            TakeShot(cameraRay.origin, cameraRay.direction);
                        }
                    }
                    else
                    {
                        TakeShot(cameraRay.origin, cameraRay.direction);
                    }
                }
                else
                {
                    if (barrel.transform.childCount > 0)
                    {
                        foreach (Transform child in barrel.transform)
                        {
                            TakeShot(transform.position, transform.forward);
                        }
                    }
                    else
                    {
                        TakeShot(transform.position, transform.forward);
                    }
                }
                //    HitObject(initialCollisions[0], transform.position);
                //    for (int i = 1; i < barrel.transform.childCount; i++)
                //    {
                //        HitObject(initialCollisions[0], transform.position);
                //    }
            }
            Instantiate(shell, shellEjector.position, shellEjector.rotation * Quaternion.Euler(90f, 0f, 0f));

            Recoil();

            PlaySound(shotSound);

            magazineBulletsCount--;
            UpdateAmmoUI();
            if (magazineBulletsCount <= 0)
            {
                StartCoroutine(Reload());
            }
        }
    }