Hello,

My script is not exactly a jump script, it acts more like a jetpack. I have a jump function already, but it only moves the player up and down. So I wanted to give the player forward motion. So when the player presses “space” they move up via the jump script and forward via the below script. Problem is if they keep the space bar pressed down, they keep moving forward. Doesn’t look nice.

So what I would like to do is put a delay in how often this script is available. Maybe press space and the script runs for a few seconds then the player has to press space again.

Thank you in advance,

using UnityEngine;
using System.Collections;

public class JumpForward : MonoBehaviour {

	public float JumpForce;
	public float startTime = 3.5f;
	public GameObject player;

	void Start ()
	{

	}

	void Update () {
		
		if (Input.GetKey (KeyCode.Space)) {
				startTime -= Time.deltaTime;
				player.GetComponent<Rigidbody> ().AddRelativeForce(new Vector3 (0.0f, 0.0f, 1.0f) * JumpForce);

			}
		}

}

Coroutine myRoutine = null;

void Update(){
      if(myRoutine != null){
            return;
       }

    //otherwise, there is no routine in progress, launch one if needed 
     if(Input.GetKey(KeyCode.Space){
           myRoutine =  this.StartCoroutine(delayedJump(0.4f))
     }
      
}

Ienumerator delayedJump(float delay){

 yield Return new WaitForSeconds(delay);

//perform jump here

myRoutine = null
}

Igor,

I tried putting in semicolumns, but cant figure out where to put them. Seems like the error is surrounding “new”. Would you be able to help me figure it out?

Thank you,