Problem with Opening Door Sound

The problem is that when the door opens there is no sound, but when the door is closing I hear the sound twice. I have tried logging out and it looks right, but it doesn’t perform as expected. Here is my code:
OpenDoor.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
#include "OpenDoor.h"
#include "GameFramework/Actor.h"
#include "Components/PrimitiveComponent.h"
#include "Components/AudioComponent.h"

#define OUT


// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();
	InitialYaw = GetOwner()->GetActorRotation().Yaw;
	CurrentYaw = InitialYaw;
	TargetYaw += InitialYaw;
	
	FindPressurePlate();
	FindAudioComponent();

}


// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	
	if(PressurePlate && TotalMassOfActors() >= MassThatOpens){		//PressurePlate->IsOverlappingActor(ActorOpeningDoor)
		OpenDoor(DeltaTime);
		LastOpenedDoor = GetWorld()->GetTimeSeconds();
	}
	else if(GetWorld()->GetTimeSeconds() >= LastOpenedDoor + DelayClosing)
	{	
		CloseDoor(DeltaTime);
	}
	
}

void UOpenDoor::OpenDoor(float DeltaTime)
{
	CurrentYaw = FMath::Lerp(CurrentYaw, TargetYaw, DeltaTime * OpeningTime);
	FRotator OpenDoor = GetOwner()->GetActorRotation();
	OpenDoor.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(OpenDoor);
	AudioComponent->Play();

	IsCloseSound = false;
	if(!IsOpenSound)
	{
		AudioComponent->Play();
		IsOpenSound = true;
		UE_LOG(LogTemp, Warning, TEXT("Door open sound playing!"));
	}
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
	CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * ClosingTime);
	FRotator OpenDoor = GetOwner()->GetActorRotation();
	OpenDoor.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(OpenDoor);
		
	IsOpenSound = false;
	if(!IsCloseSound)
	{
		AudioComponent->Play();
		IsCloseSound = true;
		UE_LOG(LogTemp, Warning, TEXT("Door close sound playing!"));
	}
	
}

float UOpenDoor::TotalMassOfActors() const
{
	float TotalMass = 0.f;
	TArray <AActor*> OverlappingActors;
	if(!PressurePlate) {return 0.f;}
	PressurePlate->GetOverlappingActors(OUT OverlappingActors);

	for(AActor* a : OverlappingActors)
	{
		TotalMass += a->FindComponentByClass <UPrimitiveComponent>()->GetMass();
	}
	return TotalMass;
}

void UOpenDoor::FindAudioComponent()
{
	AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
	if(!AudioComponent)
	{
		UE_LOG(LogTemp, Error, TEXT("%s doesn't have audio component!"), *GetOwner()->GetName());
	}
}

void UOpenDoor::FindPressurePlate()
{
		if(!PressurePlate){
		
		UE_LOG(LogTemp, Error, TEXT("%s has the open door component, but no pressureplate set!"), *GetOwner()->GetName());
	}
}

OpenDoor.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/TriggerVolume.h"
#include "OpenDoor.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UOpenDoor();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
	void OpenDoor(float DeltaTime);
	void CloseDoor(float DeltaTime);
	float TotalMassOfActors() const;
	void FindAudioComponent();
	void FindPressurePlate();
	

private:

	float InitialYaw;
	float CurrentYaw;
	
	bool IsOpenSound = false;
	bool IsCloseSound = true;

	UPROPERTY(EditAnywhere)
	float TargetYaw = -90.f;

	UPROPERTY(EditAnywhere)
	ATriggerVolume* PressurePlate = nullptr;

	float LastOpenedDoor = 0.f;

	UPROPERTY(EditAnywhere)
	float DelayClosing = 2.f;

	UPROPERTY(EditAnywhere)
	float ClosingTime = 1.f;

	UPROPERTY(EditAnywhere)
	float OpeningTime = 1.f;

	UPROPERTY(EditAnywhere)
	float MassThatOpens = 0.f;

	UPROPERTY()
	UAudioComponent* AudioComponent = nullptr;
};

Everything looks correct to me

Would you mind sending me your project using the following link?

https://gdev.tv/projectupload

Please use File > Package Project > Zip Up Project within Unreal as this will ensure only required files are zipped up and things like the Binaries are excluded.

Thanks, I have found the problem, it was my mistake in the code as i repeated AudioComponent->Play(); twice. :slight_smile:

Guess I was temporarily blind :sweat_smile:.

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

Privacy & Terms