Deprojecting screen to world space causes weird rotation on all characters

Hello friends,

I need to rotate my player towards the point the mouse is targeting so I have written the following code:

void ALeviathanPlayerController::RotateToTarget()
	APawn* thePawn = GetControlledPawn();
	if (thePawn)
		if (IsLocalPlayerController())
			FVector2D mousePosition = FVector2D(0, 0);
			GetMousePosition(mousePosition.X, mousePosition.Y);

			FVector worldLocation = thePawn->GetActorLocation();
			FVector targetLocation, targetDirection;

			DeprojectScreenPositionToWorld(mousePosition.X, mousePosition.Y, targetLocation, targetDirection);

			DrawDebugLine(GetWorld(), targetLocation, targetDirection, FColor::Red);

			targetDirection *= 10000;

			targetDirection += targetLocation;

			FVector Intersection = FMath::LinePlaneIntersection(targetLocation, targetDirection, thePawn->GetActorLocation(), FVector(0, 0, 1));

			FRotator Rotation = FRotationMatrix::MakeFromX(Intersection).Rotator();


The character rotates but as well as all other characters, even though I checked if it’s a local player controller. And it’s happening really fast and doesn’t work.

I’ve setup the following in Blueprints and it works perfectly fine. Recreating the same thing doesn’t seem to work in C++.

Here’s the BP script:

In the working blueprint you are using Set ControlRotation on the controller, but in C++ you are using SetActorRotation. This may be your issue.

Is your game multiplayer, with many different clients? If not, then technically all Player Controllers are local.

Yes it is multiplayer, that’s why I’m using the IsLocalPlayerController();

Changed it to SetControlRotation() still the same issue occurs.

I tried different approach, a lot cleaner one, but it still rotates like crazy:

FHitResult Hit;
GetHitResultUnderCursor(ECC_Visibility, false, Hit);

FRotator Rotation = FRotationMatrix::MakeFromY(Hit.ImpactPoint).Rotator();


This handles the rotation a little better, but breaks my movement. Why is there such big difference in C++ and Blueprints?

EDIT: Fixed by removing inheritance of Pitch, Yaw, Roll in my SpringArmComponent.

Where are you calling RotateToTarget() from?

In the character’s tick function. Apparantly the weirdness dissapeared but my rotation doesn’t work properly. I suppose I don’t the the pojecting properly.