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 /
This question was closed Oct 30, 2013 at 06:10 PM by WhipJr for the following reason:

commands.close.hint.6

avatar image
0
Question by WhipJr · Oct 10, 2013 at 07:54 PM · keywhile

continue something if a key is still pressed down?

I'm trying to basically get this script to check (every frame) that the key is held down to run the if-then statements.currently it only checks on the frame that the button is pushed. I am wanting it to check every 3 seconds to run the if-then statements if someone could help me get started with that as well.

 using UnityEngine;
 using System.Collections;
 
 public class RandomBattles : MonoBehaviour {
     
 private int RandNum;
 public bool InBattle;
  
 // Use this for initialization
 void Start () {
  
 }
  
 // Update is called once per frame
 void Update () {
  
     RandNum = Random.Range(1, 1000);
  
     if (Input.GetKeyDown(KeyCode.W)||Input.GetKeyDown(KeyCode.S)){
     if(RandNum > 1){    
          if(RandNum < 1000){
        inBattle ();
          }
        }
     }
 }
 void inBattle () {
         Debug.Log ("battle");
         
     }
 }
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

  • Sort: 
avatar image
4
Best Answer

Answer by aldonaletto · Oct 10, 2013 at 07:57 PM

Use GetKey instead of GetKeyDown: GetKey returns true while the key is pressed.

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 WhipJr · Oct 10, 2013 at 08:14 PM 0
Share

that has solved that problem, any ideas on how i can keep track of the seconds that have passed, but only when the button is pressed down?

avatar image Doireth · Oct 10, 2013 at 08:36 PM 1
Share

When "On$$anonymous$$eyDown" record the time in a float variable with "Time.time". During the "On$$anonymous$$ey" event check when to stop action by comparing recorded time to current time (Time.time).

avatar image aldonaletto · Oct 11, 2013 at 12:42 AM 0
Share

As @Doreith said, you can use the Get$$anonymous$$eyDown and Get$$anonymous$$eyUp functions just to control start/stop ti$$anonymous$$g:

   float startTime; // declare this outside any function
 
   void Update () {
     if (Input.Get$$anonymous$$eyDown("w")||Input.Get$$anonymous$$eyDown("s")){
       // key pressed: save the current time
       startTime = Time.time;
     }
     if (Input.Get$$anonymous$$eyUp("w")||Input.Get$$anonymous$$eyDown("s")){
       // key released: measure the time
       float timePressed = Time.time - startTime;
       // do here whatever you want with timePressed
     }
     RandNum = Random.Range(1, 1000);
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)){
       if(RandNum > 1){    
         if(RandNum < 1000){
           inBattle ();
         }
       }
     }
   }
avatar image WhipJr · Oct 11, 2013 at 12:54 AM 0
Share

i actually had that all set up shortly after his response, thank you (:

avatar image raj727 · Jul 31, 2016 at 03:52 AM 0
Share

thanks buddy.. that helped me too !!

Show more comments

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

Unity keeps crashing when 'While' is used... 1 Answer

Input script 1 Answer

Weirdest coroutine crash ever 1 Answer

Keystore Problem 0 Answers

Coroutine, while, and Waitforseconds 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