nope,
Here is my entire code maybe it will shed some light…
// Fill out your copyright notice in the Description page of Project Settings.
#include "OpenDoor.h"
#include "Engine/World.h"
#include "Components/PrimitiveComponent.h"
#include "Components/AudioComponent.h"
#include "GameFramework/PlayerController.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;
}
void UOpenDoor::FindAudioComponent()
{
AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
if(!AudioComponent){
UE_LOG(LogTemp, Error , TEXT("%s has the OpenDoor has no audio component set!"), *GetOwner()->GetName());
}
}
void UOpenDoor::FindpressurePlate()
{
if(!PressurePlate)
{
UE_LOG(LogTemp, Error, TEXT("%s has the open door component on it, but no pressureplate set!"), *GetOwner()->GetName());
}
}
// Called when the game starts
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();
InitialYaw = GetOwner()->GetActorRotation().Yaw;
CurrentYaw = InitialYaw;
OpenAngle += InitialYaw;
FindAudioComponent();
FindpressurePlate();
}
// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
//Adding the PressurePlate in order to make sure the pointer is not NULL;
if (PressurePlate && TotalMassOfActors() > MassToOpenDoor)
{
OpenDoor(DeltaTime);
DoorOpenTime = GetWorld()->GetTimeSeconds();
}
else if( GetWorld()->GetTimeSeconds() - DoorOpenTime > DoorCloseDelay)
{
CloseDoor(DeltaTime);
}
}
float UOpenDoor::TotalMassOfActors() const
{
float TotalMass = 0.f;
//find ovelapping actors and accumulate mass
TArray<AActor*> OverlappingActors;
if (!PressurePlate) {return TotalMass;}
PressurePlate->GetOverlappingActors(OUT OverlappingActors);
for (AActor* Actor : OverlappingActors)
{
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
}
return TotalMass;
}
void UOpenDoor::OpenDoor(float DeltaTime)
{
CurrentYaw = FMath::Lerp(CurrentYaw, OpenAngle, DeltaTime * DoorOpenSpped);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
if(!AudioComponent) {return;}
UE_LOG(LogTemp, Warning , TEXT("will play sound on Open %s"), bPlaySound)
if(bPlaySound)
{
// AudioComponent->Play();
bPlaySound = false;
}
}
void UOpenDoor::CloseDoor(float DeltaTime)
{
CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseSpeed);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
if(!AudioComponent) {return;}
UE_LOG(LogTemp, Warning , TEXT("will play sound on Close %s"), !bPlaySound)
if(!bPlaySound)
{
// AudioComponent->Play();
bPlaySound = true;
}
}