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 SavvaMadar · Feb 15, 2014 at 04:01 PM · key pressedpressing

Need delayed reaction after key press

I need to do a delayed reaction after a key press, here is my current setup:

 function turn(dir:float)
 {
 if(collided==false){
 if(dir==-1){
     transform.Translate(Vector3.right * Time.deltaTime * -1.50);
 }
 else if(dir==1){
     transform.Translate(Vector3.right * Time.deltaTime * 1.50);
 }
 }
 }

 timer = Random.Range(0.45,0.95);
 //timerGo() is just WaitForSeconds(timer)
 //turn() translates the object left or right by 3


 if (Input.GetKey(KeyCode.A)){
      timerGo(timer);
      turn(-1);
 }
 if (Input.GetKey(KeyCode.D)){
      timerGo(timer);
      turn(1);
 }


I need the delay to happen and then the move for as long as the user holds the key. However currently whats happening is when the user presses a key it starts the timer after it finishes it moves translates over 1.5. Then the timer starts again with a new value and translates over 1.5, and again, and again.

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 erick_weil · Feb 15, 2014 at 04:57 PM

 //tryout this:
 var countdown : float;
 var timer : float;
 function turn(dir:float)
 {
 if(collided==false){
 if(dir==-1){
 transform.Translate(Vector3.right * Time.deltaTime * -1.50);
 }
 else if(dir==1){
 transform.Translate(Vector3.right * Time.deltaTime * 1.50);
 }
 }
 }
 
 function Update()
 { 
    
 
 //now every key press a new delaytime will be set
  if(Input.GetKeyDown(KeyCode.A)||Input.GetKeyDown(KeyCode.D)||Input.GetKeyUp(KeyCode.A)||Input.GetKeyUp(KeyCode.D))
  {
  timer = Random.Range(0.45,0.95);
  countdown = 0;
  }
  
  
 if (Input.GetKey(KeyCode.A)){
      countdown += Time.deltaTime;
      if(countdown >timer)
      turn(-1);
 }
 
 if (Input.GetKey(KeyCode.D)){
      countdown += Time.deltaTime;
      if(countdown >timer)
      turn(1);
 }
 
 
 
 
 }


if you see, I don't used your timerGo() function, waht happens now is a delay before execute the action of turn to left or turn to right

Comment
Add comment · Show 6 · 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 SavvaMadar · Feb 15, 2014 at 05:46 PM 0
Share

I actually tried a similar solution, and it didn't work. I just tried yours and it also doesn't work, now there is no delay. If I Debug.Log(countdown) it just says 0 one time and that's it.

avatar image erick_weil · Feb 15, 2014 at 07:57 PM 0
Share

this worked fine to me... you have missed the update function? see tha in my code, the countdown is into update()

avatar image SavvaMadar · Feb 15, 2014 at 10:44 PM 0
Share

I copy pasted yours and double checked it.

avatar image erick_weil · Feb 16, 2014 at 03:09 PM 0
Share

you probably is missing the variables declaration on top:

 //tryout this:
 var countdown : float;
 var timer : float;
 .
 .
 .


this NEED to works, it worked all fine to me

avatar image SavvaMadar · Feb 16, 2014 at 09:11 PM 1
Share

Oddly enough I did have it but I re copy and pasted your and it started working, i guess I missed something. But I ended up removing that mechanic from the game because it became to hard.

I'd appreciate it if you tried it out!

http://dl.dropboxusercontent.com/u/268133971/DrunkDriving/DrunkDriving.html

Show more comments

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to add a cooldown to a move. 1 Answer

How to make my game objects spawn when i walk through collider 3 Answers

How Do I Make A Health Bar 6 Answers

GUI.Box that will appear for 3sec 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