Launching a character left or right in local space

Hello again!

I’m still trying to wrap my head around C++ in Unreal but I can’t seem to get past this issue.

I am trying to launch my character off of walls. If the characters left side is hitting a wall and they jump I want the character to go right and vice versa.

The issue is the character jumps in world space meaning the system only works on some rotations.

If I am on a wall and the character is turned 90 degrees, instead of jumping to the right it will push me forward.

The character needs to be able to hop off of walls no matter which side they are on

I assume I need to get the characters local right vector but there is no explicit way of getting it.

I am using line casts to detect if the player is hitting a wall, how could I use those to help me?
Code:

			const FVector RightDir = GetActorRightVector();
			const FVector AddForce = RightDir + FVector(0, 1, 0) * 1000;

			if (hitLeft)
			{
				bCanDoubleJump = true;
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("You jumped to the right"));
				LaunchCharacter(AddForce, true, true);
			}
			if(hitRight)
			{
				bCanDoubleJump = true;
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("You jumped to the left"));
				LaunchCharacter(-AddForce, true, true);
			}

One thing you may want to consider is getting the impact normal of the hit from the raytrace you used to find the wall. (It will be included in the impact structure the raytrace returns.) This will be a vector pointing directly away from the surface of the wall.

I just tried that `const FVector RightDir = wallVect;
const FVector AddForce = RightDir + FVector(0, 1, 0) * 1000;

			if (hitLeft)
			{
				bCanDoubleJump = true;
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("You jumped left"));
				LaunchCharacter(AddForce, true, true);
			}
			if(hitRight)
			{
				bCanDoubleJump = true;
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("You jumped right"));
				LaunchCharacter(-AddForce, true, true);
			}`

It still has the issue, so it must mean I am not applying the force correctly!

Is there any other method of launching the character?
Thank you for helping!

This line looks problematic:

const FVector AddForce = RightDir + FVector(0, 1, 0) * 1000; if (hitLeft)

Am I correct in thinking you’re trying to add an upwards component to the jump?
Remember, Unreal 4 is Z-up, not Y-up. So that line’s always going to be adding a big force along the world Y-axis.

I thought (0,1,0) meant the right direction?

I am adding an upwards motion but that’s a few lines upward:

GetMovementComponent()->Velocity.Z += 100;

Thank you!
I removed the + FVector(0,1,0) and now I rely on the hit normal and I jump off of walls!
It now looks like

const FVector AddForce = RightDir * 1000;

+Y is to the right, but because you’re using it in world space, it will always be in the same direction without accounting for player facing.

You shouldn’t need it at all, since the wall impact normal is already giving a direction away from the wall. If you still want to use your original method, you need to convert the direction into world space from player local space.

The easiest way to do this is to transform the vector by the player transform, or rotation. I believe there are static methods to do this in the FMath library. They’ll be called “rotate vector” or “transform vector” or something like that - they’ll take your right vector, and the player transform or rotation, and output a new vector for you to use.