Hope you can help,
I’m unable to get the player to stop firing when unequipped. So far the only method that I can get to work is using the players sword collider to disable the fire method, which is not ideal for more than 2 weapons.
using System.Collections;
using System.Collections.Generic;//
using UnityEngine;//
public class Loadedweapon : MonoBehaviour//
{
public GameObject Machinegunloaded;//
public GameObject Spawngunloaded;
public GameObject Spawngunempty;
public GameObject Crosshair;
public GameObject Machinegunspawn;
public GameObject Gunspawn;
bool Isfiring;
private void Update()
{
if (!Isfiring)
{
Spawngunloaded.SetActive(false);
Machinegunloaded.SetActive(false);
Spawngunempty.SetActive(true);//sword
Crosshair.SetActive(false);
}
}
void OnTriggerEnter(Collider other)
// works attached to player
{
//equip tag for rifle is equip, for sword swordequip
if (other.tag == "swordequip" && Isfiring)// trigger to detect weapon not player , so to determine isequipped to prevent fire when unequipped
{
Spawngunloaded.SetActive(false);
Machinegunloaded.SetActive(false);
Spawngunempty.SetActive(true);//sword
Crosshair.SetActive(false);
gameObject.GetComponent<MeshRenderMelee>().enabled = true;//
}
// using unique tags seems to work best
//equip tag for rifle is equip, for sword swordequip
if (other.tag == "rifleequip" && Isfiring)// trigger to detect weapon not player, so to determine isequipped to prevent fire when unequipped
{
Spawngunloaded.SetActive(true);
Machinegunloaded.SetActive(false);
Spawngunempty.SetActive(false);//sword no bullets so set empty
Crosshair.SetActive(true);
Machinegunspawn.SetActive(false);
Gunspawn.SetActive(true);
}
if (other.tag == "machinegunequip" && Isfiring)// trigger tag method to detect weapon , so to determine isequipped to prevent fire when unequipped
{
Machinegunloaded.SetActive(true);
Spawngunloaded.SetActive(false);
Spawngunempty.SetActive(false);
Crosshair.SetActive(true);//
Machinegunspawn.SetActive(true);
Machinegunspawn.SetActive(true);
Gunspawn.SetActive(false);
}
if (Input.GetAxisRaw("Fire1") != 0)// right trigger of a game controller
{
Isfiring = true;
}
else if (other.tag != "machinegunequip" && Isfiring) // if player is not making contact with item in hand the following should heappen (does not work)
{
Spawngunloaded.SetActive(false);
Machinegunloaded.SetActive(false);
Spawngunempty.SetActive(true);// empty gun load out usually works
Crosshair.SetActive(false);
}
else if (other.tag != "rifleequip" && Isfiring)
{
Spawngunloaded.SetActive(false);
Machinegunloaded.SetActive(false);
Spawngunempty.SetActive(true);// empty gun load out usually works
Crosshair.SetActive(false);
}
}
}
}