delay before jump

hi guys i have a jumping script that i found it here but i need to add delay before start jumping and unfortunately idk scripting :frowning:

here’s the code:

var speed : float = 6.0;
	var jumpSpeed : float = 8.0;
	var gravity : float = 20.0;

	private var moveDirection : Vector3 = Vector3.zero;

	function Update() {
		var controller : CharacterController = GetComponent.<CharacterController>();
		if (controller.isGrounded) {
			// We are grounded, so recalculate
			// move direction directly from axes
			moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
			                        Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= speed;
			
			if (Input.GetButton ("Jump")) {
				moveDirection.y = jumpSpeed;
			
			}
		}

		// Apply gravity
		moveDirection.y -= gravity * Time.deltaTime;
		
		// Move the controller
		controller.Move(moveDirection * Time.deltaTime);
	}

idk how to use yield waitforseconds

Thank you for all supports!

You need a tempory variable to know when to jump and then a coroutine for delaying your jump. You shouldn’t change moveDirection in coroutine because it will override in update function.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var jumpDelay : float 1.0f;

private var hasToJump = false;
private var moveDirection : Vector3 = Vector3.zero;

function Update() {
	var controller : CharacterController = GetComponent.<CharacterController>();
	if (controller.isGrounded) {
		 // We are grounded, so recalculate
		 // move direction directly from axes
		 moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
		                         Input.GetAxis("Vertical"));
		 moveDirection = transform.TransformDirection(moveDirection);
		 moveDirection *= speed;
		 
			 
		if (Input.GetButtonDown("Jump")) 
			DelayJump();
		
		if(hasToJump)
		{
			moveDirection.y = jumpSpeed;
			hasToJump = false;
		}
	}
	
	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	controller.Move(moveDirection * Time.deltaTime);
}


function DelayJump()
{
	yield WaitForSeconds(jumpDelay);
	hasToJump = true;
}