How to skip an object in an array.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponSystem : MonoBehaviour
{
public GameObject[] weapons;
public bool switchingWeapons;
public float weaponSwitchTime;
float weaponSwitchingTime;
public GameObject activeWeapon;
public Animator anim;
public int a = 1;
public Sa.StatesManager states;
[System.Serializable]
public class Holders
{
public Transform mainWeaponHolder;
public Transform mainWeaponHolder2;
public Transform pistolHolder;
public Transform swordHolder;
}
public Holders weaponHolders;
[System.Serializable]
public class WeaponSwitchTimes
{
public float RifleSwitchTime;
public float RifleUnequipTime;
public float PistolSwitchTime;
public float PistolUnequipTime;
public float MeleeSwitchTime;
public float MeleeUnequipTime;
}
public WeaponSwitchTimes times;
public Renderer[] rends;
public bool equipped;
bool unequipping;
bool add;
bool sub;
public void Start()
{
a = 1;
foreach (GameObject rw in weapons)
{
if (rw.GetComponent<Sa.WeaponHook>().weapon_Type.Pistol && rw.GetComponent<Sa.WeaponHook>().isInInventory)
{
rw.transform.parent = weaponHolders.pistolHolder;
rw.transform.localPosition = Vector3.zero;
rw.transform.localEulerAngles = Vector3.zero;
activeWeapon = null;
}
if (rw.GetComponent<Sa.WeaponHook>().weapon_Type.Rifle && rw.GetComponent<Sa.WeaponHook>().isInInventory)
{
rw.transform.parent = weaponHolders.mainWeaponHolder;
rw.transform.localPosition = Vector3.zero;
rw.transform.localEulerAngles = Vector3.zero;
activeWeapon = null;
}
if (rw.GetComponent<Sa.WeaponHook>().weapon_Type.Melee && rw.GetComponent<Sa.WeaponHook>().isInInventory)
{
rw.transform.parent = weaponHolders.swordHolder;
rw.transform.localPosition = Vector3.zero;
rw.transform.localEulerAngles = Vector3.zero;
activeWeapon = null;
}
if (!rw.GetComponent<Sa.WeaponHook>().isInInventory)
{
rends = rw.GetComponentsInChildren<Renderer>();
foreach (Renderer r in rends)
{
r.enabled = false;
}
}
}
}
void Update()
{
if (a > weapons.Length)
{
a = 1;
add = true;
equipped = true;
Equip();
}
if (a < 0)
{
a = weapons.Length;
sub = true;
equipped = true;
Equip();
}
if (switchingWeapons)
{
weaponSwitchingTime += Time.deltaTime;
if (weaponSwitchingTime >= weaponSwitchTime)
{
anim.SetBool("switchingweapons", false);
switchingWeapons = false;
weaponSwitchingTime = 0;
}
}
if (Input.GetKeyUp(KeyCode.E))
{
if (!switchingWeapons)
{
switchingWeapons = true;
anim.SetBool("switchingweapons", true);
Equip();
if (!equipped)
{
a++;
add = true;
equipped = true;
}
else
{
equipped = false;
}
}
}
if (Input.GetKeyUp(KeyCode.Q))
{
if (!switchingWeapons)
{
switchingWeapons = true;
anim.SetBool("switchingweapons", true);
Equip();
if (!equipped)
{
a--;
sub = true;
equipped = true;
}
else
{
equipped = false;
}
}
}
}
void Equip()
{
if (!activeWeapon)
{
foreach (GameObject rw in weapons)
{
if (a == rw.GetComponent<Sa.WeaponHook>().wepaonId)
{
if (rw.GetComponent<Sa.WeaponHook>().isInInventory)
{
activeWeapon = rw;
StartCoroutine(Equipping());
if (rw.GetComponent<Sa.WeaponHook>().weapon_Type.Melee)
{
anim.SetBool("isSword", true);
}
if (rw.GetComponent<Sa.WeaponHook>().weapon_Type.Pistol)
{
anim.SetBool("isPistol", true);
}
if (rw.GetComponent<Sa.WeaponHook>().weapon_Type.Rifle)
{
anim.SetBool("isRifle", true);
}
}
if (!rw.GetComponent<Sa.WeaponHook>().isInInventory)
{
if (add)
{
if (a == rw.GetComponent<Sa.WeaponHook>().wepaonId && !rw.GetComponent<Sa.WeaponHook>().isInInventory)
{
a++;
}
else
{
add = false;
}
Equip();
}
if (sub)
{
if (a == rw.GetComponent<Sa.WeaponHook>().wepaonId && !rw.GetComponent<Sa.WeaponHook>().isInInventory)
{
a--;
}
else
{
sub = false;
}
Equip();
}
}
}
}
}
else
{
StartCoroutine(Unequip());
}
}
IEnumerator Equipping()
{
if (activeWeapon.GetComponent<Sa.WeaponHook>().isInInventory)
{
if (activeWeapon.GetComponent<Sa.WeaponHook>().weapon_Type.Pistol && activeWeapon.GetComponent<Sa.WeaponHook>().isInInventory)
{
yield return new WaitForSeconds(times.PistolUnequipTime);
Transform a = anim.GetBoneTransform(HumanBodyBones.RightHand);
activeWeapon.transform.parent = a;
activeWeapon.transform.localPosition = Vector3.zero;
activeWeapon.transform.localEulerAngles = Vector3.zero;
}
if (activeWeapon.GetComponent<Sa.WeaponHook>().weapon_Type.Rifle && activeWeapon.GetComponent<Sa.WeaponHook>().isInInventory)
{
yield return new WaitForSeconds(times.RifleUnequipTime);
Transform a = anim.GetBoneTransform(HumanBodyBones.RightHand);
activeWeapon.transform.parent = a;
activeWeapon.transform.localPosition = Vector3.zero;
activeWeapon.transform.localEulerAngles = Vector3.zero;
}
if (activeWeapon.GetComponent<Sa.WeaponHook>().weapon_Type.Melee && activeWeapon.GetComponent<Sa.WeaponHook>().isInInventory)
{
yield return new WaitForSeconds(times.MeleeUnequipTime);
Transform a = anim.GetBoneTransform(HumanBodyBones.RightHand);
activeWeapon.transform.parent = a;
activeWeapon.transform.localPosition = Vector3.zero;
activeWeapon.transform.localEulerAngles = Vector3.zero;
}
}
}
IEnumerator Unequip()
{
if (activeWeapon)
{
if (activeWeapon)
{
if (activeWeapon.GetComponent<Sa.WeaponHook>().weapon_Type.Pistol && activeWeapon.GetComponent<Sa.WeaponHook>().isInInventory)
{
GameObject b = activeWeapon;
activeWeapon = null;
anim.SetBool("isPistol", false);
yield return new WaitForSeconds(times.PistolSwitchTime);
b.transform.parent = weaponHolders.pistolHolder;
b.transform.localPosition = Vector3.zero;
b.transform.localEulerAngles = Vector3.zero;
}
}
if (activeWeapon)
{
if (activeWeapon.GetComponent<Sa.WeaponHook>().weapon_Type.Rifle && activeWeapon.GetComponent<Sa.WeaponHook>().isInInventory)
{
GameObject b = activeWeapon;
activeWeapon = null;
anim.SetBool("isRifle", false);
yield return new WaitForSeconds(times.RifleSwitchTime);
b.transform.parent = weaponHolders.mainWeaponHolder;
b.transform.localPosition = Vector3.zero;
b.transform.localEulerAngles = Vector3.zero;
}
}
if (activeWeapon)
{
if (activeWeapon.GetComponent<Sa.WeaponHook>().weapon_Type.Melee && activeWeapon.GetComponent<Sa.WeaponHook>().isInInventory)
{
GameObject b = activeWeapon;
anim.SetBool("isSword", false);
yield return new WaitForSeconds(times.MeleeSwitchTime);
b.transform.parent = weaponHolders.swordHolder;
b.transform.localPosition = Vector3.zero;
b.transform.localEulerAngles = Vector3.zero;
activeWeapon = null;
}
}
}
}
}
Here is where i have gotten to. It works but doesn't work. Basically, my goal is to be able to switch through weapons. Each time i switch currently i have to double tap. first tap equips and then second tap unequips. What i would like to do is have it so when i hit equip, if a weapon is already equipped, it unequips and then equips the next one. While cycling through the weapons, i currently have a bool setup on the weapon which tells this script wether its in the inventory or not, if its not in the inventory it should skip until the next available weapon. Once cycled through all the weapons, it would start back to the first, or last depending on which way your switching, ex: in the script already. You would be able to switch either - or +. I know its a lot to ask but if there's someone who can help with this quickly i will reward you with 20 points =]
Your answer
Follow this Question
Related Questions
unable to attack enemy objects? 1 Answer
Rayscast from the GameObject that is not the player does not work 0 Answers
How can i run a script with a single key press? 1 Answer
Correctly smooth crouch script? 2 Answers
ClientRpc not functioning 0 Answers