コード例 #1
0
    // Use this for initialization
    void Start()
    {
        mainCamera     = transform.Find("Main Camera").gameObject;
        ammoUI         = GameObject.FindWithTag("Ammo UI").GetComponent <AmmoUIController>();
        gameController = GameObject.FindWithTag("GameController").GetComponent <GameController>();
        bulletTimerUI  = GameObject.FindWithTag("Bullet Timer").GetComponent <BulletTimer>();

        currWeaponSlotHeld = 1;
        gunController      = mainCamera.transform.Find("Weapon Slot " + currWeaponSlotHeld).GetChild(0).GetComponent <GunController>();

        ammoUI.setAmmoCount(gunController.currAmmoInClip, gunController.currSpareAmmo);
    }
コード例 #2
0
    void SwapWeapons(int weaponSlot)
    {
        if (!mainCamera.transform.Find("Weapon Slot " + weaponSlot).GetChild(0))
        {
            // Must not have a gun in the specified weapon slot. Don't swap.
            return;
        }

        if (gunController.zoomedIn)
        {
            StartCoroutine(gunController.Zoom());
        }

        // Set current held weapon model to inactive.
        gunController.canFire = true;    // Indicates to current gunController to cancel reload.
        gunController.animations.Stop(); // Stop any animations, particularly bullet chambering animations.
        mainCamera.transform.Find("Weapon Slot " + currWeaponSlotHeld).GetChild(0).GetChild(0).gameObject.SetActive(false);

        Transform newWeapon = mainCamera.transform.Find("Weapon Slot " + weaponSlot).GetChild(0);

        // Set the new weapon model to active.
        newWeapon.GetChild(0).gameObject.SetActive(true);

        // Get reference to gunController for new weapon and reset transform.
        gunController = newWeapon.GetComponent <GunController>();
        gunController.resetTransform();
        currWeaponSlotHeld = weaponSlot;

        // Update UI.
        ammoUI.setAmmoCount(gunController.currAmmoInClip, gunController.currSpareAmmo);
        if (gunController.currAmmoInClip > 0)
        {
            StartCoroutine(bulletTimerUI.RunTimer(0.0f, "full"));
        }
        else
        {
            StartCoroutine(bulletTimerUI.RunTimer(0.0f, "empty"));
        }
    }
コード例 #3
0
ファイル: GunController.cs プロジェクト: AMTerp/UnityProjects
    public void Fire()
    {
        if (currAmmoInClip > 0)
        {
            if (attachedToPlayer)
            {
                applyRecoil(recoilAmount, recoilYBias);
            }

            gunSounds.PlayOneShot(gunFireSound, gunFireVolume);
            animations.Play(gameObject.name + " Shot");

            // If last bullet is being fired, don't do chamber bullet animation nor play .
            if (currAmmoInClip != 1)
            {
                if (chamberBullet)
                {
                    StartCoroutine(ChamberBullet());
                }

                if (attachedToPlayer)
                {
                    StartCoroutine(bulletTimerUI.RunTimer(firePause));
                }
            }
            else if (attachedToPlayer)
            {
                StartCoroutine(bulletTimerUI.RunTimer(firePause, "empty"));
            }


            RaycastHit hit;
            if (Physics.Raycast(transform.parent.parent.position, transform.up, out hit))
            {
                if (ammoType.Equals("bullet"))
                {
                    if (hit.collider.gameObject.CompareTag("Enemy"))
                    {
                        Health health = hit.collider.gameObject.transform.parent.GetComponent <Health>();
                        health.TakeDamage(damage);
                    }
                }
                else if (ammoType.Equals("object"))
                {
                    if (hit.collider.gameObject.CompareTag("Ground"))
                    {
                        Instantiate(objectToSpawn, hit.point, Quaternion.identity);
                    }
                }
            }

            currAmmoInClip -= 1;
            nextFire        = Time.time + firePause;

            if (attachedToPlayer)
            {
                ammoUI.setAmmoCount(currAmmoInClip, currSpareAmmo);
            }
        }
        else
        {
            // Clip must be empty. Play appropriate sound.
            gunSounds.PlayOneShot(emptyGunSound, emptyGunVolume);

            // Prevent the spamming of the emptyGunSound sound effect.
            nextFire = Time.time + firePause;
        }
    }
コード例 #4
0
ファイル: BuyAmmoButton.cs プロジェクト: AMTerp/UnityProjects
    void buyAmmo()
    {
        // Check that player has the relevant gun.
        Transform mainCamera = GameObject.FindWithTag("Player").transform.Find("Main Camera");
        Transform gunSlot;
        int       i;

        for (i = 0; i < mainCamera.childCount; i++)
        {
            gunSlot = mainCamera.GetChild(i);
            if (gunSlot.childCount > 0 && gunSlot.GetChild(0).gameObject.name.Equals(gunName))
            {
                // Player has the relevant gun.

                // Only buy ammo if the player has enough money.
                if (gameController.money >= ammoCost)
                {
                    GunController gunController = gunSlot.GetChild(0).gameObject.GetComponent <GunController>();
                    ammoBefore = gunController.currSpareAmmo;
                    gunController.currSpareAmmo = Mathf.Clamp(gunController.currSpareAmmo + ammoAmount,
                                                              0,
                                                              gunController.maxSpareAmmo + gunController.magazineSize - gunController.currAmmoInClip);

                    // If the player is currently holding the gun, update the ammo count on the UI.
                    if (gunSlot.GetChild(0).GetChild(0).gameObject.activeSelf)
                    {
                        ammoUI.setAmmoCount(gunController.currAmmoInClip, gunController.currSpareAmmo);
                    }

                    // Update the player's money and update the UI.
                    // Only charge for the ammo that the player actually gains.
                    moneyController.changeMoneyText(-ammoCost * (gunController.currSpareAmmo - ammoBefore) / ammoAmount);

                    // Play the buy sound only if ammo was actually bought.
                    if (ammoBefore < gunController.currSpareAmmo)
                    {
                        shopAudioSource.PlayOneShot(buySound, buySoundVolume);
                    }
                    else
                    {
                        // No ammo was bought.
                        PlayDenySound();
                    }
                }
                else
                {
                    // Player does not have enough money to buy ammo.
                    PlayDenySound();
                }

                // Relevant gun was found. Stop searching.
                break;
            }
        }

        if (i == mainCamera.childCount)
        {
            // Player did not have the relevant gun.
            PlayDenySound();
        }
    }