- Home /
Delay for jumping, I've looked everywhere.
Okay, I'm developing my second game, and it's a 2D game where I have a cube that needs to jump over a wall. I have my jump script and it works well, one problem, I can press the up arrow a million times and the cube will continue jumping. I'd like it to where when I press the up arrow it jumps once and then I have to wait a couple seconds before being able to press up arrow again. Here's my code. Thanks
pragma strict
ar speed : float = 10.0; var jumpHeight = 10.0;
function Update() { var translation : float = Input.GetAxis("Horizontal") speed; translation = Time.deltaTime; transform.Translate( translation, 0, 0);
if(Input.GetKeyDown(KeyCode.UpArrow))
{
transform.Translate(Vector3.up * jumpHeight * Time.deltaTime);
}
}
Answer by paulygons · Jul 21, 2013 at 02:49 AM
Sounds like you just need a timer of some sort. I tend to go with a boolean that I flip on (set to true) after the jump is executed, then start a coroutine that will yield for the amount of delay you need, then flips the boolean back off. Of course you'll need to add a check of the boolean in your if statement.
var spamBlocker : boolean;
if(Input.GetKeyDown(KeyCode.UpArrow) && spamBlocker == false)
{
transform.Translate(Vector3.up * jumpHeight * Time.deltaTime);
spamBlocker = true;
//start coroutine that yields then sets spamBlocker to false
}
Answer by superluigi · Jul 21, 2013 at 05:16 AM
I took the liberty to test this for you before posting it and it works.
var speed : float = 10.0;
var jumpHeight : float = 10.0;
var airTime : float = 0;
function Update()
{
var translation : float = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
transform.Translate(translation, 0, 0);
if (airTime > 0)
{
airTime -= Time.deltaTime;
}
TimedJumps();
}
function TimedJumps()
{
if (Input.GetKeyDown(KeyCode.UpArrow) && airTime <= 0)
{
airTime = .5;
transform.Translate(Vector3.up * jumpHeight * Time.deltaTime);
}
}
also translate is not the best way to move something because it with translate the moving object will ignore collisions. If there was a wall you could just walk through it with translate sometimes.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Parkour type game 1 Answer
Unity for 2d puzzle 0 Answers
When Gui Text Clicked, Quit Game 2 Answers
Simple jump code 2D - c# 1 Answer