# Move towards mouse cursor

**URL:** <https://forums.unrealengine.com/t/move-towards-mouse-cursor/348319>\
**Category:** Blueprint\
**Tags:** fps-shooter-game-sam, unreal-engine, UE4, question, Blueprint, CPP\
**Created:** [February 22, 2016, 10:04pm UTC](https://forums.unrealengine.com/t/move-towards-mouse-cursor/348319 "2016-02-22T22:04:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prometheus88](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Prometheus88](https://forums.unrealengine.com/u/Prometheus88)\
**Post date:** [February 22, 2016, 10:04pm UTC](https://forums.unrealengine.com/t/move-towards-mouse-cursor/348319/1 "2016-02-22T22:04:29Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![Prometheus88](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Prometheus88](https://forums.unrealengine.com/u/Prometheus88)\
**Post date:** [February 23, 2016, 3:55pm UTC](https://forums.unrealengine.com/t/move-towards-mouse-cursor/348319/2 "2016-02-23T15:55:41Z")

</div>

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! 🙂
