- Home /
Create a little time pause just after jumping
I would like to initiate a timer that would last 1.5 seconds just after the player pressed the "Jump" key. He will be able to move to the left / right during those 1.5 seconds, but it would be forbidden after it to avoid gameplay mechs breaks and stuff.
I found a way that would be to create a Coroutine like that:
IEnumerator RedFlash() {
yield return new WaitForSeconds (1.5f);
}
But I'm not sure if it's ok at all. It seems a bit too easy.
Answer by tormentoarmagedoom · Sep 28, 2017 at 08:14 AM
Good day @Midane53 !!
If you want to execute a order in a determinated time, maybe you should use Invoke method. You can invoke a method without parameters in the same script after determinated time. Like this:
Invoke ("Jumping", 1.5f)
void Jumping()
{
Jumpcommand;
}
Please mark answer as correct :D
Bye :D :
Note that the user can press jump multiple times in quick succession and you would perform the same number of "jumps" after that 1.5 sec delay.
You may want to do
bool jumping = false;
public void Jump()
{
if (!jumping) {
jumping = true;
Invoke ("Jumping", 1.5f)
}
}
void Jumping()
{
jumping = false;
// jumping here
}
Answer by Bunny83 · Sep 30, 2017 at 10:32 AM
An alternative would be to handle all the jumping code inside a coroutine:
void Start()
{
StartCoroutine(JumpHandling());
}
IEnumerator JumpHandling()
{
while (true)
{
// wait for the jump key to be pressed
while (! Input.GetKeyDown(yourJumpKey))
yield return null;
// when jump has been pressed, wait 1.5 seconds
yield return new WaitForSeconds(1.5f);
// apply the actual jumping here.
// Maybe set a flag that you just jumped to block the normal movement
// this would be reset when you land after the jump (OnCollisionEnter or something similar)
}
}
Your answer
Follow this Question
Related Questions
How to make a chronometer ?? 2 Answers
CountDownTimer Help 1 Answer
Problem with 2D movment system C#. Keeps moving when no command is given 0 Answers
Error while spawning multiple objects 1 Answer
Unintended bounce on 2D platformer 1 Answer