Search Unity

Picking up ammo and making ammo clips

Discussion in 'Scripting' started by Psylova, Dec 19, 2019.

  1. Psylova

    Psylova

    Joined:
    May 5, 2016
    Posts:
    7
    Hey everyone. I've recently been trying my hand at designing a first-person shooter game. Following the tutorials on the Brackeys channel, I was able to make it work.





    However he didn't go over how to collect ammo pickups. Only how to make reloading work when you have ammo. So I tried to add this myself. I created a new boolean called clips, which has the amount of ammo clips you can reload before you need to collect some more. The default is 5. Then I created a new script on my hand (his weapon holder) and called it ClipsLeft. This is the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClipsLeft : MonoBehaviour
    6. {
    7.     public int maxClips;
    8.     public int clipsLeft;
    9.  
    10.     void OnEnable()
    11.     {
    12.         maxClips = GetComponent<Gun>().clips;
    13.         clipsLeft = maxClips;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (GetComponent<Gun>().isReloading)
    19.         {
    20.             clipsLeft = clipsLeft--;
    21.         }
    22.  
    23.  
    24.         if (clipsLeft >= 0)
    25.         {
    26.             GetComponent<Gun>().noAmmo = true;
    27.         }
    28.     }
    29. }
    In it, I take the clips bool in my Gun script and use it to manage the max clips on this script. Clips left is then set to the same as maxClips. Every time the isReloading bool is set to true on the other script, a clipsLeft is set to remove one from it. When it is equal to or greater than zero, it is supposed to set another bool that I added to the gun script called noAmmo to true.

    I then created an ammo script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ammo : MonoBehaviour
    6. {
    7.     public Gun gunScript;
    8.     public GameObject thisAmmo;
    9.  
    10.     void Start()
    11.     {
    12.      
    13.     }
    14.  
    15.  
    16.     void OnTriggerEnter(Collider other)
    17.     {
    18.         if (other.CompareTag("Player"))
    19.         {
    20.             if (gunScript.noAmmo)
    21.             {
    22.                 thisAmmo.gameObject.SetActive(false);
    23.                 gunScript.noAmmo = false;
    24.             }
    25.         }
    26.     }
    27. }
    It's placed on my ammo object (a small yellow cube on the ground). When the player walks over it, it's supposed to set noAmmo to false, so that the game will load the reload animation.

    In working on this throughout the afternoon, I have gotten the clips script to match up to the gun script. In other words, the values maxClips is working. Unfortunately, for some reason it is automatically enabling isReloading and noAmmo, as well as a reloading animation I have (which is consistently looping). I have outlined in the scripts Start and OnEnable functions for it to have these off by default, but it keeps turning them on and I have no idea why it's doing this.

    Could someone please help me solve this issue, or give me a better way to allow ammo pickups with my current system?

    Thanks!

    GUN SCRIPT:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class Gun : MonoBehaviour
    5. {
    6.     public float damage = 10f;
    7.     public float range = 100f;
    8.     public float impactForce = 30f;
    9.  
    10.     public int maxAmmo = 10;
    11.     public int currentAmmo;
    12.     public bool noAmmo = false;
    13.     public int clips = 5;
    14.  
    15.     public float reloadTime = 1f;
    16.     public bool isReloading = false;
    17.  
    18.     public Camera fpsCam;
    19.     public GameObject muzzleFlash;
    20.     public GameObject ImpactEffect;
    21.     public float fireRate = 15f;
    22.  
    23.     private float nextTimeToFire = 0f;
    24.     public Animator animator;
    25.  
    26.     private void Start()
    27.     {
    28.         noAmmo = false;
    29.         isReloading = false;
    30.         currentAmmo = maxAmmo;
    31.     }
    32.  
    33.     private void OnEnable()
    34.     {
    35.         noAmmo = false;
    36.         isReloading = false;
    37.         animator.SetBool("Reloading", false);
    38.     }
    39.  
    40.     void Update()
    41.     {
    42.         if (isReloading)
    43.         {
    44.             clips = clips--;
    45.             return;
    46.         }
    47.  
    48.         else
    49.         {
    50.             if (currentAmmo >= 0f)
    51.             {
    52.                 if (noAmmo)
    53.                 {
    54.                     muzzleFlash.SetActive(false);
    55.                     isReloading = true;
    56.                     animator.SetBool("Reloading", true);
    57.                     return;
    58.                 }
    59.  
    60.                 else
    61.                 {
    62.                     StartCoroutine(Reload());
    63.                     return;
    64.                 }
    65.             }
    66.  
    67.             if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    68.             {
    69.                 nextTimeToFire = Time.time + 1f / fireRate;
    70.                 Shoot();
    71.                 muzzleFlash.SetActive(true);
    72.             }
    73.  
    74.             else
    75.             {
    76.                 muzzleFlash.SetActive(false);
    77.             }
    78.  
    79.             if (Input.GetButtonDown("KeyCode.R"))
    80.             {
    81.                 muzzleFlash.SetActive(false);
    82.                 isReloading = true;
    83.                 animator.SetBool("Reloading", true);
    84.                 return;
    85.             }
    86.         }
    87.      
    88.     }
    89.  
    90.     void Shoot()
    91.     {
    92.  
    93.         currentAmmo--;
    94.  
    95.         RaycastHit hit;
    96.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    97.         {
    98.      
    99.             Target target = hit.transform.GetComponent<Target>();
    100.             if (target != null)
    101.             {
    102.             target.TakeDamage(damage);
    103.             }
    104.  
    105.                 if (hit.rigidbody != null)
    106.                 {
    107.                     hit.rigidbody.AddForce(-hit.normal * impactForce);
    108.                 }
    109.  
    110.                 GameObject impactGO = Instantiate(ImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    111.                 Destroy(impactGO, 0.1f);
    112.         }
    113.  
    114.     }
    115.  
    116.     IEnumerator Reload()
    117.     {
    118.         muzzleFlash.SetActive(false);
    119.         isReloading = true;
    120.         Debug.Log("Reloading...");
    121.  
    122.         animator.SetBool("Reloading", true);
    123.  
    124.         yield return new WaitForSeconds(reloadTime - .25f);
    125.         animator.SetBool("Reloading", false);
    126.         yield return new WaitForSeconds(.25f);
    127.  
    128.         currentAmmo = maxAmmo;
    129.         isReloading = false;
    130.     }
    131. }
     
    Last edited: Dec 19, 2019
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Please use code tags. If not sure how, please check top pined threads in scripting forum.
     
  3. Psylova

    Psylova

    Joined:
    May 5, 2016
    Posts:
    7
    Thanks for letting me know. I have fixed it.
     
    Antypodish likes this.