Opening and closing Doors

Hello everybody, im following the course and is exiting how it is going on, i want to share you something i made to make the doors open and close constantly

void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();	
	CurrentYaw = GetOwner()->GetActorRotation().Yaw;
	TargetYaw = CurrentYaw + 90.f;
	bIsOpen = false;
}


// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	CurrentYaw = GetOwner()->GetActorRotation().Yaw;
	FRotator OpenDoor(0.f, TargetYaw, 0.f);
	OpenDoor.Yaw = FMath::FInterpTo(CurrentYaw,TargetYaw, DeltaTime, 1);
	GetOwner()->SetActorRotation(OpenDoor);
	if(FMath::Abs(CurrentYaw - TargetYaw) < 0.1f){
		bIsOpen = !bIsOpen;
		TargetYaw = bIsOpen ? CurrentYaw - 90.f :CurrentYaw + 90.f;
	}
}

Let me know what do you guys think about it.

Privacy & Terms