I’m making a simple 2D game.I am trying to make my character jump properly. At the moment when i press and hold down spacebar the character continuously flys upwards.
#pragma strict
var moveLeft : KeyCode;
var moveRight : KeyCode;
var Jump : KeyCode;
var speed : float = 4;
var JumpHeight : float = 10;
function Update ()
{
if (Input.GetKey(moveLeft))
{
rigidbody2D.velocity.x = speed *-1;
}
else if (Input.GetKey(moveRight))
{
rigidbody2D.velocity.x = speed;
}
else
{
rigidbody2D.velocity.y = 0;
}
if (Input.GetKey(Jump))
{
rigidbody2D.velocity.y = JumpHeight;
}
else if (Input.GetKey(Jump))
{
rigidbody2D.velocity.y = speed;
}
}