Hello everyone,
I wrote the code below to create a function to be called through blueprint. It should be a Dash-like ability.
void ATBO02Character::Dash()
{
if (CanDash)
{
UE_LOG(LogTemp, Warning, TEXT(“Dash”));
FVector MeshForwardVector = GetActorForwardVector();
MeshForwardVector.Normalize();
GetCharacterMovement()->Velocity = MeshForwardVector * DashSpeed;
const float Delay = 1 / PlayBackSpeedForAnimation;
GetWorldTimerManager().SetTimer(DashTimerHandle, this, &ATBO02Character::StopDash, Delay, false);
CanDash = false;
}
}
void ATBO02Character::StopDash()
{
UE_LOG(LogTemp, Warning, TEXT(“StopDash”));
GetCharacterMovement()->Velocity = FVector::ZeroVector;
CanDash = true;
}
Even though everything runs smoothly: the velocity is changed and the timer finishes calling StopDash(), the player doesn’t do the Dash.
The only 2 variables I set through the blueprint, before calling the function itself, are DashSpeed and PlayBackSpeedForAnimation.
Maybe I am missing something stupid, but I’m new both to c++ and UE5.
I asked ChatGPT to create me a function to do the Dash, thought I were missing something, but it basically wrote the same thing.
I tried using .AddImpulse instead of changing the velocity, but it changes nothing.
It’s hard to say without seeing all of your character’s code. You may have something else that’s overriding the velocity. Otherwise, I’d suggest looking at the functions/settings on your movement component. Maybe take a look at UpdateComponentVelocity and bRequestedMoveUseAcceleration.
You may need to adjust maxwalkspeed on the fly. I needed to in order to create sprint and slow walk functions. If that is kinda of what you are after.
float MaxWalkSpeed The maximum ground speed when walking. Also determines maximum lateral speed when falling.
As @BadScientist89 mentions below. i adjust maxwalkspeed - maxacceleration - breakingfriction - groundfriction before applying my movement and adjusting my globalanimratescale.
It’s probably important to note that setting the MaxWalkSpeed will just increase the speed that the character walks. It will not actually move them forward. The player would still have full control of movement. They would be able to move left/right/backwards or even stand still while “dashing” instead of automatically moving directly forward.
1 Like
In the end I just changed everything considering I gave up on it, it was taking me too long.
I opted for the LaunchCharacter function and I changed everything to this:
void ATBO02Character::Dash(bool IsMoving)
{
FVector LastInputVector = GetLastMovementInputVector();
FVector MeshForwardVector = GetActorForwardVector();
if (IsMoving)
LaunchCharacter(LastInputVector * DashSpeed, true, true);
else
LaunchCharacter(MeshForwardVector * DashSpeed, true, true);
}
The function is called when I press SpaceBar and it receives through blueprint the DashSpeed, and by calling the Input_Action_Move I set CanDash as true if the IA is triggered and false if the IA is completed.
I really didn’t understand if it was an override or what, considering that I was actually printing the Velocity in the Tick function , and it wouldn’t change even after 1 minute of not pressing anything.
Thank you all for the kind suggestions, tomorrow I will try to do what @ShawnDaGeek and @BadScientist89 were talking about. Will update in case something positive comes out.
me and Bard ai just discussed this lol and Bard believes this maybe a good starting point for your function. I did not test it yet, but i will test eventually this week. At a glance you should be able to nest this function in as you need.
void ATBO02Character::DashForward(float DashDistance)
{
FVector DashDirection = GetActorForwardVector();
// Temporarily override max acceleration to ensure dash speed
UCharacterMovementComponent* CharacterMovement = GetCharacterMovement();
float OriginalMaxAcceleration = CharacterMovement->MaxAcceleration;
CharacterMovement->MaxAcceleration = 10000.0f; // Adjust as needed
// Calculate dash velocity based on distance and desired duration
float DashDuration = 0.5f; // Adjust as desired
float DashVelocity = DashDistance / DashDuration;
// Apply impulse using AddImpulse for more control over acceleration
CharacterMovement->AddImpulse(DashDirection * DashVelocity, true);
// Restore original max acceleration after dash
CharacterMovement->MaxAcceleration = OriginalMaxAcceleration;
// Optionally clamp velocity to prevent excessive speed
FVector CurrentVelocity = GetVelocity();
if (CurrentVelocity.Size() > MaxDashSpeed)
{
SetVelocity(CurrentVelocity.GetSafeNormal() * MaxDashSpeed);
}
}
Key aspects of this function:
- Explicit Acceleration Override: Temporarily overrides
MaxAcceleration
to ensure the dash can achieve the desired velocity.
- AddImpulse Usage: Uses
AddImpulse
for more granular control over acceleration, potentially avoiding issues with LaunchCharacter
.
- Velocity Calculation: Explicitly calculates dash velocity based on distance and duration, providing better control over the dash’s behavior.
- Velocity Clamping: Optionally clamps velocity to a maximum value after the dash to prevent unintended overshoots.
Remember:
- Adjust
DashDistance
, DashDuration
, and MaxDashSpeed
to match your specific gameplay requirements.
Blockquote
Hey! Thanks a ton for taking the time in this hellhole of a problem.
I tried your method, but it doesn’t work. At this point I am inclined to believe that something is overriding the velocity and I cannot change it.
I tried logging the velocity in the tick and it always stays at 0 even though I clearly set it to something else.
I am thinking the same, have you tried testing blueprints with addforce node or any thing beside the c code?
- Are you only firing the impulse once, or rapid fire until you hit your target velocity?
The dash function I created is “copied” from the blueprint I did beforehand. This is why I find it so strange: in the blueprint the velocity gets modified, and the dash function works.
- I only fire it once, when the space bar is pressed.