Implemented code for better sound activation

Dancing in and out of the triggervolume would trigger the doorsound regardless of the door being half way open/close… Makes no sense, so I put in a check for CurrentYaw vs InitialYaw

On OpenDoor i put +5.f since the door would start to move before reaching the play sound in the code and it would no longer be at exactly InitialYaw
On CloseDoor i put everything in the initial yaw vs current yaw check, so you can’t play the open door sound unless the closedoor sound has been played… Gives a nice feel to the doorsound
The way you can make it play one right after the other if you activate dooropen right when it closes

void UOpenDoor::OpenDoor(float DeltaTime)
{
CurrentYaw = FMath::FInterpConstantTo(CurrentYaw, OpenAngle, DeltaTime, DoorOpenSpeed);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
if (!AudioComponent) { return; }
if (!OpenDoorSound)
{
OpenDoorSound = true;
CloseDoorSound = false;
if (CurrentYaw < InitialYaw+5.f)
{
AudioComponent->Play();
}

}
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
CurrentYaw = FMath::FInterpConstantTo(CurrentYaw, InitialYaw, DeltaTime, DoorCloseSpeed);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
if (!AudioComponent) { return; }
if (CurrentYaw == InitialYaw)
{
if (!CloseDoorSound)
{
AudioComponent->Play();
CloseDoorSound = true;
OpenDoorSound = false;
}
}

}

An interesting aproach, well done.
But, since we let the OpenAngle a property that can be edited at any value (even negative ones to make the door open in the other way) maybe this condition will never be true:

Instead, I recomend you to use FMath::IsNearlyEqual function.
https://docs.unrealengine.com/4.26/en-US/API/Runtime/Core/Math/FMath/IsNearlyEqual/2/

1 Like

Privacy & Terms