Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ritzilla · Nov 10, 2016 at 05:47 PM · jumpnot workingdelay

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;
         }
     }
 }
 

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TBruce · Nov 10, 2016 at 06:21 PM 0
Share

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.

avatar image Ritzilla TBruce · Nov 10, 2016 at 06:33 PM 0
Share

okay, thank you.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ritzilla · Nov 10, 2016 at 07:45 PM 0
Share

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!

avatar image Ritzilla Ritzilla · Nov 10, 2016 at 07:47 PM 0
Share

Like you meant i just needed to call the coroutine whenever i jumped

avatar image TBruce Ritzilla · Nov 10, 2016 at 07:51 PM 0
Share

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.

Show more comments
avatar image
1

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ritzilla · Nov 10, 2016 at 06:41 PM 0
Share

okay, i think i get it, but how do i put that into my code?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges