Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to set timers for while.do

Discussion in 'Scripting' started by kilix19980, May 27, 2017.

  1. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    hi all i have a reaload script and i need a timer to set the realoads and be able to shoot if timer has passed

    this is reaload code i made it shorter for reading purposes
    Code (CSharp):
    1. void Reaload ()
    2.     {
    3.         if (weaponswitch == false) // if we have primary
    4.         {
    5.             if (Input.GetButton ("Reaload") & lalalal)
    6.             {
    7.                 do
    8.                 {
    9.                     ammoinclip++;
    10.                                     ammo--;
    11.                 }
    12.                 while (we have ammo );
    13.             }
    14.         }
    15.         else // this is if we have secondary
    16.         {
    17.             if (Input.GetButton ("Reaload") & lala)
    18.             {
    19.                 do
    20.                 {
    21.                     ++;
    22.                     --;
    23.                 }
    24.                 while (we still have ammo );
    25.         }
    26.     }
    27.  
    28. }
    29. }
    30.  
    31. }
    the shooting code is basicly if input fire1 instanciate so how can i make a timer ?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    A simple way to do what you're asking would be to set the next available firing time in a variable.
    When you fire, set the NextFireTime = Time.time + FiringDelay;
    then , when you check for your input:
    Code (csharp):
    1.  if (Input.GetButton(fire1) && Time.time >= NextFireTime) {
    2.    NextFireTime = Time.time + FiringDelay;
    3.    // fire whatever..
    4. }
    5.  
    etc.. like so.
     
  3. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    i mean to reaload a weapon not to shoot it ... when i press R (reaload key ) i want it to wait for a a couple of seconds then reaload the gun well actulay to run the code which reloads the gun
    Code (CSharp):
    1.   do
    2.                 {
    3.                     ++;
    4.                     --;
    5.                 }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, you could still use the same idea. Just think of the variables I provided before, but maybe with different names: like DoneReloadingTime and TimeToReload or something like that.
    So, Fire, fire, fire.. out of ammo:
    - now reloading (variable) , and finish time = current time + reload time.
    check when the time >= finish time and "reloading" is no more, and you're set.

    There are some other ways you could do this, but that seems pretty simple.

    If your situation is more complicated, please explain more :)