Opening Door with Relative Yaw

So I stuck with just the Lerp() for this and didn’t use the FInterpConstantTo() but I added a relative yaw angle so it should work for any door. I also added two editable variables to the Door component so that you can choose the Relative Yaw angle to open the door to and the Opening Rate of the door from within the UE Editor. A google search helped with that :slight_smile:

OpenDoor.h

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

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Attributes)
	float OpenYaw = 90.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Attributes)
	float OpenRate = 0.005f;

private:
	float TargetYaw;
};

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);

	FRotator CurrentRotation = GetOwner()->GetActorRotation();
	CurrentRotation.Yaw = FMath::Lerp(CurrentRotation.Yaw, TargetYaw, OpenRate);
	GetOwner()->SetActorRotation(CurrentRotation);

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

Privacy & Terms