Doors remember their placed position

So in my version of the puzzle room and door mechanics I added the simple solution that every door remembers its original position (the one given to it when its placed in the level). With the code in the lecture the door would have trouble opening if you replicated it anywhere else, ie if it faced any other direction.

defaultPositionOfDoor is a FRotator and thus you need to retrieve the yaw in the set rotation segment for each closing and opening of the door. Otherwise everything is the same as the courses dictate. Just felt like this was worth sharing because its not completely obvious! :slight_smile: Happy coding!

void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();
	
	// get the current object
	myOwner = GetOwner();
	// get the actor object that will be opening the door, in this case the player controller pawn
	ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
	// get the current position
	defaultPositionOfDoor = myOwner->GetActorRotation();
}


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

	// check for actor overlap over the trigger volume
	if (PreasurePlate->IsOverlappingActor(ActorThatOpens))
	{
		OpenDoor();
		// get the time that the door opened
		LastDoorOpenTime = GetWorld()->GetTimeSeconds();
	}		

	// check if its time to close door: Current time - time the door opened > than the total time alloted
	if ((GetWorld()->GetTimeSeconds() - LastDoorOpenTime) > DoorClosesDelay)
		CloseDoor();
		
}

void UOpenDoor::OpenDoor() 
{
	// apply the new rotation to the current position
	myOwner->SetActorRotation(FRotator(0.0f, defaultPositionOfDoor.Yaw + OpenAngle, 0.0f));
}

void UOpenDoor::CloseDoor()
{
	// close the door
	myOwner->SetActorRotation(FRotator(0.0f, defaultPositionOfDoor.Yaw, 0.0f));
}

Privacy & Terms