Unwanted Revolving Door for Single Door: Extra Credit Spoilers

I figured I’d create a few test cases to make sure that the code worked even in some non-standard circumstances and discovered something weird.

Here’s the code I added to the header file:

private:

FRotator OriginalRotation{0.f, 0.f, 0.f}; // We're just initializing these at 0.f for good practices.
float OpeningYaw = 90.f;
float TargetYaw = 0.f;

Here’s my cpp file:

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

	// Each door should know how far it can open, and what state it had when closed
	// By adding this here, we can avoid some revolving door bugs with every door
	OriginalRotation = GetOwner()->GetActorRotation();
	TargetYaw = OriginalRotation.Yaw + OpeningYaw;

}


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

	// UE_LOG(LogTemp, Warning, TEXT("%s"), *GetOwner()->GetActorRotation().ToString());
	// UE_LOG(LogTemp, Warning, TEXT("Yaw is: %f"), GetOwner()->GetActorRotation().Yaw);
	
	// We're keeping FRotator OpenDoor for convenience. 
	FRotator OpenDoor{
		OriginalRotation.Pitch,
		FMath::Lerp(GetOwner()->GetActorRotation().Yaw, TargetYaw, 0.1),
		OriginalRotation.Roll
	};
	
	GetOwner()->SetActorRotation(OpenDoor);
}

It’s hard do illustrated this, but here’s a screenshot. So it has motion lines I guess.
Door GOES WHEEEE!!
Please help me make it stop.

Yeah, the way the next lesson does it is a lot better.

It fixed the issue! :slight_smile:

I figured out what was causing it so I can avoid issues like this in the future too.
Each Frame I would grab the CurrentYaw for the door instead instead of setting the CurrentYaw with FMath::Lerp(). This caused a rounding error where the current rotation would be above the TargetYaw forcing it to rotate around again and again. There might have been a point where it finally did meet, but that would have been rare because of the rounding issues.

I like the solution in the other lesson more anyway because I feel like I did some weird stuff here and I didn’t really want to make a GetOwner() call every frame. I feel like this solution is much more performant.

Also note in one of my comments I said I initialized for good practice. That’s good practice in Python and JavaScript, but not in Unreal I think.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms