Absolute Movement independent of Rotation

Hello, straight to my problem:

I started with the Top-Down C++ Sample and implemented simple WASD movement.
Next I made that the character always rotates to the mouse position.
The player camera is fixed on the character and does NOT rotate.
So far so good.

The problem is that I don’t want the character to actually walk to the mouse position when holding “MoveForward”.
I want him to face the mouse cursor at all times but I want him to move on an absolute plane.

In simple terms: WASD always moves UP/LEFT/DOWN/RIGHT regardless of mouse position, mouse position only changes the characters “view”(rotation).

I hope my question is understandable regardless of my broken english.
Here’s my code so far:

void ATopDownCodePlayerController::TurnToMouse()
{

	
	//Gets local reference to the controller's character and its rotation
	ACharacter *currentChar = GetCharacter();
	

	//determine mouse Location in world space
	FHitResult mouseHit = FHitResult();
	bool bMouseResult = GetHitResultUnderCursor(ECC_Visibility, true, mouseHit);


	//create FVector MouseHitLoc to store the impact point of the location in world below the mouse
	FVector_NetQuantize MouseHitLoc = FVector_NetQuantize();

	//If we hit something set the location and update the rotation
	MouseHitLoc = mouseHit.ImpactPoint;

	//Get Pawn Loc
	FVector pPawnLoc = currentChar->GetActorLocation();

	//Create new vector from player to the mouse loc
	FVector TargetVector = (MouseHitLoc - pPawnLoc);
	TargetVector.Normalize();

	FRotator targetRotation = FRotator(0.f, TargetVector.Rotation().Yaw, 0.f);

	//set character to look at mouse cursor
	currentChar->SetActorRotation(targetRotation);



}

EDIT
These are my simple movement functions:

void ATopDownCodeCharacter::MoveForward(float Value)
{
	
	if (Value != 0.0f)
	{
		AddMovementInput(GetActorForwardVector(), Value);
	}
	
}

void ATopDownCodeCharacter::MoveRight(float Value)
{
	if (Value != 0.0f)
	{
		AddMovementInput(GetActorRightVector(), Value);
	}
}

I guess it’s obvious that I’m kinda new to all of this :).

Again, what my code does right now is:

  • Character rotates to mouse position.
  • Pressing “MoveForward” moves him to mouse position (not wanted)

Thanks for your time and help!

What you are doing wrong is using GetActorForwardVector() and GetActorRightVector(). Since your character is rotated toward your mouse position, it only makes sense that the forward vector is actually the direction to the mouse.

If what you want is an absolute World space translation, you could always replace the GetActorForwardVector() by a simple FVector(1.0f,0.0f,0.0f);

This is assuming that your “forward” is the positive X-Axis.

This will work, but I suspect what you may want is the actual “camera-based” forward vector. For that you need to get your camera forward vector (usually minus the Z component).

FVector cameraForward = GetWorld()->GetFirstPlayerController()->PlayerCameraManager->GetCameraRotation().Vector();

cameraForward .Z = 0.0f;
cameraForward.Normalize();

//Now you can use cameraForward instead of GetActorForwardVector().

You should have a better way to access your player controller, I only put something here that should work pretty much everywhere (as long as your World is up)

Hope this helps!

This is perfect, thanks!
Do you know how I do the same with “MoveRight” now?
I.e. always move left/right regardless of pointer position/character rotation.

Again, thank you!

EDIT: I got it, I simply used the same solution as above, I just added
cameraRight.Y = 1.0f;