Search Unity

Function Update and yield WaitForSeconds ?

Discussion in 'Scripting' started by 08/15, Jan 7, 2011.

  1. 08/15

    08/15

    Joined:
    Jul 10, 2010
    Posts:
    23
    Hi everybody !

    I have a really simple question.
    I wrote this simple shooting script. My goal for now is to have a delay between the fire.

    I know that I can´t use yield WaitForSeconds but I´m sure that there is a really simple solution for that?

    Thanks for all answeres !!

    Code (csharp):
    1. #pragma strict
    2.  
    3. var distancex : float;
    4. var emty_game_object_fire_point : Transform;
    5. var firepoint : Vector3;
    6. var firedirection : Vector3;
    7. var impactpoint : Vector3;
    8. var the_thing_add : Transform;
    9. var the_thing_add_rotation = Quaternion.Euler(0, 0, 0);
    10. var allowfire = true;
    11.  
    12. function Update(){
    13. firedirection = emty_game_object_fire_point.TransformDirection (Vector3.forward);
    14. firepoint = emty_game_object_fire_point.position;
    15. if (allowfire) {
    16. if (Input.GetMouseButton(0)) {
    17. var hit : RaycastHit;
    18. if (Physics.Raycast (firepoint,firedirection,hit, distancex)) {
    19. impactpoint = hit.point;
    20. Instantiate (the_thing_add, impactpoint , the_thing_add_rotation);
    21. allowfire = false;
    22. //and now wait for seconds and turn allowfire true again
    23. //to allow shooting again !!??
    24.  
    25. }
    26. }
    27. }
    28. }
    29.  
     
  2. devans770

    devans770

    Joined:
    Nov 25, 2010
    Posts:
    45
    A Number of ways to solve this one. The simplest being the forum search or looking in the Documentation :)

    You could put your instantiate in to a separate function and call it using an invokerepeating which can be canceled with a cancelinvoke

    Another way is to capture the Time.time when firing and then comparing this on subsequent updates to get the difference... This code from the documentation does what you need..
    Code (csharp):
    1.  
    2. // Instantiates a projectile off every 0.5 seconds,
    3. // if the Fire1 button (default is ctrl) is pressed.
    4.  
    5. var projectile : GameObject;
    6. var fireRate = 0.5;
    7. private var nextFire = 0.0;
    8.  
    9. function Update () {
    10.   if (Input.GetButton ("Fire1")  Time.time > nextFire) {
    11.     nextFire = Time.time + fireRate;
    12.     var clone = Instantiate (projectile, transform.position, transform.rotation);
    13.   }
    14. }
    15.  
     
  3. 08/15

    08/15

    Joined:
    Jul 10, 2010
    Posts:
    23
    Sorry, the next time I will look in the Documentation or use the search !

    The code is exactly what I needed !

    Thanks :)
     
  4. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Just an additional bit of info:

    I personal HATE coding stupid timestamp based timers in Update(), i think they're ridiculous and clumsy. It is REALLY easy to code a simple a coroutine that functions exactly the same as Update(), except lets you use WaitForSeconds() yields. Just create a function and use while(true) and a yield, and initialize it in start, like this:

    Code (csharp):
    1.  
    2. function Start(){
    3.     MyUpdate();
    4. }
    5.  
    6. function MyUpdate(){
    7.     while(true){
    8.         //do stuff you would do in Update here
    9.         yield;
    10.     }
    11. }
    12.  
    So in your case, you could easily do something like this:

    Code (csharp):
    1.  
    2. public var shotDelay : float = 0.25;
    3.  
    4. function Start(){
    5.     ShootUpdate();
    6. }
    7.  
    8. function ShootUpdate(){
    9.     while(true){
    10.         if(Input.GetButton("Fire1")){
    11.             //do shot stuff
    12.             yield WaitForSeconds(shotDelay);
    13.         }  
    14.         yield;
    15.     }
    16. }
    17.  
     
  5. 08/15

    08/15

    Joined:
    Jul 10, 2010
    Posts:
    23
    @ legend411

    hey that is also a very cool way doing it !!

    I will try it :)