- Home /
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;
}
}
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().
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!
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().
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!