Making a player jump, getting random results

Hi, I have written a function in my player controller to make the character jump, however I seem to get pretty random results like every 3rd or 4th jump seems to be higher or sometimes it barely jumps at all.

Have I made an error in assuming that rigidbody.addforce was the way to go here?

My player is a 2d plane that has a rigidbody with gravity.

I have variables assigned on the player to control this

var playerOneJumpRate = 1.0;		//Players time between jumps
var playerOneJumpPower = 600;		//Players Jump Power
var playerOneControlJump= ",";		//Player Controls (re-mappable)

I also have the following control function on the player

// This function controls the players jump - makes the player wait to be able to jump again
function jumpTime() 

{
	while (true) 
	{
		if (Input.GetKeyDown (GameObject.Find("GameObjectPlayerOne").GetComponent(ScriptPlayerOneStats).playerOneControlJump)) {
		rigidbody.AddForce (Vector3.up * GameObject.Find("GameObjectPlayerOne").GetComponent(ScriptPlayerOneStats).playerOneJumpPower);
		yield WaitForSeconds(playerOneStats.playerOneJumpRate);
	} 
		else 
			{
				yield;
			}
	}
	
}

Rather than adding force, you can directly set the velocity. This way, the jump height is not dependent on the characters current force. (e.g. if the character is falling fast, a jump with AddForce would only reduce his falling speed, not actually jump)

Aaah that makes sense, I shall try to have a look at that now, thanks for the swift reply :slight_smile: