Need help figuring out how to set Movement speed multiple ways

So instead of having variables like Walk speed, Run speed, and Aim speed plugged directly into ‘Set Max Walk Speed’, Is there a way to have a variable of sorts that stores the walk, run, and aim speed variables? Then the one that stores all the others can have 1 instance where it plugs into ‘Set Max Walk Speed’?

Basically, my issue is that I have the Left Shift key setting the Max Walk Speed based off being pressed or released.

344333-aa1.png

I’m also setting up an Aiming speed, so that if the player is running or walking it slows them down in order to aim and shoot. But once the player stops aiming, theyre stuck in the aim speed. I’m sure it can have simple fix but in the long run, how can I solve this problem more efficiently?

(Aiming is set to the right mouse button and ‘Is Aiming?’ boolean is set by being pressed or released.)

Here are some steps you can take to guide you through setting this up, there are quite a few things you can do to help make this more efficient but hopefully, it gets you started.

  • Create a new enum for the different speed types, then create a new variable in your player’s blueprint, making its type the new enumeration you just created
  • Off of your keybindings set the enum value to be the movement type you want to use, making sure you use branches in case multiple buttons are held at the same time, I gave one example in the pictures below
  • Next, create a new function, and set it to “Pure” on the right-hand side, and make sure to add an output variable (float) then in the function use “Switch on Enum” (and select the enum you made for the speeds)
  • Then create a new local variable (float) and off of the different types set the float value to the speed you want to use based on the movement type, and off of each of the switch values connect the float to the output
  • Finally, all you have to do is off of “Event Tick” set the “Max Walk Speed” to the output value of the function

If that got too confusing here are some images to give you the basic idea of how to set it up, hope this helps :slight_smile:

344342-setspeed.png

Not sure how well this example code will hold up depending on how many different keys the player might be holding at once, but hopefully it at least gives you an idea of how to go about using enums for this, and feel free to message back if you have any issues with this code.

Ah, ENUMS! It was on the tip of my tounge but I couldn’t remember what it was. Thank you for this, I’ll see what I can do.

I do have another question tho. The whole reason I was looking for a more efficent way to do this was because of the aiming speed. So with the aiming speed, If the player is running or walking and decides to aim (RMB), the movement speed is decresed to slightly below walking speed. But when the player stops aiming, they are stuck in the slower speed untill they sprint (basically until they trigger a different max walk speed value). The simple fix would be to make the RMB release set the ‘Max Walk Speed’ back to the ‘Walking speed’ then from there they can sprint or just walk.

So I guess its not really that much of an issue rather than a quirk. Should the player have to repress the shift key in order to sprint once they are no longer aiming or should it just let them sprint if they release the RMB with the shift key still down.

Right now, if you are sprinting and press the RMB and release it you are no longer sprinting until you press it again.

Sorry for long post, talking it out lol

Lol, no worries, yea that makes sense. I knew that enums would help and wanted to provide an answer fairly quickly since I saw you’ve had this posted for 2 hours with no replies, but didn’t do as much testing with the code as I probably should have before sending. Hopefully, this steered you in the right direction and best of luck with your game!

On the second picture. What is connect to Types Enum?..