Move towards mouse cursor

Hey! Im trying to implement topdown input movement, where the player always walks towards the mouse cursor while using the wasd movement.

I did the “look at” (in this case at the mouse cursor) in blueprint:

https://impetus-games.com/uploads/cgblog/id6/05_Actor_Rotation_BP.jpg

Now, i use the standard move forward input in c++:

 void ACharacter::MoveForward(float Value)
    {
        if ( (Controller != NULL) && (Value != 0.0f) )
        {
            // find out which way is forward
            FRotator Rotation = Controller->GetControlRotation();
            
  
            // add movement in that direction
            const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
            AddMovementInput(Direction, Value);
        }
    }

Now the problem is: when i dont move, the character is facing the mouse cursor how it should, but ofc when i move with wasd, it walks not towards the mouse direction, but instead in the typical fps/3rd directions. I know that i need to declare the function, but nothing works properly.

I´ve tried already a lot of stuff but i can´t find an solution. Would be great if someone could help! Hope everything is clear. Thanks already so much!

Hey guys! I´ve found the solution by myself after trying, trying, trying and again trying.

I post the code here for anyone who has a similiar problem. Basicly i´ve added a new line who defines the current ActorRotation (look into the ue4 documentation for controller). Than i´ve added the new rotation to the direction definition.I just post the old and the new code, so it should be clear for everyone!

Heres the old code:

 void ACharacter::MoveForward(float Value)
     {
         if ( (Controller != NULL) && (Value != 0.0f) )
         {
             // find out which way is forward
             FRotator Rotation = Controller->GetControlRotation();
             
   
             // add movement in that direction
             const FVector Direction =    FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
             AddMovementInput(Direction, Value);
         }
     }

Updated one

void ACharacter::MoveForward(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f))
	{
		// find out which way is forward
		FRotator Rotation = Controller->GetControlRotation();

		//Get the updated current delta rotation from the mouse input
		FRotator ActorRotation = GetActorRotation();
		
		// get forward vector (set updated rotation with ActorRotation Matrix)
		const FVector Direction = FRotationMatrix(ActorRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}

(for the Right Vector you do exactly the same.

Greetings! :slight_smile: