Multiple Doors opening different ways

I decided to make two doors one which open to the left and one which opened to the right, this meant i had to rotate one door 180 degrees. This does not work with close door as the yaw value is being set to 0 which rotates the door inside the wall.

The solution is simple for this, get the current yaw and +/- the open angle value

void UOpenDoor::OpenDoor()
{
	Owner->SetActorRotation(FRotator(0.0f, Owner->GetTransform().Rotator().Yaw + OpenAngle, 0.0f));
}

void UOpenDoor::CloseDoor()
{
	Owner->SetActorRotation(FRotator(0.0f, Owner->GetTransform().Rotator().Yaw - OpenAngle, 0.0f));
}

The angle I chose was 90 for the door that opened to the left and -90 for the one that opened to the right.

This added another problem when checking the LastDoorOpenTime as CloseDoor() is repeatedly called every frame, so to resolve this I added

bool IsDoorOpen = false;

to the the OpenDoor.h file and then added the following code to open the door

if (PressurePlate->IsOverlappingActor(ActorThatOpens) && !IsDoorOpen)
{
	OpenDoor();
	LastDoorOpenTime = GetWorld()->GetTimeSeconds();
	IsDoorOpen = true;
}

To close the door I added the following code

if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay && IsDoorOpen && (!ActorThatOpens->IsOverlappingActor(PressurePlate)))
{
	CloseDoor();
	IsDoorOpen = false;
}

There is an extra check on the end which will only allow the door to close if the player has left the TriggerVolume otherwise the door would close then open again repeatedly, this has a side effect that if you stay in the volume too long then leave the door closes immediately. To get round this I added the following

if (IsDoorOpen && ActorThatOpens->IsOverlappingActor(PressurePlate))
{
	LastDoorOpenTime = GetWorld()->GetTimeSeconds();
}

This will update the last door open time for everyframe if the door is open and the player is inside the TriggerVolume.

1 Like

Privacy & Terms