- Home /
need help with restricting the key press
i m just developing a simple 2d game...the problem is that the character needs to jump only once i.e if i press jump more than once it should restrict the key press and execute the command of jump only once until the character is not on the ground or platform again...is there any way to do it...the script i m using now is...
var down=false;
function OnCollisionEnter(coll:Collision)
{
if(coll.gameObject.name=="FLOOR")
{
down=true;
}
}
function Update() { if(down==true)
{
if(Input.GetButtonDown("jump"))
{
Debug.Log("ON THE FLOOR");
rigidbody.AddForce(Vector3(0,505,0));
}
}
if(transform.rotation.z!=0)
{
transform.rotation.z=0;
}
}
I think you are on the right lines, try just adding down = false; on the line after 'rigidbody.AddForce(Vector3(0,505,0));' as that will mean when you jump if will say you are not on the ground and then if your scene is set up right when you hit the ground again it will set it to true. also a few tips, ins$$anonymous$$d of writing if(down==true) you can just write if(down){ they mean the same. It looks like you have formatted your code on this site by hand, in future you can just highlight it and hit the little 101010 button to automatically format code.
Your answer
Follow this Question
Related Questions
Head bob script overrides ability to jump,Not able to jump with Head bobbing script 1 Answer
Jumping to high 0 Answers
Multiple keys not detected at once 1 Answer
Making a character to glide 2 Answers
Namespace error with my jump code 1 Answer