I have an issue with AudioComponent->Play(); and UE

Mike’s issue that appeared at 10:06 did not appear for me.
When I am calling AudioComponent->Play() then I press Play in Unreal I don’t hear anything but if I click outside the viewport in UE, the sound plays once!
I should receive an infinite loop of the open door sound, right??
OpenDoor.h

// StrivnX

#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:
	// Lerping
	float InitialYaw;
	float CurrentYaw;
	UPROPERTY(EditAnywhere)
	float TargetYaw = -90.f;
	UPROPERTY(EditAnywhere)
	float AlphaOpen = 1.f;
	UPROPERTY(EditAnywhere)
	float AlphaClose = 1.f;

	// Closing the Door with Delay
	float DoorLastOpened = 0.f;
	UPROPERTY(EditAnywhere)
	float DoorCloseDelay = .5f;

	// pick up the TriggerVolume & Player
	UPROPERTY(EditAnywhere)
	ATriggerVolume* PressurePlate = nullptr;

	UPROPERTY(EditAnywhere)
	float MassToOpen = 40.f;

	UPROPERTY()
	UAudioComponent* AudioComponent = nullptr;

	bool OpeneDoorSound = false;
	bool CloseDoorSound = true;

};

OpenDoor.cpp

// StrivnX

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


// 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();
}

void UOpenDoor::FindPressurePlate()
{
	if (!PressurePlate)
	{
		UE_LOG(LogTemp, Error, TEXT("[MISSING]%s: has the OpenDoor component on it, but no pressureplate set!"), *GetOwner()->GetName());
	}
}
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("AudioComponent Was Found"));
	}
}


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

	if (TotalMassOfActors() > MassToOpen)
	{
		OpenDoor(DeltaTime);
		DoorLastOpened = GetWorld()->GetTimeSeconds();
	}
	else
	{
		// if the door has been open long than DoorCloseDelay
		if (GetWorld()->GetTimeSeconds() - DoorLastOpened > DoorCloseDelay)
		{
			CloseDoor(DeltaTime);
		}
	}
}
void UOpenDoor::OpenDoor(float DeltaTime)
{
	AudioComponent->Play();
	//GetActorRotation
	FRotator OpenRotation = GetOwner()->GetActorRotation();
	//Make Lerp then set CurrentYaw to Lerp.value
	CurrentYaw = FMath::Lerp(CurrentYaw, TargetYaw, DeltaTime * AlphaOpen);
	//Set OpenRotation.Yaw to the CurrentYaw
	OpenRotation.Yaw = CurrentYaw;
	//Set it OpenRotation the Actor
	GetOwner()->SetActorRotation(OpenRotation);
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
	//GetActorRotation
	FRotator CloseDoor = GetOwner()->GetActorRotation();
	//Make Lerp and set CurrentYaw to Lerp.Value
	CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * AlphaClose);
	CloseDoor.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(CloseDoor);
	AudioComponent->Play();
}

float UOpenDoor::TotalMassOfActors() const
{
	float TotalMass = 0.f;
	if (!PressurePlate) { return TotalMass; }
	// Find All Overlapping Actors
	TArray<AActor*> OverlappingActors{ nullptr };
	PressurePlate->GetOverlappingActors(OverlappingActors);
	// Add up their masses
	for (AActor* Actor: OverlappingActors)
	{ 
		UE_LOG(LogTemp, Error, TEXT("Num: %i"), OverlappingActors.Num());
		TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
	}
	return TotalMass;
}

When I click on Those spots The sound plays once!
another issue is when the doors are opening The sound Doesn’t work.

Thanks advance

You are calling Play every tick.

void UOpenDoor::OpenDoor(float DeltaTime)
{
	AudioComponent->Play();

OpenDoor is called in Tick so this is constantly going to call Play resulting in no sound.

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

Privacy & Terms