Open Door with FInterpTo and kill tick once it's open

Not sure if this is the best way but the examples left tick running and that just keeps recalculating and in the case of FInterpTo it just gets exponentially smaller so I added a check that said when it was close to being open kill tick so it’s stops processing in the background all the time. Also set it up so it just adds 90deg to whatever the current Yaw is at the beginning of the game so it doesn’t matter how it’s rotated to start with.;

#include "OpenDoor.h"
#include "GameFramework/Actor.h"

// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


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

	//Set initial target Yaw set in header as private so its available across class.
	TargetYaw = GetOwner()->GetActorRotation().Yaw + 90.f;
	
}


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

	//Get Current Yaw Rotation
	float CurrentYaw = GetOwner()->GetActorRotation().Yaw;

	//OpenDoor rotator declared as private in header so I'm just delcaring in once, not every tick.

	//for constent open
	//OpenDoor.Yaw = FMath::FInterpConstantTo(CurrentYaw, TargetYaw, DeltaTime, 45);
	//Use FInterpTo so it smooths out open 	Dist between current Yaw and Target Yaw * FMath::Clamp<float>(DeltaTime * InterpSpeed, 0.f, 1.f) then add that back to CUrrent and update for next tick.
	OpenDoor.Yaw = FMath::FInterpTo(CurrentYaw, TargetYaw, DeltaTime, 2);
	GetOwner()->SetActorRotation(OpenDoor);
	//UE_LOG(LogTemp, Warning, TEXT("Yaw -> %f"), OpenDoor.Yaw);
	//Once it gets close to 90 deg kill tick else it just keeps taking cycles in the background all the time and never ends the calculation.
	if (TargetYaw - CurrentYaw < 1)
	{
		SetComponentTickEnabled(false);
		UE_LOG(LogTemp, Warning, TEXT("TurnedOffTickDoorOpen"));
	}
	
}
1 Like

Privacy & Terms