Hold down button for strength throw

Hi all,

currently my player throws granades, but its always the same direction with the same power. Would be nice for the player to adjust the throwing power depending on how long he holds the button down. a tap would drop it just by his beet and then holding it down for 1 and a half seconds would be maximum.

What button function would you use to accomplish this?

Each frame the button is pressed increase your throw force such that you go from min to max over some duration, like 1.5 seconds. When you release the button throw the grenade using that throwforce and reset it back to 0.

1 Like

Don’t drop it by his beet! That would hurt!!

Yes, like Yoreki says, you need to track how long the button is held and increment your notion of fling velocity.

Normal Unity UI buttons cannot do this alone… to do this you need to add a component called EventTrigger to them. This lets you track individual down (and up) events on the button, which you would use to start charging the grenade, and then release it, respectively.

I also like your spaceman-blown-to-bits animation in your gameplay video. Cool! CHOOF!

1 Like

Yes that rings a bell have done something like that before using event triggers, think it was for a thruster, thanks, thats put me on the right path!

1 Like

My bad, i thought OP meant mouse buttons or something, not UI buttons!

Im using a UI button.
so when i hold the button down it should time from 0 to 1.5 seconds maximum then throw the granade accordingly

so i need an event button down and up, but how do i work out the length of time the button is held down?

Thanks for replying.

  1. On down zero out a float timer

  2. In Update() always count up on the float by Time.deltaTime

  3. On up, look at the timer value, clamp it between 0 and 1.5f, use that to fling!

  4. Get your beet out of the way.

1 Like

Hi Kurt

Sorry just to clarify

So i need 2 functions right.

1 called GranadeButtonUp and GranadeButtonDown

then point my event trigger pointer up and down to their respective functions.

then have a float variable called downGranadeButtonTimer set to 1.5f

in the GranadeButtonDown function have
downGranadeButtonTimer -= Time.deltatime;

on the up function im not really sure what to do.

Sorry just getting a layout as i hate chasing my tail for hours not knowing if im even going in the right direction.

I have tried above and it doesnt seem to minus time from the counter holding the button, only on each click.

Go back to my list above… I said count UP

Upon release, use the value from 0.0 seconds to 1.5 seconds (or whatever range you want) to calculate a value for how hard to toss, something like.

ThrowPower = BaseThrowPower + (TimeHeldDown * AdditionalThrowPower) / MaxTimeHoldable;

Cool got it working!!! very nice touch!!! way better controlling grenade throws! next: Adding some sounds. As usual thanks Kurt.

int beersOwedToKurt = 12233;

2 Likes