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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Firing buton issue for rapid firing

Discussion in 'Scripting' started by Sulemanalimalik, Nov 7, 2018.

  1. Sulemanalimalik

    Sulemanalimalik

    Joined:
    Jul 26, 2017
    Posts:
    79
    In unity tool i can use rapid fire by calling the update function but when i use it in the button event for andriod platform the gunfires whenever i touch anywhere on the secreen so i converted the function to fire function but now the gun only fires once on pressing the button although the working is still the same i.e for rapid firing inside the Fire function , anyone help please
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Post your code please
     
  3. Sulemanalimalik

    Sulemanalimalik

    Joined:
    Jul 26, 2017
    Posts:
    79
    Code (csharp):
    1.  
    2.  
    3. [code=JavaScript]public function Fire(){
    4.  
    5.             if(Input.GetButton("Fire1"))
    6.          {
    7.  
    8.                         M4Firing();
    9.             }
    10.  
    11.    
    12.  
    13.  
    14.  
    15.                         }
    16.  
    17.  
    18.  
    19.  
    20.             function M4Firing()
    21.                         {                          
    22.  
    23.  
    24.  
    25.                         nextFire=Time.time+fireRate;
    26.  
    27.              
    28.  
    29.                         gunSound.Play();
    30.  
    31.                         Flash.SetActive(true);
    32.  
    33.                         var hitShot:RaycastHit;
    34.  
    35.                         if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward)*100,hitShot))
    36.                         {
    37.  
    38.  
    39.                     hitShot.transform.SendMessage("DeductPoints",DamageAmount,SendMessageOptions.DontRequireReceiver );
    40.                                     if(hitShot.collider.tag !="Enemy")
    41.                    
    42.                         }
    43.                       }
    44.         }
    [/CODE]

     
    Last edited: Nov 7, 2018
  4. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
  5. Sulemanalimalik

    Sulemanalimalik

    Joined:
    Jul 26, 2017
    Posts:
    79
  6. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Ok, if I understand what it is you're doing, there's a lot of stuff that's wrong with this.

    I assume that you are using a unity UI button to call the Fire() method.

    First of all, the UI button doesn't work this way. It fires ONCE upon being released. So what happens is when you press the button and release it, it will call Fire() and be done until the next time you press and release it.

    Update() works differently. It is called every frame, this is why your method worked just fine before and doesn't work now.

    Try to replace your UI button with this script instead:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class FireButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    5. {
    6.     bool isHeld;
    7.  
    8.     void Update()
    9.     {
    10.         if(isHeld)
    11.         {
    12.             //Call your fire method in your script
    13.         }
    14.     }
    15.  
    16.     public void OnPointerDown(PointerEventData eventData)
    17.     {
    18.         isHeld = true;
    19.     }
    20.  
    21.     public void OnPointerUp(PointerEventData eventData)
    22.     {
    23.         isHeld = false;
    24.     }
    25. }
    https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html

    You also won't need the if(Input.GetButton("Fire1")) check anymore.
     
  7. Sulemanalimalik

    Sulemanalimalik

    Joined:
    Jul 26, 2017
    Posts:
    79
    which function should i put in the pointer down button ? because i make the update as public and put the update here then it's not even firing for once now and the other two functions i.e public void OnPointerDown(PointerEventData eventData) are not appearing for the button
     
  8. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Hey, they're not supposed to appear, the script is working as a replacement for the UI button component, meaning you should remove the UI button. It was more of an example, you'll need to rewrite it in javascript if you want to call the method in your script (you should really rewrite your script in c# instead).

    In the Update method of the script I provided, you should be calling the Fire() method of your script. Make a public reference to your script, drag and drop it in the editor if you want to keep things very simple.
     
  9. Sulemanalimalik

    Sulemanalimalik

    Joined:
    Jul 26, 2017
    Posts:
    79
    thank you so much bro i'm very greatful to you