Getting a character to face the cursor in a Top-down game

As a beginner who downloaded UE4 for less than two weeks, I had a lot of trouble finding the right solution
of how to rotate a character so that it faces the cursor(in a top down game), I feel like such basic operation should not
be a struggle to do. But anyway it may just be me lacking the google skill… Enjoy!
Edit: The code below is working ! I just wanna share my solution.


void ATdCharacter::SetCursorDir() {

		//note that the line 58-59 is seen pretty common on the internet to find the cursor porjection

		//onto the internet, however, they do not give me the right vector.

		//FVector CursorWorldLocation, CursorWorldDirection;

		//GetWorld()->GetFirstPlayerController()->DeprojectMousePositionToWorld(CursorWorldLocation, CursorWorldDirection);



		//gets location of the actor in the world

		FVector CurrLoc = this->GetActorLocation();


		// the right method of getting cursor location!!! note: i used the exact same method in the Epic Top Down Blueprint.

		FHitResult hitResult;

		GetWorld()->GetFirstPlayerController()->GetHitResultUnderCursorByChannel(UEngineTypes::ConvertToTraceType(ECC_Visibility), true,hitResult);

		FVector hitLoc = hitResult.Location;


		//geting the original rotation of the acter;

		FRotator newRot = this->GetActorRotation();

		//Using atan2 function to get the degree between our character location and cursor location

		// aka how much we want the character to rotate

		float newYaw = (hitLoc - CurrLoc).Rotation().Yaw;;

		//Using that degree as the Yaw(rotating around Z axis) of our Frotator

		newRot.Yaw = newYaw;


		//in the end, we set it;



the complete code to class can be found on my github: GitHub - trazd66/Source: The right way to setup a cursor-facing character in UE4 with C++


First off, normalize the Direction between the hit location and your current location before calling .Rotation(). You can do this with GetSafeNormal().

The last line (SetControlRotation), you may want to read the API deceleration of it. If it is not effecting the rotation, it may be because bAbsoluteRotation is not set on the root component. I’m fairly new to UE myself, but I believe there is a checkbox on the pawn for “use controller rotation” or something like that, this may be what you want?

Hope I can be of some help!


Also, I should mention that DeprojectMousePositionToWorld is working, but not in the way that you think it is.

What this is doing is getting your mouse position on your screen to the world position where the screen (camera front plane) starts. If you wanted to get what your cursor is hovering over in the world… You would use the world location from DeprojectMousePositionToWorld as the starting point of a line trace in the direction the camera is facing (also output via DeprojectMousePositionToWorld).

I recently fell into that trap thinking it was projecting into the world!

Ya i realized that by drawing a debug line withthe direction i got from DeprojectMousePositionToWorld, it seem like it’s from my camera to where the cursor is. I had no luck with it so i tried a different approach and it worked. The code above i posted is working . And thank you very much for your reply!