Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by gospel_gamer · Sep 10, 2015 at 02:32 PM · c#animationboolslowontriggerstay

Bad Response Time for Input.GetKeyDown

So I've got a scene set up where a character walks over to a boundary zone and if they're within that zone (checked by OnTriggerStay) the player can push "e" (checked by Input.GetKeyDown)to animate a button pushing down (just a basic little thing with a cylinder moving down). The problem is, Unity is only recognizing me hitting "e" approximately 1/3 of the time. If I stand still and rapidly press the button, it will eventually go. Is there any way to tell by looking at the code below what is causing this to take so long? Any assistance would be greatly appreciated! :-)

TriggerBox (This is attached to the boundary zone - a child of the button prefab)

 public class TriggerBox : MonoBehaviour
 {
     public bool isActive = false;
     void OnTriggerStay(Collider other)
     {
         if (other.CompareTag("Player")) //Compare if the entering Collider belongs to "Player"
         {
             if (Input.GetKeyDown("e")) //If the above is true and the player selects "e", continue
             {
                 isActive = true; //gives a point of interaction between TriggerBox and ButtonPush
                 Debug.Log("isActive has been set to true");
             }
         }
     }
 }

ButtonPush (the script that is attached to the actual button)

 public class ButtonPush : MonoBehaviour {
     public GameObject otherGameObject;
     private TriggerBox triggerBox;
     private Animator thisButton;
     // Update is called once per frame
     void Start () 
     {
         triggerBox = otherGameObject.GetComponent<TriggerBox>();
         thisButton = GetComponent<Animator>();
     }
 
     void Update()
     {
         //This is designed to check once per frame if the boundary
         //zone's bool "isActive" is true - if so, it will run the
         //code to animate the button, then set the bool to false
         if (triggerBox.isActive == true)
         {
             thisButton.SetBool("buttonAnimPlay", true);
             Debug.Log("Animation has played.");
             triggerBox.isActive = false;
             Debug.Log("isActive has been set to false");
         }
     }
 
     void ButtonEnd() //Animation Event
     {
         thisButton.SetBool("buttonAnimPlay", false);
     }
 }
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
6
Best Answer

Answer by Positive7 · Sep 10, 2015 at 02:38 PM

Input should be called in Update() or Corutines only see doc : http://docs.unity3d.com/ScriptReference/Input.html

OnTrigger... is Physics based and its not getting called every frame. Similar to FixedUpdate() if I'm not mistaking that's why your button press is not registered.

Set up a bool and set it true in OnTriggerStay() Then In Update check if keydownn && bool is true e.g.:

 bool mybool;
 
     void OnTriggerStay(Collider other)
     {
         if (other.CompareTag ("Player")) {
             mybool = true;
         }
     }
 
     void Update(){
         if (Input.GetKeyDown ("e") && mybool) {
             isActive = true;
         }
     }


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 gospel_gamer · Sep 10, 2015 at 02:44 PM 0
Share

Just tried this out and got it working. Thanks! :-)

avatar image shawnsi · Jan 26, 2019 at 12:36 AM 0
Share

Good to know. Thanks @Positive7 !

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Animation won't stop (Solved) 2 Answers

On-click and trigger animation c# 1 Answer

SetBool animation state based on direction object is moving 0 Answers

Referenced Bool is not updating 1 Answer

Trying to animate using Breadcrumb(Asset) 0 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