Search Unity

Is there a code for wait or delay ?

Discussion in 'Scripting' started by stonegolem, May 17, 2010.

Thread Status:
Not open for further replies.
  1. stonegolem

    stonegolem

    Joined:
    May 8, 2010
    Posts:
    20
    1) I have a gun with muzzle-flash.
    2) I want to show muzzle-flash whenever a player press fire button.
    3) muzzle-flash have to stay there for 1/4 seconds before disappearing.

    Everything is OK. My gun can fire and I can see muzzle-flash for a very short time. But I couldn't find a way to make it wait for 1/4 seconds before disappear.

    Is there a code for wait or delay ?

    Code (csharp):
    1. var bullet:Rigidbody;
    2. var muzzleFlash:Renderer;
    3.  
    4. function Update ()
    5.     {
    6.         if(Input.GetButtonDown("Fire1"))
    7.         {
    8.             var realbullet=Instantiate(bullet,this.transform.position,this.transform.rotation);
    9.             realbullet.velocity=transform.TransformDirection(Vector3.forward*50);
    10.         }
    11.     }
    12.    
    13. function LateUpdate ()
    14.     {
    15.         if(Input.GetButtonDown("Fire1"))
    16.         {
    17.             muzzleFlash.transform.Rotate(new Vector3(0,Random.Range(-180,180),0));
    18.             muzzleFlash.enabled = true;
    19.         }
    20.        
    21.         else
    22.         {
    23.         //  I need a code which makes it wait for 1/4 seconds before executing the next line.
    24.         muzzleFlash.enabled = false;
    25.         }
    26.     }
    Here is the link for my test project. You can easily see the issue.
    http://mysql01.georgebrown.ca/~muney/Weapon.html

    Thank you
     
  2. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Sure is. You're looking for WaitForSeconds. So, replace your LateUpdate with
    Code (csharp):
    1. function LateUpdate ()
    2.    {
    3.       if(Input.GetButtonDown("Fire1"))
    4.       {
    5.          muzzleFlash.transform.Rotate(new Vector3(0,Random.Range(-180,180),0));
    6.          muzzleFlash.enabled = true;
    7.          yield WaitForSeconds (0.25);
    8.          muzzleFlash.enabled = false;
    9.       }      
    10.    }
     
  3. deadManN

    deadManN

    Joined:
    May 12, 2010
    Posts:
    154
    if fire key is down :

    InvokeRepeating("myFireFunction",0,0.05);
    var dBool=true;
    function myFireFunction()
    {
    if dbool=true
    {
    statement1...
    dbool=false;
    }

    if dbool=false
    {
    statement2...
    dbool=true;
    }
    }

    else if fire key is up :

    InvokeCancel();
     
  4. deadManN

    deadManN

    Joined:
    May 12, 2010
    Posts:
    154
    if you want it for machine gun use 2 instead of 0, or some thing near like:
    InvokeRepeating("myFireFunction",2,0.05);
    both of these number are second, one is waiting time till run function for first time, second is the time that take the invoke call "myFireFunction"...
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can't yield in Update, because that runs every frame. More like:

    Code (csharp):
    1. var bullet : Rigidbody;
    2. var muzzleFlash : Renderer;
    3.  
    4. function Update ()
    5.    {
    6.       if (Input.GetButtonDown("Fire1"))
    7.       {
    8.          StopCoroutine("Shoot");    // Interrupt in case it's running
    9.          StartCoroutine("Shoot");
    10.       }
    11.    }
    12.    
    13. function Shoot ()
    14.    {
    15.       var realbullet = Instantiate(bullet, transform.position, transform.rotation);
    16.       realbullet.velocity = transform.forward*50.0;
    17.       muzzleFlash.transform.Rotate(Vector3(0.0, Random.Range(-180.0, 180.0), 0.0));
    18.       muzzleFlash.enabled = true;
    19.      
    20.       yield WaitForSeconds(.25);
    21.       muzzleFlash.enabled = false;
    22.    }
    --Eric
     
    clho40 and Stef_Morojna like this.
  6. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Eric's right. My apologies for the confusion.
     
    ROSHAN_26 likes this.
  7. stonegolem

    stonegolem

    Joined:
    May 8, 2010
    Posts:
    20
    First of all, let me thank all of you for answering my question.

    I have used Eric's code and it worked like charm.
    This is what I understand so far:

    yield WaitForSeconds (time in seconds) used for delays. But I can't use it with Function Update or Function LateUpdate.

    And "coroutine" function let me use "yield"

    deadManNs code is hard for me to understand right now. I will try to figure out it as well.

    Thanks for your help.
     
  8. nadhimali

    nadhimali

    Joined:
    Apr 4, 2015
    Posts:
    17

    LateUpdate() can not be a coroutine.
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    You make your first post in order to necro a 5 years old thread, in order to point out something that's already been pointed out in the thread?

    Forum etiquette needs to be taught in schools, man.
     
  10. justinrobinson

    justinrobinson

    Joined:
    Apr 29, 2019
    Posts:
    2
    im working with Vuforia and im looking for help with shutting off all image target behavior if the target hasn't been recognized for more than 10 sec... any ideas?
     
Thread Status:
Not open for further replies.