Issue with Audio not playing on Door Close

Hey there.

I’ve been facing some issues with this part of the course.
The audio implementation is causing me issues and is not behaving the way I would expect (or is shown in the course material).

Things I tried confirming so far:

  • Audio Component is actually attached to my doors
  • Audio file is added to the doors
  • Open Door sound plays correctly
  • Close Door event fires correctly and log prints as per code below (as expected)
  • No sound is played on Close Door even though the code clearly executes as per log

The code here might be a bit messy as I was trying to figure out if my Close Door Event fires correctly and print some log outputs, but here is what I have:

BeginPlay

void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();

	//Door Yaw functionality
	InitialYaw = GetOwner()->GetActorRotation().Yaw;
	CurrentYaw = InitialYaw;
	DoorTargetYaw += InitialYaw;

	FindAudioComponent();
	PressurePlateWarning();
}

FindAudioComponent

void UOpenDoor::FindAudioComponent()
{
	AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();

	if (!AudioComponent)
	{
		UE_LOG(LogTemp, Error, TEXT("%s MISSING AUDIO COMPONENT!"), *GetOwner()->GetName());
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("%s attached to Audio Component."), *GetOwner()->GetName());
	}
}

TickComponent

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

	if (TotalMassOfActors() > MassToOpenDoors)
	{
		OpenDoor(DeltaTime);
		DoorLastOpened = GetWorld()->GetTimeSeconds();
	}
	else
	{
		if (GetWorld()->GetTimeSeconds() - DoorLastOpened > DoorCloseDelay)
		{
			CloseDoor(DeltaTime);
		}	
	}
}

DoorOpen

void UOpenDoor::OpenDoor(float DeltaTime)
{
	CurrentYaw = FMath::Lerp(CurrentYaw, DoorTargetYaw, DeltaTime * DoorOpenSpeed);
	DoorRotation = GetOwner()->GetActorRotation();
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

	if (!AudioComponent){return;}
	if (!OpenDoorSound)
	{
	AudioComponent->Play();
	OpenDoorSound = true;
	UE_LOG(LogTemp, Warning, TEXT("Open Door Sound on %s played."), *GetOwner()->GetName());
	CloseDoorSound = false;
	UE_LOG(LogTemp, Warning, TEXT("Close Door Sound for %s set to false."), *GetOwner()->GetName());
	}
}

Door Close

void UOpenDoor::CloseDoor(float DeltaTime)
{
	AudioComponent->Play();
	CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseSpeed);
	DoorRotation = GetOwner()->GetActorRotation();
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

	FindAudioComponent();
	if (!AudioComponent) { return; }
	if (!CloseDoorSound)
	{
		AudioComponent->Play();
		CloseDoorSound = true;
		UE_LOG(LogTemp, Warning, TEXT("Close Door Sound played on %s"), *GetOwner()->GetName());
		OpenDoorSound = false;
		UE_LOG(LogTemp, Warning, TEXT("Open Door Sound for %s set to false."), *GetOwner()->GetName());
	}
}

I’ve seen this issue pop up a few times on the forum but haven’t been able to find a resolution on this.
Anyone got any ideas?

Appreciate it!

Having this here would mean it’s constantly being played as CloseDoor is called in the else of Tick.

I copied your code with this line removed and it works as expected.

Oops!
Thank you, it does work now.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms