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 SnotE101 · Mar 26, 2014 at 07:24 PM · c#collisioninput.getkeywtf

Input.GetKey(KeyCode.E) Not Working

I have a situation where when the player collides with a tree it has the option to select "E" and wood will be added to the inventory. This is my script:

 void  OnTriggerEnter (Collider other)
     {
         if (other.tag == "Tree")
         {
             Debug.Log ("hit");
             if(Input.GetKey(KeyCode.E))
             {
                 AddItem (2);
                 Debug.Log ("wood added");
             }
         }
     }

The first debug, "hit" responds but the second does not. The AddItem (2) command works because I inserted before (Input.GetKey(KeyCode.E)). What am I doing wrong? Is Input.GetKey(KeyCode.E) incorrect?

Thanks in advanced

Comment
Add comment · Show 5
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 perchik · Mar 26, 2014 at 07:24 PM 2
Share

I believe Input.Get$$anonymous$$ey* can only be used in Update

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

avatar image Destran · Mar 26, 2014 at 07:31 PM 0
Share

@perchik is right. From the Input Reference

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

https://docs.unity3d.com/Documentation/ScriptReference/Input.html

avatar image aballif Destran · May 09, 2016 at 10:14 PM 0
Share

I am doing the same thing. How would I enter a door by clicking or pressing a button when I am close if I don't put it in OnTriggerEnter?

avatar image Eno-Khaon aballif · May 09, 2016 at 11:03 PM 1
Share

Ideally, you would still want a trigger region to handle OnTriggerEnter. Then, while you're in that region, you can open the door with a button press.

 // C#
 bool inRange = false;
 
 // Walk up to the door
 void OnTriggerEnter(Collider other)
 {
     if(other.CompareTag("Player"))
     {
         inRange = true;
     }
 
 }
 
 // Walk away from the door
 void OnTriggerExit(Collider other)
 {
     if(other.CompareTag("Player"))
     {
         inRange = false;
     }
 
 }
 
 void Update()
 {
     if(inRange && Input.Get$$anonymous$$eyDown($$anonymous$$eycode.E))
     {
         // Open door here
     }
 
 }
Show more comments

1 Reply

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

Answer by Leuthil · Mar 26, 2014 at 07:48 PM

To perchik's comment, you will want to set a boolean which says when a tree was hit within that frame from OnTriggerEnter.

 private bool treeHit = false;
 
 void  OnTriggerEnter (Collider other)
 {
     if (other.tag == "Tree")
     {
         Debug.Log ("hit");
         treeHit = true;
     }
 }
 
 void Update()
 {
     if (treeHit && Input.GetKey(KeyCode.E))
     {
         Debug.Log("wood added");
         treeHit = 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

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Displaying text on trigger enter 1 Answer

C# Plane Collision Detection 1 Answer

OnCollisionStay only detects collisions after a secondary collision 0 Answers

Newb Question: Are there any limitations with creating a single script for controlling all game collisions 1 Answer

Movement around a huge object by avoiding obstacles 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