Search Unity

Is there a way to make bool come true after few seconds?

Discussion in 'Scripting' started by Atix07, Jul 25, 2014.

  1. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Hello! So I have FPS system and when I press the button "G" my current weapon is droping. But when ı pressed the button "G" quickly my current weapon is cloning 5 or more times. I am using instantiate to drop my weapon and deleting the old one in the ınventory. So I made an bool called it "Once" so when I press the g bool goes false. So ı can only drop once now. How ı can make that bool come true again?

    Code (csharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.G) && m_PlayerAyar.Once)
    3.         {
    4.             m_PlayerAyar.Once = false;
    5.             GameObject go = Instantiate(Resources.Load("SodaCanPickUp"),Player.position,Player.rotation) as GameObject;
    6.             m_PlayerAyar.Wait();
    7.             m_PlayerAyar.Once = true;
    8.         }
    9.  
    Somethink like this or maybe you have cooler idea :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @LeftyRighty Thanks! I think I should work on why so slow to destroy the weapon on ınventory... If I can destroy that quick you cant press G.
     
  4. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    It looks like you need an input delay...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class InputDelay : MonoBehaviour {
    5.  
    6.     public float inputDelay = 0.1f;
    7.     public bool inputDelayed = false;
    8.  
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (Input.GetMouseButton(0))
    16.         {
    17.             if (!inputDelayed)
    18.             {
    19.                 Debug.Log("Button Pressed");
    20.                 SetInputDelay();
    21.                 // DO STUFF          
    22.             }
    23.             else
    24.             {
    25.                 Debug.Log("Button Pressed but blocked by input delay");
    26.             }
    27.         }
    28.     }
    29.  
    30.     void SetInputDelay()
    31.     {
    32.         inputDelayed = true;      
    33.         Invoke("ClearInputDelay", inputDelay);
    34.     }
    35.  
    36.     void ClearInputDelay()
    37.     {
    38.         inputDelayed = false;
    39.     }
    40. }
     
  5. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @Arowx Thanks for the help but its now working...
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Soda : MonoBehaviour
    6. {
    7.     private vp_FPInventory m_Inventory = null;
    8.     private PlayerAyar m_PlayerAyar = null;
    9.     private TextInventory m_TextInventory = null;
    10.  
    11.     public Transform Player;
    12.  
    13.     public float inputDelay = 2;
    14.     public bool inputDelayed = false;
    15.  
    16.     // Use this for initialization
    17.     void Awake ()
    18.     {
    19.         Player = GameObject.FindGameObjectWithTag("Player").transform;
    20.         m_Inventory = Player.transform.GetComponent<vp_FPInventory>();
    21.         m_PlayerAyar = Player.transform.GetComponent<PlayerAyar>();
    22.         m_TextInventory = Player.transform.GetComponent<TextInventory>();
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update ()
    27.     {
    28.         vp_ItemInstance currentObject = m_Inventory.CurrentWeaponInstance as vp_ItemInstance;
    29.        
    30.         if (Input.GetMouseButtonDown(0))
    31.         {
    32.             m_PlayerAyar.Susuzluk = m_PlayerAyar.Susuzluk - 20;
    33.             m_TextInventory.SodaAdet -= 1;
    34.             m_Inventory.TryRemoveItem(currentObject);
    35.         }
    36.         if (Input.GetKeyDown(KeyCode.G))
    37.         {
    38.             if (!inputDelayed)
    39.             {
    40.                 Debug.Log("Button Pressed");
    41.                 SetInputDelay();
    42.                 // DO STUFF  
    43.                 m_TextInventory.SodaAdet -= 1;
    44.                 GameObject go = Instantiate(Resources.Load("SodaCanPickUp"), Player.position, Player.rotation) as GameObject;
    45.             }
    46.             else
    47.             {
    48.                 Debug.Log("Button Pressed but blocked by input delay");
    49.             }
    50.         }  
    51.     }
    52.     void SetInputDelay()
    53.     {
    54.         inputDelayed = true;
    55.         Invoke("ClearInputDelay", inputDelay);
    56.     }
    57.  
    58.     void ClearInputDelay()
    59.     {
    60.         inputDelayed = false;
    61.     }
    62. }
    63.  
    64.