Guide: How to get OpenDoor and CloseDoor to work when your door doesn't start with yaw 0

I encountered a problem when following the course’s code for opening and closing the door, because my door starts at 90 degrees (because I put it on a different wall). I fixed this and this code should work for any upright door, whatever the start yaw angle:

in the private section of the OpenDoor.h file:

    UPROPERTY(EditAnywhere)
    float OpenAngle = -90.0f;
    float ClosedAngle;

in the constructor of OpenDoor.cpp:

    ClosedAngle = GetOwner()->GetActorRotation().Yaw;
    OpenAngle += ClosedAngle;

and OpenDoor and CloseDoor functions:

void UOpenDoor::OpenDoor() {
    FRotator Rotation = FRotator(0.f, OpenAngle, 0.f);
    Owner->SetActorRotation(Rotation);
}

void UOpenDoor::CloseDoor() {
    FRotator Rotation = FRotator(0.f, ClosedAngle, 0.f);
    Owner->SetActorRotation(Rotation);
}

When I approached this, I saved the FRotator for the open and close positions as Private Variables. I retained the Open Angle Variable as editable everywhere.

This change cleans up the open and close functions into a single line.

I did my work in BeginPlay (instead of the constructor)
My code to set this up looks like this:

Add on the FRotator changes the rotator it’s used on, that’s why you see me hit up GetActorRotation twice.
I could combined lines 2+3, Didn’t cause I think it reads better for me.

1 Like

I did the same thing - saving the initial angle of the door and offsetting from that. Seems to have worked well.