Need actor to roll in a certain direction depending on what keys were pressed. (C++)

Hello, I am currently trying to implement a dodge roll mechanic in my game. The idea is that, depending on which keys were pressed, the character will roll towards that direction. Depending on which direction the actor is facing, he will either dodge roll smoothly, or dodge, but also in a direction they aren’t supposed to dodge, or not dodge at all because there is some sort of force pushing them in the opposite direction.

I tried a lot of different ways to fix it. Adding in things like Forward vectors to the final calculation, but I’m still getting the same problems. I have a feeling that the actors forward vector has something to to with the wonkiness of the direction, but I’m not exactly sure.

Here is the code:

FVector FinalDirection = FVector(0.f, 0.f, 0.f);

if (InputComponent->GetAxisValue(FName("MoveForward")) > 0)
FinalDirection += FVector(1.f, 0.f, 0.f);

else if (InputComponent->GetAxisValue(FName("MoveForward")) < 0)
    FinalDirection += FVector(-1.f, 0.f, 0.f);

if (InputComponent->GetAxisValue(FName("MoveRight")) > 0)
FinalDirection += FVector(0.f, 1.f, 0.f);

else if (InputComponent->GetAxisValue(FName("MoveRight")) < 0)
    FinalDirection += FVector(0.f, 1.f, 0.f);

return GetActorForwardVector() + FinalDirection;

Any pointers on what I am doing wrong? Or if there is a piece of code that makes something like this much easier? Any help will be appreciate.

This is the part that’s incorrect:

 return GetActorForwardVector() + FinalDirection;

You don’t want to add the vectors together. You want to rotate the ‘FinalDirection’ so it’s relative to the Actor’s facing. There are many ways to do this and it’s a very important concept when doing things in 3D space.

return GetActorForwardVector().GetRotation().RotateVector( FinalDirection );

But a better way is to use the GetActorQuat() function used internal to GetActorForwardVector since it’s just rotating the global FVector::ForwardVector and returning it to you.

return GetActorQuat().RotateVector( FinalDestination );

However the easiest way do this would be to use GetActorForwardVector() and GetActorRightVector() instead of your hard coded vectors (ex: FVector(0.f, 1.f, 0.f)) then you wouldn’t have to rotate them at all. Just negate them if they’re pressing down or left. Here’s your ‘Down’ section:

 else if (InputComponent->GetAxisValue(FName("MoveForward")) < 0)
     FinalDirection -= GetActorForwardVector();

Also, you missed a ‘-’ in your Left roll check.

I tried what you did, but I am still getting the same problems. When I’m moving down, it’ll sometimes launch him up depending on which way he’s facing. Could Orientate Rotation To Movement be an issue perhaps?

The Orientate Rotation To Movement is used to turn a Character to face the direction he’s moving at all times. That’s useful for a 3rd person game when you never want the character to strafe or move backwards relative to his facing. I would guess you wouldn’t want that on in this case b/c you want the character to move in these other directions when he dodges.

But honestly without more insight into how you want your character controlled or a reference from another game then it’s hard for me to tell you for sure.

I’m going to mark this question as answered since I found a workaround. I just increased the movement speed temporarily. Thank you for your help though.