- Home /
so im making a game and i wondering how would i put a delay between jumps
// Update is called once per frame void FixedUpdate () { rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey ("d"))
{
rb.AddForce(forwardD * Time.deltaTime, 0, 0 );
}
if (Input.GetKey ("a"))
{
rb.AddForce(forwardA * Time.deltaTime, 0, 0 );
}
if (Input.GetKey ("w"))
{
rb.AddForce(0, forwardw * Time.deltaTime, 0 );
}
}
Answer by Honorsoft · Jan 09, 2018 at 04:27 AM
You could use Unity's built-in "yield return new WaitForSeconds(SECONDS);" function, but it has to used in an IEnumerator function (Coroutine). The example for Coroutines/IEnumerators is at: UnityDocs:WaitForSeconds However, for jumping, a better method is to set up a Boolean (true/false) to see if the player is allowed to jump yet or if it is already jumping. Or, keep track of whether the player "canJump" or not. I am guessing you want to delay the time between jumps because you don't want to try to jump while you are already jumping. So you could declare bool canJump = true;
at the start of your script with the other variables, and then change the if (Input.GetKey ("w"))
into if (Input.GetKey ("w") && canJump = true)
You want to know when you are able to jump at all times so when you do JUMP, in the start of the JUMP function, set the bool canJump to false. When finished jumping, set it to true so you can jump again.
If you want to know when the player has finished jumping, there are many methods like timers or player-position checking, but a good way is to determine when you've landed. That can be done with collision checking in Unity. It's not hard, use the void OnCollisionEnter(Collision collision)
and void OnCollisionExit(Collision collision)
functions to know when a collision has taken place and when one has stopped(Exit). There is someone else working on a similar jumping script: Jumping In Mid Air
PS, The way your script is now, the jump button (w) will function like a rocket, it is called every frame from Update() so every frame it's pressed will add upwards force.
Oh yeah, the AddForce function has a Force$$anonymous$$ode, you can use the Impulse (see Unity's script documentation) mode to send a quick burst of force that will have more of a jump effect. I've noticed though that using AddForce physics to make an object 'jump' will also cause random turning and twisting even on a flat 3D surface, so a controlled scripted 'jump', or an animated 'jump' in Unity might be more suitable.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150118im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Prior Knowledge 0 Answers
Make a game in C# 0 Answers
How to start in Unity? 1 Answer
Mecanim Jump up HELP!! 1 Answer