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. Dismiss Notice

Stop an asset from walking on GetKeyUp

Discussion in 'General Discussion' started by FrancisProgrammer, Jan 11, 2015.

  1. FrancisProgrammer

    FrancisProgrammer

    Joined:
    Jan 11, 2015
    Posts:
    6
    So first time to post here guys.

    I just wanted to ask what could be done so that the asset would stop walking for, say 1.5-2 sec., upon pressing 'Z'
    Here is what I have done for now.

    if (Input.GetKeyUp (KeyCode.Z)) {
    IsHitting = true;
    speed = 0;
    for(double i=0;i<=1.5f;i+=0.1){ Debug.Log ("Halted"); }
    speed = 50.0f;
    }

    An immediate reply would be very much appreciated.
    BTW. This is for a 2D Game. Theeenks
     
    Last edited: Jan 11, 2015
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    Use a yield WaitForSeconds(1.5) call instead.
     
  3. FrancisProgrammer

    FrancisProgrammer

    Joined:
    Jan 11, 2015
    Posts:
    6
    It is giving me an error. BTW. I put it inside the void Update()
     
  4. FrancisProgrammer

    FrancisProgrammer

    Joined:
    Jan 11, 2015
    Posts:
    6
    Wel, thanks for the info. I found a way around it already. Theeenksss!
     
  5. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    Nope.. It will yield every frame.
    Get that stuff outside the Update loop. Usually make a boolean switch which when true calls a function. Put the yield in the function and set it back to false when the yield is completed. With C# it is a bit more complicated than UnityScript and you have to use an IEnumerator instead of a void to set up the yield.. But really yield is the best method for timing without continually looping..