Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Preventing a player firing unequipped

Discussion in 'Scripting' started by Silver100, Jul 19, 2023.

  1. Silver100

    Silver100

    Joined:
    May 4, 2019
    Posts:
    30
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;//
    3. using UnityEngine;//
    4.  
    5. public class Loadedweapon : MonoBehaviour//
    6. {
    7.     public GameObject Machinegunloaded;//
    8.     public GameObject Spawngunloaded;
    9.     public GameObject Spawngunempty;
    10.     public GameObject Crosshair;
    11.     public GameObject Machinegunspawn;
    12.     public GameObject Gunspawn;
    13.  
    14.     bool Isfiring;
    15.     private void Update()
    16.     {
    17.         if (!Isfiring)
    18.     {
    19.         Spawngunloaded.SetActive(false);
    20.         Machinegunloaded.SetActive(false);
    21.         Spawngunempty.SetActive(true);//sword
    22.         Crosshair.SetActive(false);
    23.     }
    24.         }
    25.     void OnTriggerEnter(Collider other)
    26.     // works attached to player
    27.     {
    28.         //equip tag for rifle is equip, for sword swordequip
    29.         if (other.tag == "swordequip" && Isfiring)// trigger to detect weapon not player , so to determine isequipped to prevent fire when unequipped
    30.         {
    31.             Spawngunloaded.SetActive(false);
    32.             Machinegunloaded.SetActive(false);
    33.             Spawngunempty.SetActive(true);//sword
    34.             Crosshair.SetActive(false);
    35.             gameObject.GetComponent<MeshRenderMelee>().enabled = true;//
    36.         }
    37.         // using unique tags seems to work best
    38.         //equip tag for rifle is equip, for sword swordequip
    39.         if (other.tag == "rifleequip" && Isfiring)// trigger to detect weapon not player, so to determine isequipped to prevent fire when unequipped
    40.  
    41.         {
    42.             Spawngunloaded.SetActive(true);
    43.             Machinegunloaded.SetActive(false);
    44.             Spawngunempty.SetActive(false);//sword no bullets so set empty
    45.             Crosshair.SetActive(true);
    46.             Machinegunspawn.SetActive(false);
    47.             Gunspawn.SetActive(true);
    48.  
    49.         }
    50.         if (other.tag == "machinegunequip" && Isfiring)// trigger tag method to detect weapon , so to determine isequipped to prevent fire when unequipped
    51.  
    52.         {
    53.             Machinegunloaded.SetActive(true);
    54.             Spawngunloaded.SetActive(false);
    55.             Spawngunempty.SetActive(false);
    56.             Crosshair.SetActive(true);//
    57.             Machinegunspawn.SetActive(true);
    58.             Machinegunspawn.SetActive(true);
    59.             Gunspawn.SetActive(false);
    60.         }
    61.  
    62.         if (Input.GetAxisRaw("Fire1") != 0)// right trigger of a game controller
    63.         {
    64.             Isfiring = true;
    65.         }
    66.  
    67.         else if (other.tag != "machinegunequip" && Isfiring) // if player is not making contact with           item in hand the following should heappen  (does not work)
    68.         {
    69.             Spawngunloaded.SetActive(false);
    70.             Machinegunloaded.SetActive(false);
    71.             Spawngunempty.SetActive(true);// empty gun load out usually works
    72.             Crosshair.SetActive(false);
    73.         }
    74.         else if (other.tag != "rifleequip" && Isfiring)
    75.         {
    76.             Spawngunloaded.SetActive(false);
    77.             Machinegunloaded.SetActive(false);
    78.             Spawngunempty.SetActive(true);// empty gun load out usually works
    79.             Crosshair.SetActive(false);
    80.         }
    81. }
    82.  
    83. }
    84.  
    85. }
     
    Last edited: Jul 19, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Steps to success:

    - identify / create a boolean expression that captures "do I have a sword equipped?"

    - use that value to decide if you allow or inhibit an attack intention.
     
  3. Silver100

    Silver100

    Joined:
    May 4, 2019
    Posts:
    30
    Thanks been trying that a fair while and became content just with my simple method of using the sword to disable. Be good to resolve but has taken far too much time for something so trivial. Maybe I will return to it with more speedy resolution knowledge.