How to change Max Walk Speed in C++?

Hello.
I made a simple Player Class and I want to add sprint mechanic.
I already binded it to functions called “StartSprint” and “StopSprint”, so my question is what’s the name of the variable or function that controls Max Walk Speed.

Check out the character movement actor component… That has all sorts of stuff (like walking) that will assist… Here’s the documentation. UCharacterMovementComponent | Unreal Engine Documentation

It’s “MaxWalkSpeed” of “UCharacterMovementComponent”.

Ok, but how do I write it? What’s the name of the default UCharacterMovementComponent?

Try this…

http://droneah.com/content/ue4-getting-sprint-work-c

Check the answer by @teak421. But replace in code -&gt with -> (hope that will be obvious).

When I do what you’re saying it gives me an error:

Don’t know how You managed to do that, since MaxWalkSpeed is public variable.

If You’re new to c++ You better to study it first. There’s a good Unreal c++ course by Ben Tristem: https://www.udemy.com/unrealcourse/ (It’s only $15 now)

The UCharacterMovementComponent, CharacterMovement, to which MaxWalkSpeed belongs, is private, though. The only way I’ve figured out how to access this directly in my own Character C++ class inheriting from ACharacter is something like this:

Cast(GetMovementComponent())->MaxWalkSpeed = 100.0f;

1 Like

You can modify the MaxWalkSpeed through:

UCharacterMovementComponent* CharacterMovement = GetCharacterMovement();

float WalkSpeed = CharacterMovement->MaxWalkSpeed;
CharacterMovement()->MaxWalkSpeed = WalkSpeed*2

This solution does not work. There is no function definition.

I did it myself on 4.16 and this does work. From where did you try to access MaxWalkSpeed?

Put this in your Header:
class UCharacterMovementComponent* CharacterMovement = GetCharacterMovement();

Put this in your Cpp:
#include “GameFramework/CharacterMovementComponent.h”

float WalkSpeed = CharacterMovement->MaxWalkSpeed;
CharacterMovement()->MaxWalkSpeed = WalkSpeed*2;

This works 4.18

GetMovementComponent() returns a UPawnMovementComponent, not a UCharacterMovementComponent. To get the character one, you can either cast to it or just call GetCharacterMovement().

And don’t forget to include the header:
#include "GameFramework/CharacterMovementComponent.h"

3 Likes

#include “GameFramework/CharacterMovementComponent.h”

	UCharacterMovementComponent *MovementPtr =  Cast<UCharacterMovementComponent>(Character->GetCharacterMovement());


	MovementPtr->MaxWalkSpeed = Speed;

for multiplayer check out shooter game