Problem no.1:- Door Sound automatically plays once when I press the play button.
Note:- Auto Active is turned off.
CPP:-
// Fill out your copyright notice in the Description page of Project Settings.
#include "DoorMovement.h"
#include "Components/AudioComponent.h"
#include "Components/PrimitiveComponent.h"
#include "Engine/World.h"
#include "GameFramework/Actor.h"
#include "GameFramework/PlayerController.h"
#define OUT
// Sets default values for this component's properties
UDoorMovement::UDoorMovement()
{
// 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 UDoorMovement::BeginPlay()
{
Super::BeginPlay();
// UE_LOG(LogTemp, Warning, TEXT("Name: %s"), *GetOwner()->GetName()) //Debug line to check which-which actor has this component attached
PressurePlateCheck();
InitializeBeginning();
FindSoundComponent();
}
// Called every frame
void UDoorMovement::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
DoorOperations(DeltaTime);
}
void UDoorMovement::OpenDoor(float DeltaTime) // Opens the door
{
FRotator DoorRotation = GetOwner()->GetActorRotation();
CurrentYaw = FMath::Lerp(CurrentYaw, OpenAngle, DeltaTime * DoorOpenSpeed);
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
if (!SoundComponent)
{
return;
} // Debug Line
if (CurrentYaw <= InitialYaw + 10.0f)
{
if (PlaySound) // Binary switch to play sound when door gets open
{
SoundComponent->Play();
PlaySound = false;
}
}
}
void UDoorMovement::CloseDoor(float DeltaTime) // Closes the door
{
FRotator DoorRotation = GetOwner()->GetActorRotation();
CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseSpeed);
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
if (!SoundComponent)
{
return;
} // Debug Line
if (CurrentYaw <= InitialYaw + 5.0f)
{
if (StopSound) // Binary switch to play sound when door gets closed
{
SoundComponent->Play();
StopSound = false;
}
}
}
float UDoorMovement::TotalMassFunction() const
{
float Mass = 0.f;
TArray<AActor *> OverlappingActors;
if (!PressurePlate)
{
return Mass;
}
PressurePlate->GetOverlappingActors(OUT OverlappingActors);
for (AActor *Actor : OverlappingActors)
{
Mass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
}
return Mass;
}
void UDoorMovement::PressurePlateCheck() const
{
if (!PressurePlate) // This will check the all the actor have this component has volume trigger set
{
UE_LOG(LogTemp, Error, TEXT("Please ensure that you initialized trigger volume to %s Actor."), *GetOwner()->GetName());
}
}
void UDoorMovement::InitializeBeginning()
{
InitialYaw = GetOwner()->GetActorRotation().Yaw;
CurrentYaw = InitialYaw;
OpenAngle = InitialYaw + OpenAngle;
// ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn(); // Setting the actor which will open the door when entering in trigger volume
}
void UDoorMovement::DoorOperations(float DeltaTime)
{
if (PressurePlate && TotalMassFunction() >= MassToOpenDoor) // This will exute only when Pressure plate will not be null and declared actor or pawn will be in tigger volume zone
{
OpenDoor(DeltaTime);
DoorOpenTime = GetWorld()->GetTimeSeconds(); // Sets the time of opening door most recently
StopSound = true; // Resets the binary switch value to default so that sound can be played one again
}
else
{
if (GetWorld()->GetTimeSeconds() - DoorOpenTime > CloseDoorDelaySeconds) // This line will let close the door only if the delay of given seconds has been made since last opening the door
CloseDoor(DeltaTime);
PlaySound = true; // Resets the binary switch value to default so that sound can be played one again
}
}
void UDoorMovement::FindSoundComponent() // Finds that if audio component exists or not
{
SoundComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
if (!SoundComponent)
{
UE_LOG(LogTemp, Error, TEXT("Audio component is missing in %s actor."), *GetOwner()->GetName());
return;
}
}
Header:-
// 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 "DoorMovement.generated.h"
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class ESCAPEBUILDING2_API UDoorMovement : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UDoorMovement();
void CloseDoor(float DeltaTime); // This fuction is to close the door
void DoorOperations(float DeltaTime);
void FindSoundComponent();
void InitializeBeginning();
void OpenDoor(float DeltaTime); // This fuction is to open the door
void PressurePlateCheck() const;
float TotalMassFunction() const;
bool PlaySound = true;
bool StopSound = true;
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:
float InitialYaw;
float CurrentYaw;
float DoorOpenTime = 0.f;
UPROPERTY(EditAnywhere)
float MassToOpenDoor = 50.f;
UPROPERTY(EditAnywhere)
float OpenAngle = 90.f;
UPROPERTY(EditAnywhere)
float DoorOpenSpeed = 1.f;
UPROPERTY(EditAnywhere)
float DoorCloseSpeed = .5f;
UPROPERTY(EditAnywhere)
float CloseDoorDelaySeconds = 2.f;
UPROPERTY(EditAnyWhere)
ATriggerVolume *PressurePlate = nullptr;
UPROPERTY()
UAudioComponent* SoundComponent = nullptr;
// UPROPERTY(EditAnywhere)
// AActor *ActorThatOpens = nullptr;
};
Problem No.2:- I have written the code of playing the sound as the sound should play only if while opening the door, door rotation is between initial Yaw and Initial Yaw + 10 degrees, and while closing the door, door rotation is between initial Yaw and Initial Yaw + 10 degrees. But the sound is playing every time I enter and leave the Trigger Volume.