Above is the link to the Medieval Escape Building Project, there I wrote fresh codes for Opening Doors and Playing Sounds, and because of that, now my door is playing sound at the correct time when it should play. But still, when I press the play button it plays the sound for once in the beginning.
My fresh code for opening door:-
.H File:-
// 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 "DoorOpenMedieval.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class ESCAPEBUILDING2_API UDoorOpenMedieval : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UDoorOpenMedieval();
void OpenDoor(float DeltaTime);
void PressurePlate(float DeltaTime);
void CloseDoor(float DeltaTime);
void SettingUp();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
bool PlaySound = true;
bool CloseSound = true;
float InitialYaw = 0.f;
UPROPERTY()
APawn* PawnThatOpens = nullptr;
UPROPERTY(EditAnywhere)
float DoorRotationValue = 90.f;
UPROPERTY(EditAnywhere)
float DoorOpenTime = 0.5f;
UPROPERTY(EditAnywhere)
float DoorCloseTime = 0.5f;
UPROPERTY(EditAnywhere)
ATriggerVolume* TriggerVolume = nullptr;
UPROPERTY(EditAnywhere)
AActor* ActorThatOpens = nullptr;
UPROPERTY(EditAnywhere)
AActor* ActorThatCloses = nullptr;
UPROPERTY()
UAudioComponent* SoundComponent = nullptr;
};
CPP File:-
// Fill out your copyright notice in the Description page of Project Settings.
#include "DoorOpenMedieval.h"
#include "Components/AudioComponent.h"
#include "GameFramework/Actor.h"
#include "GameFramework/Pawn.h"
#include "GameFramework/PlayerController.h"
#include "Engine/World.h"
// Sets default values for this component's properties
UDoorOpenMedieval::UDoorOpenMedieval()
{
// 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 UDoorOpenMedieval::BeginPlay()
{
Super::BeginPlay();
SettingUp();
// ...
}
// Called every frame
void UDoorOpenMedieval::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
PressurePlate(DeltaTime);
}
void UDoorOpenMedieval::OpenDoor(float DeltaTime)
{
FRotator DoorRotation = GetOwner()->GetActorRotation();
float CurrentYaw = DoorRotation.Yaw;
CurrentYaw = FMath::Lerp(CurrentYaw, DoorRotationValue, DeltaTime * DoorOpenTime);
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
if (!SoundComponent)
{
return;
}
if (CurrentYaw <= InitialYaw + 10.0f)
{
if (PlaySound)
{
SoundComponent->Play();
PlaySound = false;
}
}
}
void UDoorOpenMedieval::CloseDoor(float DeltaTime)
{
FRotator DoorRotation = GetOwner()->GetActorRotation();
float CurrentYaw = DoorRotation.Yaw;
CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseTime);
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
if (!SoundComponent)
{
return;
}
if (CurrentYaw <= InitialYaw + 10.0f)
{
if (CloseSound)
{
SoundComponent->Play();
CloseSound = false;
}
}
}
void UDoorOpenMedieval::PressurePlate(float DeltaTime)
{
if (!TriggerVolume)
{
UE_LOG(LogTemp, Error, TEXT("Trigger Volume not found in %s actor."), *GetOwner()->GetName());
return;
}
if (ActorThatCloses && TriggerVolume->IsOverlappingActor(ActorThatCloses))
{
CloseDoor(DeltaTime);
PlaySound = true;
}
else if (ActorThatCloses && !(TriggerVolume->IsOverlappingActor(ActorThatCloses)))
{
OpenDoor(DeltaTime);
CloseSound = true;
}
else if (ActorThatOpens && TriggerVolume->IsOverlappingActor(ActorThatOpens))
{
OpenDoor(DeltaTime);
CloseSound = true;
}
else if (!ActorThatOpens && PawnThatOpens && TriggerVolume->IsOverlappingActor(PawnThatOpens))
{
OpenDoor(DeltaTime);
CloseSound = true;
}
else
{
CloseDoor(DeltaTime);
PlaySound = true;
}
}
void UDoorOpenMedieval::SettingUp()
{
InitialYaw = GetOwner()->GetActorRotation().Yaw;
PawnThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
SoundComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
if (!SoundComponent)
{
return;
}
}
And yes there is no Comments because I thought that I am the only one whose gonna read this code but now I regret. But still I thinks that You will understand the code where I played the sound. And in the PressurePlate function I used this many Conditional loops cause I used the same component on all the doors in the level and the condition for opening for all the doors are different.