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

(JS)WaitForSeconds Within Update

Discussion in 'Scripting' started by Csharpner, Jan 31, 2015.

  1. Csharpner

    Csharpner

    Joined:
    May 27, 2013
    Posts:
    7
    So i have a JavaScript here, it's for a catapult, kinda like the one you can see in this trailer:

    "Go to 2:12 to see what i mean". So my basic functions are, if i press "F", the catapult goes into a charging animation. And that calls a function called Charged, which waits for 10 seconds before it's charged.
    When it's charged, i want it to be shootable with "p" after 10 seconds when it's charged.
    I have tried soooo many ways. I have barely any idea how to script, i just mixed some stuff up o_O
    First of i can't put a if(Input.GetKeyDown("p")) in my custom functions, and i cannot put Yield WaitForSeconds(x);
    in my update fuctions. There are probably good reasons for that, that i don't understand, but that's not the point though:D
    Here is my messy unproffesional script:

    1. #pragma strict
    2. var node1 : Transform;
    3. var Boulder : Transform;

    4. function Update () {
    5. if (Input.GetKeyDown("f"))
    6. {
    7. animation.CrossFade ("Charging");
    8. Charged();
    9. yield WaitForSeconds (12);
    10. {
    11. Debug.Log("Ready to Fire");
    12. }
    13. if (Input.GetKeyDown("p"))
    14. {
    15. Instantiate(Boulder,node1.transform.position,node1.transform.rotation);
    16. animation.CrossFade("Firing");
    17. }


    18. }
    19. }


    20. function Charged() {

    21. yield WaitForSeconds (9);
    22. animation.CrossFade("Charged");
    23. Debug.Log("Charged");
    24. }

    -Thank you for taking your time to read through the mess lol :D
     
    Last edited: Jan 31, 2015
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Use code tags when posting code. You can't use yield in Update; only in coroutines.

    --Eric
     
  3. Csharpner

    Csharpner

    Joined:
    May 27, 2013
    Posts:
    7
    Yeah, but i don't know how i would do this, i don't know how to use coroutines properly, could you give me a tip?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398