How do I setup having a NPC Rotate towards player Character?

Hey guys,

I’m using the top-down template controller and right now I have a few NPC actors out in the game. I already have interactivity between the player and the NPCs, but I would like the NPC to face the player during these interactions.

I have been trying to get the NPC to rotate around, and have had no luck at getting to be correct. Any ideas or any sort of math that has to be done to get it to work?

Hey ProfessionsQuest,

For each of your pawns, you can use the Find Look at Rotation node with Start set to your Pawn’s location, and Target set to your Character’s location. You can use the resulting Rotation with a Set World Rotation on your corresponding Pawn to have them face towards your Character.

Hope that helps!

-Steve

1 Like

theres a perfect example of that in the Shootergame:

void AShooterAIController::UpdateControlRotation(float DeltaTime, bool bUpdatePawn)
{
	// Look toward focus
	FVector FocalPoint = GetFocalPoint();
	if( !FocalPoint.IsZero() && GetPawn())
	{
		FVector Direction = FocalPoint - GetPawn()->GetActorLocation();
		FRotator NewControlRotation = Direction.Rotation();
		
		NewControlRotation.Yaw = FRotator::ClampAxis(NewControlRotation.Yaw);

		SetControlRotation(NewControlRotation);

		APawn* const P = GetPawn();
		if (P && bUpdatePawn)
		{
			P->FaceRotation(NewControlRotation, DeltaTime);
		}
		
	}
}

Thanks for the reply! :slight_smile:

When I put what you recommended into blueprints, the rotation does something quite weird.

The blue guy who looks normal is the player, while the blue guy performing the super lean is the rotated NPC.

6192-weirdrotation.jpg

Hey ProfessionsQuest,

I left out a step before, sorry, you are only going to be concerned with the Yaw component of the rotation in your setup, so you’ll need to isolate it from the Pitch and Roll using a combination of Break Rot and Make Rot.

Another thing we have to take into consideration if you are using the HeroTPP skeletal mesh is that the mesh itself is facing down the Y-axis (Green in the screenshot) and not the X-axis (Red in the screenshot):

What this means is that if you use the Find Look at Rotation node, it returns the rotation to look at that location when facing down the mesh’s X-axis so this mesh will look like it is facing perpendicular to what you want. You can simply add a -90 degree offset to your Yaw to fix this–here’s what it would look like overall:

Hope this helps out!

-Steve

Thank you so much! It worked! :slight_smile:

Thank you!

I know this is an old thread but is there a way to do this where the rotation is smooth? I tried this for an AI and it just turns instantly and unnaturally.