Search Unity

Gun with passive reloading, stops when switching to another weapon

Discussion in 'Scripting' started by raileanu22dragos, Oct 7, 2021.

  1. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    In my fps game, that I would like to be very fast paced, reloading doesnt have an animation, and it doesnt trigger when the gun gets to 0 bullets, but instead every second a bullet is recharged.
    Everything works fine, no error or anything, but when I switch guns, which also works perfectly fine, the one that I dont have in my hand doesnt recharge the usual bullet per second, because its disabled by the weapons switch script.
    I am not posting a segment of my script as the script works perfectly fine, and Im only looking forward to an idea of how I might make my weapons charge in the background while not in my hand.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Introduce your own notion of what this "not equipped" type of disabled means, such that at the Unity script level it still gets called every second to reload the bullets, but it doesn't respond to being fired because it isn't the active script.
     
  3. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    So just changing the weapon switch script a bit should do it. I was thinking about using an Empty object to take care of it, but your response sounds better. Thanks
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I suppose you could make a "weapon reloader" thing and bolt that onto weapons... its job would just be to watch the weapon and anytime there was ammo missing it would slowly restore it, ignoring the state of whether the weapon is selected or not. Kinda up to you really.

    If you did that then you would probably want a generic "ammo holder" object that contains the ammo in a standard way, and then each type of gun (shotgun, laser, machine gun, etc) would consume ammo via the ammo holder.

    That would let you have a standard "ammo count watcher" UI object that just looks for the ammo box on the current weapon... I mean the number of ways you can slice and dice this stuff is pretty crazy, but all of those seem like logical possibilities that might give you some benefits, depending on how you have stuff set up.
     
    Lethn likes this.
  5. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    I'll try the ammo count watcher first, tomorrow, and update you on what comes of it
     
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    You also don't necessarily need to disable the entire gameobject or do anything fancy, you can just disable the mesh component and therefore hide the model, games developers use these sorts of tricks all the time but as far as the gamer is concerned the previous gun is missing.
     
    Kurt-Dekker likes this.
  7. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    I can try this too, but wouldn't it make the weapons that are not in my hand to shoot as well?

    foreach(Transform weapon in transform)
    {
    if(i == selectedWeapon)
    weapon.gameObject.SetActive(true);
    else
    weapon.gameObject.SetActive(false);
    i++;

    How would I change it as you said?
     
    Last edited: Oct 8, 2021
  8. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Correct, using this method you would need to disable the matching projectile or effect as well or if you have individual ammo counters for each gun set those to 0 until you show the models again. The upside is you have more control with this method whereas with blanket setActive on the GameObject it disables everything.
     
    Last edited: Oct 9, 2021
  9. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    I cant seem to make static variables work, its giving me errors upon errors, even though I'm referencing the original script before the variable name.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class AmmoBox : MonoBehaviour
    {
    static private float maxAmmoPistol = 20;
    static private float currentAmmoPistol;
    static private float reloadTimePistol = 1f;
    static public int PistolAmmo;
    void Start()
    {
    gun.currentAmmoPistol = gun.maxAmmoPistol;
    }
    void Update()
    {
    if (gun.currentAmmoPistol < 20)
    {
    StartCoroutine(Reload());
    }
    }
    IEnumerator Reload ()
    {
    yield return new WaitForSeconds(reloadTimePistol);
    gun.currentAmmoPistol++;
    }
    }

    Error: the name "gun" does not exist in the current context
     
  10. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    Ok so I made it work. Fixed some problems in the AmmoBox script and scratched my hand for like 30 minutes not realising I didn't actually make the empty gameobject for the script to work. Thanks for the idea and help
     
    Lethn likes this.
  11. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Just for future reference always post code with the code tags to make it readable, good job fixing it yourself though and I've been there tons of times posting something and then working it out.
     
    Last edited: Oct 11, 2021
  12. raileanu22dragos

    raileanu22dragos

    Joined:
    Sep 3, 2021
    Posts:
    27
    I just thought that the problem would take much less time to fix. Will do in the future