Character rotation problem C++

Hey, I have been making a top down shooter like character and I have been trying to make my character rotate towards mouse direction using C++. The problem is that character is only able to rotate 180 degrees, not 360. When I rotate more than 180 degrees (+90 or -90 from the original rotation) it starts to turn backwards.

Here is a picture of the problem. The blue cross is the current mouse location

Here is the code I’m currently using for the character rotation

APlayerController* playerController = Cast<APlayerController>(GetController());

if (!playerController) {
	return;
}

FVector MouseLocation;
FVector MouseDirection;

if (!playerController->DeprojectMousePositionToWorld(MouseLocation, MouseDirection)) {
	return;
}

FRotator characterRotation = this->GetActorRotation();
FRotator newActorRotation(characterRotation.Pitch, MouseDirection.Rotation().Yaw, characterRotation.Roll);

this->SetActorRotation(newActorRotation);

Also I’m initializing my character rotation relative configurations with folowing code:

// Character doesn’t rotate to camera rotation
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;

So the Camera doesn’t rotate with the character.

I wonder that maybe mouse direction is not what i think it is (maybe it is not relative to the actor location) or something else is wrong I just can’t figure it out. All help will be appreciated.

Seeing that how you approach the problem makes me assume that you are not familiar with linear algebra, and that is the usual way how this kind of problem is solved.

I tried to manipulate degrees couple of years ago to make a laser reflect and that did not go well.

Anyways, this is the “general solution” for this problem.

1.you get the hit vector resulted from the mouse raycast(I see your already have that)
(then it’s all linear algebra)

  1. get player position and then use the hit vector subtract player position, then normalize that vector.
  2. construct a unit rotation matrix out of that unit vector(which is the vector that’s been normalized), the unreal math library should have this function, if not, google how to construct a matrix out of a direction vector, it’s not too hard
  3. apply that rotation matrix to player(done)

I’m sorry, made a mistake. You can only construct a rotation matrix from two vectors. But the Up vector is implied so the solution still works.

Hi,

I found a solution to this problem. You just need to use the Mouse coordinates and Character coordinates on the screen. Add this code in “MyProjectPlayerController.cpp” in function “void MyProjectPlayerController::PlayerTick(float DeltaTime)”

AMyProjectCharacter *Character = Cast<AMyProjectCharacter>(GetControlledPawn());

	// Get mouse position on screen
	float xMouse;
	float yMouse;
	GetMousePosition(xMouse, yMouse);

	// Get Character position on screen
	FVector CharLoc = Character->GetActorLocation();
	FVector2D CharInScreen;
	ProjectWorldLocationToScreen(CharLoc, CharInScreen);
		
	// Get mouse position relative to the Character.
	FVector2D Result;
	Result.X = - (yMouse - CharInScreen.Y);
	Result.Y = xMouse - CharInScreen.X;

	// Get angle rotation and rotation Character
	float angle = FMath::RadiansToDegrees(FMath::Acos(Result.X/Result.Size()));

	if (Result.Y < 0)
		angle = 360 - angle;

	FRotator rot(0, angle, 0);

	Character->SetActorRotation(rot);

Thank you for your answer. It worked perfectly for me. Thank you so much! Sorry it took me so long to try it.