• 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 /
avatar image
Question by Jaidan · Jan 31, 2015 at 07:16 PM · c#2dtimejumpingdelay

Have a delay after each jump, so user cant spam jump

Hello, i was wondering how i could implement a sort of delay into my game so that after the user jumps, they cant immidiately jump straight after. My game is all in the air so i have no onGround or grounded function, and i just need to know how i would make it so that there is a delay in between jumps. I hope it make sense and thank you

My Code:

using UnityEngine; using System.Collections;

public class NewJump : MonoBehaviour {

 public int jumpHeight;
 float jumpSpeed;
 Vector3 velocity;
 
 void Start(){

     jumpSpeed = Mathf.Sqrt(-2*Physics.gravity.y*jumpHeight) + 0.1f;
 }
 
 void Update(){
     if (Input.GetMouseButtonDown(0)){

         velocity = rigidbody.velocity;
         velocity.y = jumpSpeed;
         rigidbody.velocity = velocity;
     }
 }

}

Comment
hako-975

People who like this

1 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by Mmmpies · Jan 31, 2015 at 08:11 PM

just set a Time.time to a bit in the future like this:

 private float canJump = 0f;
 
 // then in update
 
 if (Input.GetMouseButtonDown(0) && Time.time > canJump){
  
     velocity = rigidbody.velocity;
     velocity.y = jumpSpeed;
     rigidbody.velocity = velocity;
     canJump = Time.time + 1.5f;    // whatever time a jump takes
 }
Comment
extrablade
Joe365365
SetingDev
hako-975
simplify

People who like this

5 Show 0 · 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

Answer by Derek-Wong · Feb 01, 2015 at 07:23 AM

or you can simply set a bool

 private bool isJumping;
 
 private void jump(){
   if(!isJumping){
      isJumping = true;
      //jump here
      //reset isJumping after 1.5 sec
      invoke("resetIsJumping", 1.5f);
   }
 }
 
 private void resetIsJumping(){
   isJumping=false;
 }
 

Comment
Brunehaut

People who like this

1 Show 0 · 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

Answer by Shabby91 · Oct 04, 2017 at 09:11 AM

I had used a "timer" to let the addforce work otherwise the character has no time to jump because update start every frame. (timer>5 works fine for me and the delay is 1 sec). This is my code:

 private float jumpDelay=1f;
     public bool jumped;
     public int timer;


//bla bla bla

     void Start () {
         jumped = false;
    
         if (jumpDelay<=0)
         {
             jumpDelay = 1;
         }
     }
 
     private void Update()
     {
         if (grounded && Input.GetAxis("Jump") > 0 && !jumped)
         {
             grounded = false;
             myAnim.SetBool("isGrounded", grounded);
             myRB.AddForce(new Vector2(0, jumpHeight));
             timer += 1;
             if (timer > 5)
                 StartCoroutine(SpamBlockco());
             else
                 jumped = false;
         }
     }
 
     public IEnumerator SpamBlockco()
     {
         jumped = true;
             yield return new WaitForSeconds(jumpDelay);
         
         yield return null;
         jumped = false;
         timer = 0;
     }
Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Super Ghouls 'n Ghosts style jumps & double jumps? 1 Answer

Do something every 0.5 seconds 3 Answers

Character jumping at random heights. What in my code is causing this? 1 Answer

C#: Jump function, grounded stays True 1 Answer

2D Platformer Jump while Running Android 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