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
0
Question by archaismic · Oct 13, 2013 at 10:45 PM · inputwaitblock

Block specific input for amount of time

I need to block a specific input from being able to be used for a few seconds after it is initially used.

 #pragma strict
 
 //create variables for the force inputs
 var intYForce : int;
 var intXForce : int;
 var intZForce : int;
 //create variable for angstyness
 public var chuckingStrength : int;
 //define d20fixed as a rigid body so we can do physics stuff
 var d20fixed : Rigidbody;
 
 function Start(){
 
 //set initial value of chuckingstrength
 chuckingStrength=10;
 
 }
 function Update () {
     
 //throw the dice    
     if(Input.GetButtonUp("Jump")){
         
 //set randoms for force inputs        
         intXForce=Random.Range(-10,10);
         intYForce=Random.Range(1,10);
         intZForce=Random.Range(-10,10);
 //make sure they're not zero
         while(intXForce==0){
             intXForce=Random.Range(-10,10);
             }
         while(intZForce==0){
             intZForce=Random.Range(-10,10);            
             }
 //add force to the die
         d20fixed.AddForce(10+intXForce*chuckingStrength,20+intYForce*chuckingStrength,10+intZForce*chuckingStrength);
         d20fixed.useGravity=true;
         
 //reset the die
         Reload();
     }    
 
 //make it so when you press the up button angst goes up
     if(Input.GetButtonDown("up")){
     
         chuckingStrength = chuckingStrength + 1;
         if(chuckingStrength==21)  {
         chuckingStrength = 20;
         }
         }
 
 //make it so when you press the down button angst goes down
     if(Input.GetButtonDown("down")){
     
         chuckingStrength = chuckingStrength - 1;
         if(chuckingStrength==2)  {
         chuckingStrength = 3;
         }
         }
         
     
     
 }
 
 //function to wait 3 seconds and reload the level
 function Reload(){
     yield WaitForSeconds (3);
     Application.LoadLevel (Application.loadedLevel);
 }

Specifically, I need to make it so you can't press jump for a few seconds while the scene reloads.

I tried messing with the dead time of the input but it doesn't seem to be making a difference.

Obviously the yield waitforseconds isn't interrupting the space bar (jump) from being used while it's waiting.

I don't know where to go from here.

Comment
Add comment · Show 1
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 DylanW · Oct 13, 2013 at 11:10 PM 0
Share

Use time.deltaTime and a boolean variable.

3 Replies

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

Answer by aldonaletto · Oct 13, 2013 at 11:04 PM

Use a boolean that says when Jump is blocked:

 ...
 //define d20fixed as a rigid body so we can do physics stuff
 var d20fixed : Rigidbody;

 private var blocked = false; // create this variable
  
 function Start(){
 //set initial value of chuckingstrength
 chuckingStrength=10;
  
 }
 function Update () {
 //throw the dice - but only if not blocked!   
     if(!blocked && Input.GetButtonUp("Jump")){
     ...
 }
  
 //function to wait 3 seconds and reload the level
 function Reload(){
     blocked = true; // block the input
     yield WaitForSeconds (3);
     // unblock input - actually, this isn't necessary in this
     // case because the level is going to be reloaded anyway!
     blocked = false; 
     Application.LoadLevel (Application.loadedLevel);
 }
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 archaismic · Oct 13, 2013 at 11:20 PM 0
Share

thank you!! i didn't think to just turn it off and on like that

avatar image
1

Answer by Cherno · Oct 13, 2013 at 10:57 PM

Set a boolean like "DisableJumpInput" to true, and when pressing the jump button, check if the boolean is set to false before calling the actual jumping function.

To enable jump input again, you could just check in Update() if the boolean is set to true, and if yes, call a function that waits a few seconds and then sets teh boolean back to 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
1

Answer by DylanW · Oct 13, 2013 at 11:09 PM

Use time.deltaTime to check the amount of time that has passed by. Once it reached or passed a specific value, set a flag that "unblocks" the input.

Please read the comments.

 float waitTime = 10.0f;
 bool doneWaiting = false;
 void Update()
 {
     // Wait until waitTime is below or equal to zero.
     if(waitTime > 0) {
         waitTime -= Time.deltaTime;
     } else {
         // Done.
         doneWaiting = true;
     }
     
     // Only proceed if doneWaiting is true.
     if(doneWaiting && Input.GetButton("Jump")) {
         Debug.Log("Jumped!");
     }
 }
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

18 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

Related Questions

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

yield wait error iterator block 2 Answers

Help with waiting for input? I'm too used to how Console.ReadLine() works. 0 Answers

Reading Instance Variables Inside a Coroutine 2 Answers

Waiting for input using yield and coroutines 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