Home > Net >  Unity - Weapon equipping and swapping between them (Arena Shooter Style)
Unity - Weapon equipping and swapping between them (Arena Shooter Style)

Time:07-19

I have been struggling to try and put together a weapon equip and swap system. I will be honest in saying that I am a novice programmer and I trying to combine two scripts together but I feel this approach is just confusing me and it doesn't seem to many people have my problem.

Basically, I have a gameobject of the weapon in which when the player steps in its trigger, the gameobject disappears and the weapon is active in there hand. If they picked up multiple weapons they are able to swap between them. That is basically what I am trying to achieve. At the moment I am using Brackey's weapon swapping code and my own code to achieve this. Yet when I walk over the trigger is actives the weapon, but I am still able to scroll up and down and swap to weapons that I didn't even pick up yet.

If anyone could help me make it so you could pick up the weapon and unless you step over it, you can not swap to it. I have provided code down bellow to help give you a better understanding about what I am trying to say...

//[SerializeField] public GameObject[] myGuns;
//[SerializeField] public GameObject[] theAmmo;

void Start()
{
    SelectWeapon();
}

void Update()
{
    int previousSelectedWeapon = selectedWeapon;

    if (Input.GetAxis("Mouse ScrollWheel") > 0f) // up
    {
        if (selectedWeapon >= transform.childCount - 1)
            selectedWeapon = 0;
        else
            selectedWeapon  ;
    }
    if (Input.GetAxis("Mouse ScrollWheel") < 0f) // down
    {
        if (selectedWeapon <= 0)
            selectedWeapon = transform.childCount - 1;
        else
            selectedWeapon--;
    }

    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        selectedWeapon = 0;
    }

    if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
    {
        selectedWeapon = 1;
    }

    if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
    {
        selectedWeapon = 2;
    }

    if (Input.GetKeyDown(KeyCode.Alpha4) && transform.childCount >= 4)
    {
        selectedWeapon = 3;
    }

    if (Input.GetKeyDown(KeyCode.Alpha5) && transform.childCount >= 5)
    {
        selectedWeapon = 4;
    }

    if (Input.GetKeyDown(KeyCode.Alpha6) && transform.childCount >= 6)
    {
        selectedWeapon = 5;
    }

    if (Input.GetKeyDown(KeyCode.Alpha7) && transform.childCount >= 7)
    {
        selectedWeapon = 6;
    }

    if (Input.GetKeyDown(KeyCode.Alpha8) && transform.childCount >= 8)
    {
        selectedWeapon = 7;
    }

    if (Input.GetKeyDown(KeyCode.Alpha9) && transform.childCount >= 9)
    {
        selectedWeapon = 8;
    }

    if (Input.GetKeyDown(KeyCode.Alpha0) && transform.childCount >= 10)
    {
        selectedWeapon = 9;
    }

    if (previousSelectedWeapon != selectedWeapon)
    {
        SelectWeapon();
    }
}

void SelectWeapon()
{
    int i = 0;
    foreach (Transform weapon in transform)
    {
        if (i == selectedWeapon)
            weapon.gameObject.SetActive(true);
        else
            weapon.gameObject.SetActive(false);
        i  ;
    }
}

This is the script used for the pickup.

public GameObject weapon; //Place the weapon from the hierarchy here
public GameObject weaponHolder; //Place the weapon holder here
public GameObject weaponDummy; //Place the pickup here

void Start()
{
    weaponDummy.SetActive(true); //The weapon pickup is set to active
}

void OnTriggerEnter(Collider col)
{
    if (col.tag == "Player") //If player walks into gun collider
    {
        foreach (Transform obj in weaponHolder.transform)
        {
            obj.gameObject.SetActive(false); //Set all the weapons in the weapon holder to not active
    }
        weapon.SetActive(true); //Specified weapon is active
        weaponDummy.SetActive(false); //Set to false
    }
}

If you could provide syntax for better understand that will be much apperated!

CodePudding user response:

You have not added anything that checks which weapons are picked up and able to be selected. When you scroll and change the selectedWeapon, the SelectWeapon() method activates that GameObject without knowing anything about which ones are picked up or not.

You'll need some variable to store which weapons are currently picked up or not. Once you have that, you can change the state of that weapon in OnTriggerEnter to say you have picked it up.

Then check if that weapon is picked up before you activate it in SelectWeapon().

  • Related