Smooth Sprint, crouch and prone system

Hi!

I am trying to make a system where when I press the sprint key, instead of instantly going to my sprint speed, it would gradually go from walk speed to sprint and crouch and prone would do the same but with the added height change.

I already tried using timelines and it worked perfectly until I went from sprinting to crouching where when I try to crouch it first stops the sprint which reverses the sprint timeline and right after plays the crouch timeline but as the stop sprint is little bit longer than crouching the final character speed instead of being the crouched speed would be the walking speed (when the sprint timeline reverses it goes to the walking speed) because the stop sprint ends after the crouching.

I imagine I could just tweak the times so they finish when they should but I am almost certain that there’s a better way to do that and also I don’t plan on using animations for now.

Any help would be really apreciated!

1 Like

You could use finterp.

The moment the relevant key gets pressed, a new target speed is set and the code moves between speeds like this

You just set ‘new max speed’ when a key is pressed.

You’ll need to twiddle with the inter speed to get it to your liking. If you want a more timeline feel to it, you can also try

image

1 Like

Thx for the reply and sorry for mine being late!

I am using C++ and tried to convert your blueprint code to C++ and either I did it wrong or it is not working.

**Tick Function**

void APlayerChar::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	UE_LOG(LogTemp, Error, TEXT("Current Speed = %f"), GetCharacterMovement()->Velocity.Size());

	GetCharacterMovement()->MaxWalkSpeed = FMath::FInterpTo(currentMaxSpeed, newMaxSpeed, DeltaTime, 1.f);

}

**Stance handling function**

void APlayerChar::handleStanceChange(EStance RequestedStance)
{
	if (RequestedStance != currentStance)
	{
		switch (RequestedStance)
		{
		case EStance::sprinting:
			currentMaxSpeed = 500.f;
			newMaxSpeed = 850.f;
			break;
         }
    }
}

The problem is that when I sprint the max speed only goes up to “505.XXXXX” instead of 850 and if I set the interp speed to 10 then i goes to “55X.XXXX”.

I also tried Interp to constant and it did not work.

The system I used to have was using a Lerp and timeline and only had problems when the timelines overlap. Is there someway to make a function wait for a timeline to end?

Edit: And also if it is possible not to use the tick function? I am a big noob and I learned/heard that using Tick is a bad practice

Afraid I’m not a CCP ocifionado :-/

Tick for a small calculation like this will not use resources. You’ll find it a much more elegant system, once you get it working :slight_smile:

All you need to do it set the ‘new max speed’ and the interp will shoot right there.

Not at a machine right now, but on that tick node, it’s just

Tick → finterp → set character max speed

I should be able to check it out later…

Thank you for that resource and I’ll continue to try and make it work!

1 Like

In your CPP example, the ‘current max speed’ needs to read from the character, every tick :wink:

( it needs to be a live sample ).

PS: To be clear, there’s no rule about reading it from the character, but it does need to be the actual current max speed. How you do that is up to you, but it will basically mean setting the speed on the character every tick and either reading it again, or reading it from a variable you’re using to set the speed.

1 Like

Wow I can’t believe I’m this blind…

Thank you so much, you are a life saver I can’t believe it’s been nearly 2 days that I was trying to solve this and the solution was that simple.

Again THANK YOU!

1 Like

You’re welcome :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.