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 cwkx · May 15, 2013 at 11:28 PM · guiinputeventtextfieldfocus

TextField, Event.current, Input.GetKey, and GUI.FocusControl locking

If I hold the RightArrow key to run right, then press Enter to start chatting, the running state:

if (Input.GetKey(KeyCode.RightArrow) { ... } // in Update()

gets locked until I press the RightArrow key again after ending the chat. Instead, I want to clear all Input states as soon as the chat TextField is focused, so that my character does not continue running later on. I'm handling the chat in OnGUI():

 if (Event.current.Equals(Event.KeyboardEvent("return"))) {
     chatEnter = !chatEnter;
 }
 if (chatEnter) {
     GUI.SetNextControlName("ChatField");
     chatString = GUI.TextField(..., chatString);
     GUI.FocusControl("ChatField");
 } else
     GUI.FocusControl("");

  • I need a way to clear the Input.GetKey state, or a workaround. I can't simply stop my character moving if (chatEnter) because the character then undesirably continues moving later (even after chatEnter = false;). I searched this problem, and while there are similar issues, most simply refer to Event.current which is what i'm using.

Edit: I solved this after debugging. You need to set the focus in the OnGUI Repaint pass; otherwise it steals the Update Input for some reason. If anyone could comment and expand the reasoning, it would be useful.

     if (chatEnter) {
         if (Event.current.type == EventType.Repaint) 
             GUI.FocusControl("ChatField");
     } else
         GUI.FocusControl("");
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 Aeron0 · May 16, 2013 at 12:03 AM

 //Can't you add a bool variable chatEnter in your movement code and OnGUI? 
 //something like
 
 // make the same bool variable available in both the movement code and OnGUI code
 bool chatEnter;
 
 // Wherever your movement code is located
 if(chatEnter == false)
   {
     // movement code goes here
   }
 
 // OnGUI code
 // You need code in OnGUI to make chatEnter true

 If(whatever code you have for here)
 {
   chatEnter = true;
 }
 
 if(chatEnter == true)
   {
 
   }

 // And you will need to set chatEnter back to false when chat closes
Comment
Add comment · Show 2 · 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 cwkx · May 16, 2013 at 09:17 AM 0
Share

I mentioned that I tried this - the problem is not of stopping the character moving - its of the Input.Get$$anonymous$$ey states locking as focus is shifted to the chat. If I do this approach, the character stops (when chatting) then undesirably continues moving after the chat closes.

avatar image Aeron0 · May 16, 2013 at 03:39 PM 0
Share

$$anonymous$$aybe this will help.

If you are using input for any kind of movement behaviour use Input.GetAxis. It gives you smoothed and configurable input that can be mapped to keyboard, joystick or mouse. Use Input.GetButton for action like events only. Don't use it for movement, Input.GetAxis will make the script code smaller and simpler.

Note also that the Input flags are not reset until "Update()", so its suggested you make all the Input Calls in the Update Loop.

Class Funtion:ResetInputAxes Resets all input. After ResetInputAxes all axes return to 0 and all buttons return to 0 for one frame.

unity3d: Input Get Axis

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

14 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

Related Questions

Emulate RightArrow GUI Event 0 Answers

Using GUI.FocusControl on TextField selects all text 4 Answers

Get key state while textfield has focus? 0 Answers

TextField not getting focus on iOS 1 Answer

Input not registering while TextField is selected 3 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