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

How to prevent a button from being spammed?

Discussion in 'Scripting' started by AlejandroGorgal, Nov 24, 2013.

  1. AlejandroGorgal

    AlejandroGorgal

    Joined:
    Jul 2, 2013
    Posts:
    14
    Hi, I've got a GUI Texture button which when pressed it triggers an attack animation, it's a button for a mobile device, here's the code that triggers it:

    Code (csharp):
    1. function Update ()
    2. {
    3.     // This code detects if the pixels of the GUITexture are being touched and does the action assigned to it
    4.     for (var touch:Touch in Input.touches)
    5.     {
    6.         // Note: TouchPhase controls the type of touch, check the Script Guide for reference
    7.         if (touch.phase == TouchPhase.Began  attackButton.HitTest (touch.position))
    8.         {
    9.             // When the button is pressed it'll call the AttackDamage function
    10.             AttackDammage ();  
    11.         }
    12.     }
    13. }
    As you can see, when pressed it calls the AttackDamage function which is in charge of applying damage, playing the attack animation and sound. All of that works well but the problem is that you can spam this button and attack as much as you want, ideally I want to make it so that you can only attack again once the animation is over.
    So, Im thinking that I need another function that prevents the AttackDamage function from being called but I can't figure out how to do that. Adding a simple timer like a "yield WaitForSeconds(1.0);" culd very well work for this, but I can't work out how to make it work, any pointers?
     
  2. Mattyy

    Mattyy

    Joined:
    Jun 12, 2013
    Posts:
    42
    You can make your own timer.
    Example in C#
    public float time;
    public float timer;

    time = 2;
    timer = time;

    void Update() {
    timer -= Time.deltaTime;
    if(timer < 0 ) {
    AttackDamage();
    timer = time;
    }
    }
     
    Surd23 likes this.
  3. AlejandroGorgal

    AlejandroGorgal

    Joined:
    Jul 2, 2013
    Posts:
    14
    Thank you for the reply, Im doing this in JavaScript though, would the syntax be similar in this case?
     
  4. Napalm6b

    Napalm6b

    Joined:
    Oct 18, 2013
    Posts:
    1
    Hi I also work with java script, although not a mobile game. I think this script example will help you. I would check on the maximum number of shots on the screen, if a person tries to fire more than that, then a boolean variable switches to false and the button won't work.

    This example uses the right mouse button, but the concept should work for you:

    Code (csharp):
    1. function Update () {
    2.     //checks if player is holding mouse button down, if true fires gun then goes back to false when button isn't held
    3.     loaded = false;
    4.     if(Input.GetMouseButton(0)){
    5.     loaded = true;
    6.     }
    7. }    
    8.  
    9. function Shoot(){
    10.     //sets the gun shoot bursts of 9 bullets
    11.      if(loaded == true  bullets < 9){
    12.  
    13.         Instantiate(bullet_pre, transform.position, transform.rotation);//creates bullet
    14.         audio.Play();//plays sound
    15.         bullets++;//adds to bullet counter
    16.     }
    17.     else if(loaded == false  bullets > 0){//empties bullet counter
    18.         bullets = 0;
    19.    
    20.     }
     
  5. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Read up on animations, mainly here:
    http://docs.unity3d.com/Documentation/ScriptReference/Animation-isPlaying.html

    Code (csharp):
    1.  
    2.     function Update ()
    3.     {
    4.         // This code detects if the pixels of the GUITexture are being touched and does the action assigned to it
    5.         for (var touch:Touch in Input.touches)
    6.         {
    7.             // Note: TouchPhase controls the type of touch, check the Script Guide for reference
    8.             if (touch.phase == TouchPhase.Began  attackButton.HitTest (touch.position)  !animation.isPlaying)
    9.             {
    10.                 // When the button is pressed it'll call the AttackDamage function
    11.                 AttackDammage ();  
    12.             }
    13.         }
    14.     }
    15.  
     
  6. AlejandroGorgal

    AlejandroGorgal

    Joined:
    Jul 2, 2013
    Posts:
    14
    That works but the problem is that it only does if no animation is playing, the thing is that there's always some animation playing such as the Idle or Movement anims.

    I took that principle and tried to adapt it so that it would look for a specific animation, so this:

    Code (csharp):
    1. if (touch.phase == TouchPhase.Began  attackButton.HitTest (touch.position)  animation["Attack1"].enabled == false)
    My idea with this is that if the animation "Attack1" is not currently playing then you should be able to press the attack button, and if it's playing then the button wont work, sadly this didn't work so Im a bit lost :(
     
  7. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    I was just pointing you on the right track :)

    A quick search on google and the unity scripting reference and I found this:
    http://docs.unity3d.com/Documentation/ScriptReference/Animation.IsPlaying.html

    Code (csharp):
    1.  
    2.     function Update ()
    3.     {
    4.         // This code detects if the pixels of the GUITexture are being touched and does the action assigned to it
    5.         for (var touch:Touch in Input.touches)
    6.         {
    7.             // Note: TouchPhase controls the type of touch, check the Script Guide for reference
    8.             if (touch.phase == TouchPhase.Began  attackButton.HitTest (touch.position)  !animation.isPlaying("Attack1"))
    9.             {
    10.                 // When the button is pressed it'll call the AttackDamage function
    11.                 AttackDammage ();  
    12.             }
    13.         }
    14.     }
    15.  
    try that, try doing a little more research for next time :)
     
  8. FreeSandwiches

    FreeSandwiches

    Joined:
    Feb 15, 2013
    Posts:
    5
    make a variable that saves the Time each time a bullet is fired. before firing a new bullet subtract it from the current time and check to see if currentTime - lastTime > x. if true then set last time to current time and fire a bullet.
     
    Last edited: Nov 26, 2013
    Graham-B likes this.