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 Esthernia · Feb 04, 2014 at 04:32 PM · c#inputkeyboardkeykeycode

Key not recognized

Hello,

I have a problem with the Input.KeyCode command. I need to set an action on a specific key, (which in my case is "c"), but it doesn't respond to it. The code is not wrong since when I tested it with "d" or "q", which are used for moving the character and thus are naturally recognized by the program, it worked. But when I use a key that is not used by default, like "y" or "c", nothing happens.

here's my code :

 using UnityEngine;
 using System.Collections;
 
 public class Img10Accroupi : MonoBehaviour {
 
     private GameObject image;
     Texture3D texture;
     GUITexture guitexture;
         
     public float timer = 0.0f;
     
     public bool imageAffichee ;
 
     
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnTriggerEnter (Collider other) 
     {
         if (imageAffichee == false){
             if(other.tag == "Player" ) {        
                 
                 image = new GameObject("img");  
                 image.AddComponent("GUITexture");
                 image.guiTexture.texture = Resources.Load("10Accroupi") as Texture;
                 image.transform.position = new Vector3(0.5f, 0.2f, 0.5f);
                 image.transform.localScale = new Vector3(0.5F, 0.30F, 0.5F);
                 
                 imageAffichee = true ;    
             }
         }    
         
         if (imageAffichee == true)
         {    
             if (Input.GetKey("c")) 
             {
                 timer += Time.deltaTime;
                 if (timer >= 2.0) 
                 {
                     Destroy (gameObject);    
                 }
             }
         }
     }
 }

for the line "if (Input.GetKey("c"))", I also tried "if (Input.GetKey(KeyCode.C)).

Thank you for your answers !

Comment
Add comment · Show 3
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 Ibzy · Feb 04, 2014 at 05:12 PM 0
Share

To help debug this, I would suggest putting a Debug.Log() message at each stage to see where this is falling over. I appreciate you say it works with any other key, but there could be an underlying problem which is bypassed by the fact that d and q are used elsewhere.

I'd suggest one at the start of each IF statement for starters.

avatar image fafase · Feb 04, 2014 at 05:17 PM 0
Share

@$$anonymous$$ortoc, it i the other way around, GetXXXDown/Up only returns true on the frame you are pressing/releasing while GetXXX returns true as long as you press.

avatar image Mortoc · Feb 04, 2014 at 05:30 PM 0
Share

Yup, I had it backwards, already deleted the comment when I went to double check.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Feb 04, 2014 at 05:22 PM

I think you have a logic error here rather than issues with GetKey(). You are getting your key inside OnTriggerEnter(). This function will be called only once for each collision. Assuming a frame rate of 60 fps, that means you will have to have around 120 different collisions with the 'C' key held down before your game object is destroyed. Put a Debug.Log() statement between lines 42 and lines 43. You should see this body of code fire once for each collision. I'm not sure of your game mechanic is here, but you may want OnTriggerStay().

Comment
Add comment · Show 3 · 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 Esthernia · Feb 04, 2014 at 07:54 PM 0
Share

Thanks for these quick answers. But this code works with the keys "z" "q" "s" "d" ins$$anonymous$$d of the "c" so I don't think there's a probleme with the Get$$anonymous$$eyDOWN . I'll try the Debug.Log and hope to find the answer!

avatar image robertbu · Feb 04, 2014 at 08:00 PM 0
Share

The code above use 'Get$$anonymous$$ey()', not 'Get$$anonymous$$eyDown()'. Using Get$$anonymous$$eyDown() would make the problem even worse. There is a potential issue here since I believe the OnTrigger*() callbacks works on fixedTimestep, so it is possible for Get$$anonymous$$eyDown() event to be missed, but that should happen for all keys, not just for the 'c' key.

avatar image fafase · Feb 04, 2014 at 08:07 PM 0
Share

Indeed Collision is part of the Physics of Unity which is run with the Unity framerate while the Input is taken care by the OS. Your solution is to detect the Input in the Update and set a variable with it. According to Unity docs, Physics is done before the Update. So this below should work as OnTriggerEnter, is called should happen before Update, so your boolean set in the previous frame should work.

 bool inputPressed;
 void Update()
 {
    inputPressed = false;
    if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.C))inputPressed = true;
 } 
 
 void OnTriggerEnter(Collider col)
 {
    if (imageAffichee == true && inputPressed)// Do something
 }
avatar image
0

Answer by Esthernia · Feb 04, 2014 at 08:42 PM

After testing a few things, adding a public bool to see where the problem was and make some tests again, it finally works!

Thanks a lot for your help !

Here's the final code :

 using UnityEngine;
 using System.Collections;
 
 public class Img10Accroupi : MonoBehaviour {
 
     private GameObject image;
     Texture3D texture;
     GUITexture guitexture;
         
     public float timer = 0.0f;
     
     public bool triggerEntree ;
     public bool destruction;
 
     
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
                 
         if (triggerEntree == true)
         {    
             if (Input.GetKey("c")) 
             {
                 destruction = true;
             }
         }
         
         if (destruction == true )
         {    
             timer += Time.deltaTime;
             if (timer >= 2.0) 
             {
                 Destroy (GameObject.Find("img"));    
             }    
         }
     
     }
     
     void OnTriggerEnter (Collider other) {
         if (triggerEntree == false){
             if(other.tag == "Player" ) {        
                 
                 image = new GameObject("img");  
                 image.AddComponent("GUITexture");
                 image.guiTexture.texture = Resources.Load("10Accroupi") as Texture;
                 image.transform.position = new Vector3(0.5f, 0.2f, 0.5f);
                 image.transform.localScale = new Vector3(0.5F, 0.30F, 0.5F);
                 
                 triggerEntree = true ;    
             }
         }    
     }
 }
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

20 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

Related Questions

Is it possible to change keyboard input inGame 1 Answer

No keyboard events detected after any key is held. 0 Answers

Key binding screen 0 Answers

Check the latest key pressed? 2 Answers

Turn Currently Held Button To A KeyCode(C#) 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