How to set up On click Run button

Hey guys I just need my character to sprint when I press the ‘r’ button and again stop running when I press the ‘r’ button again. By default I have to hold the w key to run but is there a way to change that. I am using Unreal Engine 5

Here’s how I do a toggle automove:

The premise is like we are simulating that player is holding down the MoveForward key. Just using tick to constantly feed a value of 1 into the scale of the movement input axis (probably you have this node which makes your character move on the character blueprint)

A gate is used to act as a toggle. Key inputs will call Open/Close custom events.

Besides the custom events which are really only used to keep a legible graph, the only special variable you’ll need is a boolean for “automoveOn”.

This custom event gets called from Tick:
image

It feeds a gate. Custom events to open and close the gate:

An input action opens the gate:

This part includes some of my project specific stuff. But for basic setup, you only need the select node pretty much:

  • if automove is on, a constant float value of 1 is fed into the AddMovement node (this is probably on your character blueprint

Thats very much useful but I am really confused can you please mention in which blueprint I should add this because I cannot find any function called or collapsed graph called Auto Move please be more specific in the locations of the blueprints

How about this then?:

On R Key (Pressed) → Flip Flop
-A = Get Character Movement - Set Max Walk Speed (1000)
-B = Get Character Movement - Set Max Walk Speed (600) (600 is default value)

Character Movement is a component allowing your Pawn to move, crouch, jump etc.
FlipFlow is a Macro that you can just find as a node - it works as a switch
R Key - either assign an “Action Mapping” or simply find it in Blueprint

Max Walk Speed is a part of Character Movement - you need to pull it out from there

And here a screenshot of what I mean:

1 Like

No this is not what I am looking for. I mean like If I just click R key then my player should run continously. Again If I press it My Player should stop Running. Your answer increases my player speed when pressed R key

Lol

It’s doing what you ask for

you press R you run, press again you walk

Or am I missing anything?

Animation is already prepared for that to play accordingly and numbers are just examples

did you even try the code or thought it’s wrong without testing and understanding it?

yup Just Tried it not working bro It just works in the way I mentioned before. May be you try it again.

then what is happening on your side?

I did test it cuz I had to make the code for screenshot

I press R for first time - I run
I press R for 2nd time - I walk

and this repeats for every next click

Actually I am developing a mobile game and I need to set up a button so that my player runs on clicking it and stops when clicked again. Very similar to sprint buttons used in games like pubg and free fire. Got it now?

good you mention now it’s a mobile game - it makes a difference

seriously then What am I missing…

Exactly when I touch that sprint Ui button I should run and When I touch it again I should stop

Soo, here’s your Setup:

You have a Button in your UI - make sure you use “On Pressed” not “On Clicked”

On Clicked = Press and Release and after the Release the code is running

And using same FlipFlow and Set Max Walk Speed you can change the run/walk mode without any extra Boolean

Here’s the UI BP:

And here a video (max walk speed when running is higher so you see the difference):

But want I want is I should not touch the joystick If I just press the button I should run forward thats it. Did you get it?

omg, yeah, didn’t knew there’s no run/walk but run/stop (it was my bad, now as I re-read your answer above you actually tell this xd)

one sec

In your Character - change the way you move forward from “InputAxis” to “Event Tick” or your custom Timer like that:

You need the Boolean now which I personally called the ‘Holding The Run Button’ - you can name it as you want

And in UI you don’t maintain Character Movement anymore - just the State of Boolean (Using On Pressed and On Released)

So what happens on Tick is checking wether you hold the button or not and depending on that - you run or not

1 Like


Made few Changes This is what I needed but great u did it. It worked !!!

1 Like