Detect slope steepness and adjust speed accordingly?

So I’ve been playing around with the default Third Person template and made a basic sprinting system. It’s basically a button which is pressed to lerp from 300 to 600 and reverses when the button is released. Pretty straight forward.

However I’ve recently implemented a landscape and found it very immersion breaking to be able to sprint up a hill at the same speed as a flat plane, and similar when racing down a mountain.
I was wondering if there was a way to detect a slope that the character is ascending/descending and adjust the speed accordingly, to allow for different speeds on different slopes.

Any help or pointers is greatly appreciated :smiley:

in CharacterMovement
set MaintainHorizontalGroundVelocity to false

It’s not quite what I was looking for, I mean literally a way to change the Max Speed variable or something similar because with MaintainHorizontolGroundVelocity to false he still sprints up hills.

characters don’t care about gravity when they are on the ground.

in C++ you could override UCharacterMovementComponent::SimulateMovement() on a child class of character, then you can tell it to apply gravity to velocity whether you are walking or not.

or if you want to use only blueprints, you could turn off gravity and make your own gravity by launching the character on tick in a negative Z direction. since your gravity applies whether walking or not, it should adjust your speed on slopes.

Yeah thats true, although wouldnt that cause the character to slide when not moving?

once they reach level ground they would stop sliding. for walking characters, you could set up a minimum amount of force the gravity needs to apply to overcome ground friction, and it should depend on what state your character is in, how fast they are already moving, and the friction coefficient of the ground physics material. a character that was ragdolling should slide down a hill that they would normally be able to stand on. but this depends entirely on the design of your game, because there is always a trade off between tightly controlling a character and having physics control a character. in a snowboarding/skateboarding game, you have less ground friction to overcome if your board is parallel to your direction of travel.

Yeah, while its a cool concept it’s not really what I’m aiming for, thanks anyway.

I know this is an old post but I stumbled upon it and wanted to add in my answer as I needed to do the same.

[Slope Speed Video][1]

Check it out my tutorial on it here:

[Slope Speed Tutorial][2]

There might be a better solution out there but this is how I ended up doing it with Blueprints.

A quick rundown of the tutorial.

Each tick I check if the player is moving at all and if they are on the ground to make sure we are actually needing to check for the slope angle and set the speed at all.

If we do, next we take the the character’s velocity and use it to do two line traces - One starts where the character is and goes straight down to get the point of impact below their feet. The second starts out in the direction of movement by using the velocity and again goes straight down to get that location. We take the two locations and use the Look At Rotation to get the pitch!

Finally we use this angle and feed it into a float curve so we can change the angle into a speed variable. I used a curve just because you can get more control over it, you can see below I setup the curve using the time as the angle. So -45 is just going down hill and positive is going up. I also don’t have the speed change in the central area unless the player starts moving up or down a larger angle. Most small hills don’t really change speed too much in my version but setup the curve how you would like it!

Then we interpolate the characters current speed into the desired speed!

Now we just need to connect all our functions up to the tick and we are done! The character will now change their speed based on the slope! For more in depth tutorial check out the link at the top. Cheers!

1 Like