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 /
avatar image
0
Question by impojr · Aug 26, 2017 at 01:53 PM · button trigger eventsupdate function

Moving Button Down Check from Update to separate Function

Currently, I am checking if buttons are being pressed in my update function - for some reason I just think that that is not the most efficient way to do thing.

Is there a way to morve checks for button presses to other functions? Checking for everything each frame seems like it could cause potential issues down the track.

     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.LeftShift)){
             selectedArrowPosition++;
 
             if (selectedArrowPosition >= ArrowTypes.Count){
                 selectedArrowPosition = 0;
             }
         }
 
         if (Input.GetButton ("Fire1")){
             //add speed to projectile if the fire button is held down
             if (chargeSpeed < MAXCHARGESPEED) {
                 chargeSpeed += 0.1f;
             }
         }
         
         if (Input.GetButtonUp ("Fire1")) {
             //...setting shoot direction
             Vector2 shootDirection = Input.mousePosition;
             shootDirection = Camera.main.ScreenToWorldPoint(shootDirection);
             shootDirection.x -= transform.position.x;
             shootDirection.y -= transform.position.y;
             
             GameObject arrow = Instantiate (Resources.Load (ArrowTypes[selectedArrowPosition])) as GameObject; //"BasicArrow"
             arrow.transform.position = firePosition.position;
             BasicArrow arrowScript = arrow.GetComponent<BasicArrow> ();
             arrowScript.chargeSpeed = chargeSpeed;
             arrowScript.shootDirection = shootDirection;
             if (player.transform.localScale.x < 0){
                 arrowScript.playerBackward = true;
             }
 
             //reset chargeSpeed
             chargeSpeed = 0;
         }
     }
Comment
Add comment · Show 2
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 MissingL_tter · Aug 26, 2017 at 02:13 PM 0
Share

@impojr

I hate to be the one to answer a question with a question, but when else would you do it?

If you need to know when the user started pushing a button and when they stopped, putting something in Update() allows us to know that we will check if the user did something every frame. If the game state is the same this frame as it was last frame, nothing happens. If the user did something to change the game state this frame, it will be shown after this iteration of Update().

avatar image impojr MissingL_tter · Aug 26, 2017 at 02:21 PM 0
Share

A part of that makes sense, but my reasoning was: $$anonymous$$y understanding was it wasn't something that needed to be checked each frame. For example, pressing down the left shift key - would there be a way to trigger a function on the button press, so ins$$anonymous$$d of checking it has been pressed each frame, I can save some time and only ever call that function on the trigger when pressed. But I see what you mean - if that's the general practice and how everyone does it then I can just continue doing it that way - no sweat!

1 Reply

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

Answer by MissingL_tter · Aug 26, 2017 at 09:26 PM

The most common approach that I've seen is to respond to input in Update(). In fact, Unity's own documentation gives an example doing just that. However, I do understand your concern. To my knowledge Unity simply doesn't handle events this way.

Considering the example you gave, I imagine it could get rather hard to deal with Shift key down in such a way. If pressing down the Shift key was bound to call Foo(), what would happen if you ever wanted it to call Bar()? Or if you didn't want it to call anything at all? If it worked this way you'd probably have to check several conditions each frame anyway but instead of adding one extra conditional you would be changing some assignment on the Shift key.

My point is it can get complicated pretty quickly and one extra conditional is probably cheaper than many other methods.

Unity does have an EventSystem in place for handling EventTriggers. Something like this can be very useful for organizing and handling callbacks to execute some code, but even it functions on Update().

Comment
Add comment · Show 1 · 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 impojr · Aug 27, 2017 at 12:27 AM 0
Share

Okay, I understand now - thank you! I'll take a look at EventSystems but it seems like I can just stick with what I have - thanks for explaining it for me!

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

68 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 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 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

Update function don't update 1 Answer

Character AI - Are Coroutines less heavy than using functions kept in update 2 Answers

My player passes through walls 5 Answers

why do these both codes work similarly when they are put inside update function... 3 Answers

How to hide a text and show another when Space bar is clicked?,How to hide text after pressing space? 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