Download

2d character dash

so i was trying to turn normal 2d character movement into dash with SetTimer cooldowns but in the process, my 2d character doesn’t move at all :<
do u guys know what may b the cause of it?


void ACW_SOWCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
    PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
    PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
    PlayerInputComponent->BindAxis("MoveRight", this, &ACW_SOWCharacter::MoveRight);

    PlayerInputComponent->BindTouch(IE_Pressed, this, &ACW_SOWCharacter::TouchStarted);
    PlayerInputComponent->BindTouch(IE_Released, this, &ACW_SOWCharacter::TouchStopped);
}

void ACW_SOWCharacter::MoveRight(float Value)
{
    GetWorld()->GetTimerManager().SetTimer(dash_timer_handle, this, &ACW_SOWCharacter::reset_dash, 3.0f, false);
    while (dash_enabled)
    {
        /*UpdateChar();*/
        // Apply the input to the character motion
        AddMovementInput(FVector(1.0f, 0.0f, 0.0f), Value);
    }
}

void ACW_SOWCharacter::reset_dash()
{
    bool dash_enabled = false;
    GetWorldTimerManager().ClearTimer(dash_timer_handle);
    GetWorld()->GetTimerManager().SetTimer(dash_timer_handle, this, &ACW_SOWCharacter::reset_dash_input, 3.0f, false);
}

void ACW_SOWCharacter::reset_dash_input()
{
    bool dash_enabled = true;
    GetWorldTimerManager().ClearTimer(dash_timer_handle);
}

also dash_enabled is true by default
it supposed to make 2d char move for 3 secs and then do 3 secs cooldown before it can move again, but apparently it makes it unable to move at all :confused:

This can be done simply like this


void ACW_SOWCharacter::MoveRight(float Value)
{
    if (!FMath::IsNearlyZero(Value) && !bPlayerDashing)
    {
        //Time for dash
        const float DashTime = 3.f;

        const float CooldownTime = 3.f;

        //Strength of dash, how much we should move.
        const float DashStrength = 800.f;

        bPlayerDashing = true;

        bDashRight = Value > 0.f;

        FTimerHandle THandle;
        FTimerDelegate TDel;
        TDel.BindWeakLambda(this, [this]() { bPlayerDashing = false; });

        // Cooldown dash
        GetWorld()->GetTimerManager().SetTimer(THandle, TDel, DashTime + CooldownTime, false);

        FRootMotionSource_ConstantForce* ConstantForce = new FRootMotionSource_ConstantForce();
        ConstantForce->InstanceName = "Dash";
        ConstantForce->AccumulateMode = ERootMotionAccumulateMode::Override;
        ConstantForce->Priority = 5;
        ConstantForce->Force = FVector(bDashRight ? 1.f : -1.f, 0.f, 0.f) * DashStrength;
        ConstantForce->Duration = DashTime;
        ConstantForce->StrengthOverTime = nullptr;
        ConstantForce->FinishVelocityParams.Mode = ERootMotionFinishVelocityMode::SetVelocity;
        ConstantForce->FinishVelocityParams.SetVelocity = FVector::ZeroVector;

        GetCharacterMovement()->ApplyRootMotionSource(ConstantForce);
    }
}

Adjust the three values to your liking or expose them to the editor
But this will be a 3second dash with 3 second cooldown