[tip] Smoothly SetFocus - Simple Shooter Course

Hey all,

I just wanted to share a nice solution I found for smoothly setting the focus in the Simple Shooter game.

The SetFocus function works just fine but I didn’t like how the character snapped directly to you. There is a tutorial posted on the Unreal Engine site that worked perfectly for me and I thought I’d share.

https://dev.epicgames.com/community/learning/tutorials/ZYVB/unreal-engine-smooth-focus-using-ai-setfocus-setfocalpoint

You keep your current SetFocus logic, this just overrides how the rotation happens… now I want to figure out how to show a feet shuffling animation while the rotation happens… :grin:

To make it a step easier here is the c++ code I added:

#include "Kismet/KismetMathLibrary.h"

void AShooterAIController::UpdateControlRotation(float DeltaTime, bool bUpdatePawn)
{
	//Call the original method without updating the pawn
	Super::UpdateControlRotation(DeltaTime, false);
 
	//Smooth and change the pawn rotation
	if (bUpdatePawn)
	{
		//Get pawn
		APawn* const MyPawn = GetPawn();
		//Get Pawn current rotation
		const FRotator CurrentPawnRotation = MyPawn->GetActorRotation();
 
		//Calculate smoothed rotation
		SmoothTargetRotation = UKismetMathLibrary::RInterpTo_Constant(MyPawn->GetActorRotation(), ControlRotation, DeltaTime, SmoothFocusInterpSpeed);
		//Check if we need to change
		if (CurrentPawnRotation.Equals(SmoothTargetRotation, 1e-3f) == false)
		{
			//Change rotation using the Smooth Target Rotation
			MyPawn->FaceRotation(SmoothTargetRotation, DeltaTime);
		}
        
	}
}

here is the .h additions:

protected:
	FRotator SmoothTargetRotation;
 
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float SmoothFocusInterpSpeed = 30.0f;

public:	
	virtual void UpdateControlRotation(float DeltaTime, bool bUpdatePawn) override;

Hope it helps!

Privacy & Terms