Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
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
Add comment
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

· Add your reply
  • Sort: 
avatar image
5
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
Add comment · 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
1

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
Add comment · 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
0

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
Add comment · 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

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

6 People are following this question.

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

Related Questions

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

Distribute terrain in zones 3 Answers

how to slow down speed increase? 2 Answers

Even / Constant Speed and Jumping 2D 1 Answer

Super Ghouls 'n Ghosts style jumps & double jumps? 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