How to make a delay between each jump?
So, i want to make a slight delay between each jump for spam block and animation porpuses and i thought this would work but it doesn't, i mean there's no error but i can only jump one time, then it freezes on jumped = true.
using UnityEngine;
using System.Collections;
public class TestError : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
public bool jumped;
public float jumpdelay;
private bool grounded;
public void spamBlock()
{
StartCoroutine("SpamBlockco");
}
public IEnumerator SpamBlockco()
{
if (jumped == true)
{
yield return new WaitForSeconds(jumpdelay);
jumped = false;
}
}
// Use this for initialization
void Start ()
{
jumped = false;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space) && grounded && !jumped)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
jumped = true;
}
}
}
Please post the script as text ins$$anonymous$$d of an image. To do this use the 101/010 button above to properly format the code. Please read the user guide for more information.
Answer by TBruce · Nov 10, 2016 at 06:46 PM
This will make sure there is a delay before another jump can occur
using UnityEngine;
using System.Collections;
public class TestError : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
public bool jumped;
public float jumpdelay;
private bool grounded;
Rigidbody2D rigidbody;
// Use this for initialization
void Start ()
{
jumped = false;
rigidbody = GetComponent<Rigidbody2D>();
if (jumpdelay <= 0)
{
jumpdelay = 1;
}
}
// Update is called once per frame
void Update ()
{
if (grounded && !jumped && Input.GetKeyDown(KeyCode.Space))
{
rigidbody.velocity = new Vector2(rigidbody.velocity.x, jumpHeight);
jumped = true;
StartCoroutine(SpamBlockco());
}
}
public IEnumerator SpamBlockco()
{
if (jumped == true)
{
yield return new WaitForSeconds(jumpdelay);
}
yield return null;
jumped = false;
}
}
This made it work:
void Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && grounded && !jumped)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
jumped = true;
SpamBlock();
}
}
thank you!
Like you meant i just needed to call the coroutine whenever i jumped
Unless you are going to add more code to SpamBlock()
it is not necessary to have a separate function to encase the coroutine call. Also it is more efficient that you create a reference to the rigidbody in the Start()
function and then use the reference when needed ins$$anonymous$$d of continually getting it in the Update()
function.
Answer by Filhanteraren · Nov 10, 2016 at 06:34 PM
private float nextPress = 0f;
private float delay = 5f;
public void Update()
{
if (Time.time > nextPress)
{
nextPress = Time.time + delay;
}
}
okay, i think i get it, but how do i put that into my code?
Your answer
Follow this Question
Related Questions
Player in Unity refuses to Jump 0 Answers
How to put delay after input key 0 Answers
Jump not working 0 Answers
Need a key input delay,need an input delay 1 Answer
How do I get a delay after the Player is destroyed? 1 Answer