(JS)WaitForSeconds Within Update

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 :eyes:
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:smile:
Here is my messy unproffesional script:

  • #pragma strict
  • var node1 : Transform;
  • var Boulder : Transform;
    • function Update () {
  • if (Input.GetKeyDown(“f”))
    • {
  • animation.CrossFade (“Charging”);
  • Charged();
    • yield WaitForSeconds (12);
  • {
  • Debug.Log(“Ready to Fire”);
  • }
  • if (Input.GetKeyDown(“p”))
  • {
  • Instantiate(Boulder,node1.transform.position,node1.transform.rotation);
  • animation.CrossFade(“Firing”);
  • }
      • }
  • }
      • function Charged() {
    • yield WaitForSeconds (9);
  • animation.CrossFade(“Charged”);
  • Debug.Log(“Charged”);
  • }

-Thank you for taking your time to read through the mess lol :smile:

Use code tags when posting code. You can’t use yield in Update; only in coroutines.

–Eric

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?

–Eric