- Home /
script for Jump help
I have the follwing part of a character controller code. The problem is that I have to press the jump button the hole time to perform a full jump. How i can make it, so when i press Space one time,it makes the whole jump?
if(Input.GetKey(KeyCode.Space))
{
transform.position += transform.up * +jumpspeed * Time.deltaTime;
Cam.transform.position += transform.up * +jumpspeed * Time.deltaTime;
}
Answer by Nercoe · Nov 03, 2012 at 10:45 AM
You can do this:
var jumpDelay : boolean;
var doubleJump : int = 0;
function Update()
{
if( Input.GetKeyDown(KeyCode.Space) && jumpDelay == false)
{
Jump();
}
}
function Jump()
{
if (doubleJump <= 1)
{
rigidbody.velocity.y = 175;
jumpTimer();
}
}
function jumpTimer()
{
if (Input.GetKeyDown(KeyCode.Space))
{
doubleJump ++;
}
if (doubleJump > 1)
{
doubleJump = 0;
jumpDelay = true;
yield WaitForSeconds(3);
jumpDelay = false;
}
}
Make sure you have a rigid body attached to your character when you do this. I will write you a jump timer now so that the player cannot jump repeatedly, unless that's what you want?
Answer by MountDoomTeam · Nov 02, 2012 at 08:04 PM
take this command from the unity reference http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html
with time.time, it makes the action repeat only if time + time duration has elapsed and button still pressed.
change the above code abit so that it jumps between the time you press jump and a time elapsed period after.
... make to variables, one = time when button was pressed and two = time when it was pressed + 2 seconds...
and say... if time.time is between time one and time two, jump
sorry i got the above answer wrong first time, it's correct now. although if you manage just with that info it means you already understand how to string 4 lines of code together :)
Answer by theclassicgamer132 · Mar 09, 2014 at 09:55 AM
i should make it: if(Input.GetKeyDown(KeyCode.Space))
instead of if(Input.GetKey(KeyCode.SPace))
Your answer
Follow this Question
Related Questions
Unexpected token? 1 Answer
Jump on button press. [JavaScript] 1 Answer
Simple wall jump 0 Answers
Setting Scroll View Width GUILayout 1 Answer
First time open error android game 0 Answers