Final Door code

So this is my final version of the OpenDoor class. I have used the FInterpTo() function because I think it gives a better effect and is framerate independent. I have also refactored the door opening code into a function Open().

The code in the video seems overly complicated - you don’t need to store the current yaw as a member function as it’s recalculated immediately in TickComponent().

OpenDoor.cpp

// Called when the game starts
void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();

	FRotator CurrentRotation = GetOwner()->GetActorRotation();
	TargetYaw = CurrentRotation.Yaw + OpenYaw;
}

// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	Open(DeltaTime, TargetYaw);
}

void UOpenDoor::Open(float DeltaTime, float FinalYaw)
{
	FRotator CurrentRotation = GetOwner()->GetActorRotation();
	CurrentRotation.Yaw = FMath::FInterpTo(CurrentRotation.Yaw, FinalYaw, DeltaTime, OpenRate);
	GetOwner()->SetActorRotation(CurrentRotation);

	// UE_LOG(LogTemp, Warning, TEXT("Current Yaw %f Target Yaw %f"), CurrentRotation.Yaw, TargetYaw);
}

OpenDoor.h

....
public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
	void Open(float DeltaTime, float FinalYaw);

private:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Properties)
	float OpenYaw = 90.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Properties)
	float OpenRate = 2.0f;  // Interperlation speed

	float TargetYaw;
};
1 Like

Nice!
One Question: Why store FRotatorin variable instead of Yaw(float) ? (like in tutorial)

There was no real reason. I just reused the FRotator from the GetActorRotation() instead of a separate yaw float.

Privacy & Terms