My first scripting question - Press key to crouch, hold same key to prone and more.

Hey everyone, new guy here.
I did a search and didn’t find any results that helped me. Maybe my terminology is bad since I am still learning.

But here is what I am trying to accomplish.

Bare minimum: One player definable key to perform multiple character actions depending on whether it is being pressed or held.

Ideal: When letting a player map their own keys, have the option of a key being held. Like I want to jump when space is pressed, but I can go to my “Sprint” binding and hold space bar and it will say, ok, sprinting = spacebar-held.

What I want to accomplish as a bare minimum as this is really what I need it for but would love to allow any action to be mapped to a “held” button/key.

One key/button that has multiple uses depending on its state(?).

Setting the “B” button on a controller or “Q” on a keyboard to do the following:

Pressed(tap) while standing = Crouch
Pressed(tap) while crouching = Stand

Held while standing = Prone
Held while crouching = Prone

Pressed(tap) while proning = Crouch (double tap to stand since pressing while crouching makes you stand)


I know there will be a slight delay (I am assuming I can pick the length that determines a ‘held’ vs ‘press’)

I think Battlefield 3 has a similar set up.

Any advice and code examples would be greatly appreciated. I don’t want someone to write the entire script for me because I want to learn but a nudge, or a nice firm shove would work to, in the right direction would help a lot.

You already have the right idea, you just need to sit down and think about it yourself. When you write code you say that
if (this key pressed)
do this
But if you want to change the bindings, one way to do it would be to use variables in place of
ask player what they want for jump
jumpKey = whatever the player decided
if (jumpKey)
do whatever

the rest of it, regarding whether you are standing crouching etc will require you using bool’s
if you pressed the crouch button while standing, crouching = true
then have them execute whatever actions they can while they are in the crouch state
if you press the crouch button again, set crouch to false and standing to true
then they would go back to doing whatever they do while they stand.

Next time stop and think about the actions you want to complete.

I have this same issue, I feel like there should be a simple way, like a way to determine how long a button is pressed or held for, but yet I’ve been stuck on this issue about a week, any help appreciated