Hi I’m fairly new to writing script with Unity. I’m trying to get a ball to jump more than once without stopping. The ball will jump just fine while moving left or right but has to stop before it can jump again. Can anybody suggest how I can fix this problem?
#pragma strict
var Jump : KeyCode;
var stop : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;
var speed : float = 1;
var jumpHeight : float = 250;
var grounded = false;
function Update ()
{
if (Input.GetKey(moveLeft))
{
GetComponent.<Rigidbody2D>().velocity.x = speed * -1;
if (Input.GetKey(Jump))
{
jump();
}
}
else if (Input.GetKey(moveRight))
{
GetComponent.<Rigidbody2D>().velocity.x = speed;
if (Input.GetKey(Jump))
{
jump();
}
}
else if (Input.GetKey(stop))
{
grounded = true;
GetComponent.<Rigidbody2D>().velocity.y = 0;
}
else if (Input.GetKey(Jump))
{
jump();
}
else
{
grounded = true;
GetComponent.<Rigidbody2D>().velocity.x = 0;
}
}
function OnCollisionEnter(hit : Collision)
{
grounded = true;
}
function jump()
{
if (grounded == true)
{
GetComponent.<Rigidbody2D>().AddForce(Vector3.up * jumpHeight);
grounded = false;
}
}