Hello There!!
In this lecture, we discuss how to add sound effects to our game.
UE Editor-
Audio component attached to the door-
VS Code-
C File-
// Copyright Ateeb Ahmed 2021
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/TriggerVolume.h"
#include "OpenDoor.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDING_ESCAPE_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();
// Tracks whether the sound has benn played.
bool OpenDoorSound = false;
bool CloseDoorSound = true;
private:
float InitialYaw;
float CurrentYaw;
UPROPERTY(EditAnywhere)
float MassToOpenDoors = 35.f;
UPROPERTY(EditAnywhere)
float OpenAngle = 70.f;
float DoorLastOpened = 0.f;
UPROPERTY(EditAnywhere)
float DoorCloseDelay = 0.5f;
UPROPERTY(EditAnywhere)
float DoorOpenVelocity = 0.8f;
UPROPERTY(EditAnywhere)
float DoorCloseVelocity = 2.f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate = nullptr;
UPROPERTY(EditAnywhere)
UAudioComponent* AudioComponent = nullptr;
};
C++ file-
// Copyright Ateeb Ahmed 2021
#include "Engine/World.h"
#include "Components/AudioComponent.h"
#include "Components/PrimitiveComponent.h"
#include "GameFramework/PlayerController.h"
#include "OpenDoor.h"
#include "GameFramework/Actor.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; // Stores the current yaw(Z axis) of the door.
CurrentYaw = InitialYaw;
OpenAngle += InitialYaw;
FindPressurePlate();
FindAudioComponent();
}
void UOpenDoor::FindAudioComponent()
{
AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
if (!AudioComponent)
{
UE_LOG(LogTemp, Error, TEXT("%s Missing Audio Component!"), *GetOwner()->GetName());
}
}
void UOpenDoor::FindPressurePlate()
{
if(!PressurePlate)
{
UE_LOG(LogTemp, Error, TEXT("%s this actor does not have a pressure plate!!"), *GetOwner()->GetName());
}
}
// Called every frame
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);
}
}
}
void UOpenDoor::OpenDoor(float DeltaTime)
{
CurrentYaw = FMath::Lerp(CurrentYaw, OpenAngle, DeltaTime * DoorOpenVelocity); // Changing the yaw to the target yaw
FRotator DoorRotation = GetOwner()->GetActorRotation(); // Getting the rotation of the door
DoorRotation.Yaw = CurrentYaw; // Changing yaw of the door
GetOwner()->SetActorRotation(DoorRotation);
CloseDoorSound = false;
if(!AudioComponent) {return;}
if (!OpenDoorSound)
{
AudioComponent->Play();
OpenDoorSound = true;
}
}
void UOpenDoor::CloseDoor(float DeltaTime)
{
CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseVelocity); // Changing the yaw to the target yaw
FRotator DoorRotation = GetOwner()->GetActorRotation(); // Getting the rotation of the door
DoorRotation.Yaw = CurrentYaw; // Changing yaw of the door
GetOwner()->SetActorRotation(DoorRotation);
OpenDoorSound = false;
if(!AudioComponent) {return;}
if (!CloseDoorSound)
{
AudioComponent->Play();
CloseDoorSound = true;
}
}
float UOpenDoor::TotalMassOfActors() const
{
float TotalMass = 0.f;
// Find All Overlapping Actors.
TArray<AActor*> OverlappingActors;
// Add up thier Masses.
if(!PressurePlate) {return TotalMass;}
PressurePlate->GetOverlappingActors(OUT OverlappingActors);
// for(AActor* Actor : OverlappingActors)
for(AActor* Actor : OverlappingActors)
{
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
}
return TotalMass;
}
When the door opens, a door-opening sound is played, the same goes while the door is closing.
Don’t forget to like, share, suggest and reply!!
Previous post: My Building Escape after Pointer Protection Process? lecture - #3 by TP_MakeGames
Thanks for reading,
BYE!!