My SFX & Audio Code

Basically the only difference is, I used the function AudioComponent->IsPlaying().

void UOpenDoor::OpenDoor(float DeltaTime)
{
	CurrentYaw = FMath::FInterpConstantTo(GetOwner()->GetActorRotation().Yaw, TargetYaw, DeltaTime, DoorOpenSpeed);

	NewRotation.Yaw = CurrentYaw;

	if (NewRotation.Yaw != TargetYaw)
	{
		GetOwner()->SetActorRotation(NewRotation);
	}

	if (!AudioComponent) { return; }
	FindAudioComponent();
	if (AudioPlayed == false && AudioComponent->IsPlaying() == false)
	{
		AudioComponent->Play();
		AudioPlayed = true;
	}
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
	CurrentYaw = FMath::FInterpConstantTo(GetOwner()->GetActorRotation().Yaw, InitialYaw, DeltaTime, DoorCloseSpeed);
	NewRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(NewRotation);
	
	if (!AudioComponent) { return; }
	FindAudioComponent();
	if (AudioPlayed == true && AudioComponent->IsPlaying() == false)
	{
		AudioComponent->Play();
		AudioPlayed = false;
	}
}
1 Like

Thanks for sharing!

Privacy & Terms