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 /
  • Help Room /
avatar image
0
Question by beepb33p · Apr 01, 2021 at 10:48 PM · methods

How do I limit the number of times my player can jump while also using a 2 second timer cool down?

So I'm working on a little sandbox project game (Following a tutorial/class online, but I'd like to change things up and add my own flavor to it).

This is my first time trying to make a game as well as my first time coding. Complete beginner , any advice/tips would be highly appreciated

The game is like a 3d platformer, I'd like to limit the amount of jumps the player is able to perform while in mid air. (which is most of the game)

I'd like the player to be able to jump maybe twice or three times. (would prefer to serialize a value so I could maybe change it in game for both the time and amount of jumps) I'm just stuck on how to actually implement the methods.

How can I implement a 2 second cool down delay on jumping? Currently the players able to jump infinitely.

Here's the code i have so far is C#


using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Movement : MonoBehaviour {

 [SerializeField] float mainThrust = 100f;
 [SerializeField] float rotationThrust = 1f;
 [SerializeField] AudioClip jump;
 Rigidbody rb;
 AudioSource audioSource; 

 // Start is called before the first frame update
 void Start()
 {
    rb = GetComponent<Rigidbody>();
    audioSource = GetComponent<AudioSource>();
 }

 // Update is called once per frame
 void Update()
 {
     ProcessThrust();
     ProcessRotation();
 }

 void ProcessThrust()
 {
     if (Input.GetKey(KeyCode.Space))
     {
         rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
         if(!audioSource.isPlaying)
         {
             audioSource.PlayOneShot(jump);
         }
     }
     else
     {
         audioSource.Stop();

     }
 }

 void ProcessRotation()
 {
     if (Input.GetKey(KeyCode.A))
     {
         ApplyRotation(rotationThrust);
     }
     else if (Input.GetKey(KeyCode.D))
     {
         ApplyRotation(-rotationThrust);
     }
 }

 void ApplyRotation(float rotationThisFrame)
 {
     rb.freezeRotation = true; //freezing roation so we can manually rotate
     transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
     rb.freezeRotation = false; //unfreezing 
 }

}


Thanks for your time and help.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Apr 01, 2021 at 11:28 PM

I would suggest using a coroutine for the cool down which sets a canJump bool to false while waiting: using System.Collections;

 public class Movement : MonoBehaviour
 {
 bool canJump = true;
 
 int maxJumps = 3;
 int currentJump = 0;
 
 private void Update()
 {
 
     if (Input.GetKey(KeyCode.Space) && canJump)
     {
         //your jump code goes in here along with the below code
         currentJump++;
         
         if(currentJump == maxJumps)
         {
             StartCoroutine(CoolDown());
         }
     }
 }
 private IEnumerator CoolDown()
 {
     currentJump = 0;
     canJump = false;
     yield return new WaitForSeconds(2);
     canJump = true;
 }
 }
 

That is just a basic example, but I hope you get the idea.

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 beepb33p · Apr 02, 2021 at 05:28 PM 0
Share

Thanks! Trying to make it work using this. I'll post an update after i get the script to function properly.

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

157 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 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

Calling a method twice in Start without overwriting 0 Answers

How can I add functions via a scriptswizzard variable? 0 Answers

Interval of x seconds between two method invokes. 0 Answers

Problems with Intellisense,Intellisense for Unity specific components etc. 0 Answers

Unity returns InvalidCastException when attempting to run a method from another file 2 Answers


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