The pressure plate I did set for the door that I’m attempting to open:
in OpenDoor.h I have:
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/TriggerVolume.h"
//leave last:
#include "OpenDoor.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class 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 TotalActorMass() const;
void FindAudioComponent();
void FindPressurePlate();
private:
float ClosedYaw;
float CurrentYaw;
bool DoorLockPlayedOpen;
bool DoorLockPlayedClose;
UPROPERTY(EditAnywhere)
float OpenYaw = 90.f;
float DoorLastOpened = 0.f;
UPROPERTY(EditAnywhere)
float DoorCloseDelay = 0.5f;
UPROPERTY(EditAnywhere)
float InterpSpeedOpen = 2.f;
UPROPERTY(EditAnywhere)
float InterpSpeedClose = 10.f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate = nullptr;
UPROPERTY(EditAnywhere)
float TargetOpenMass = 60.f;
UPROPERTY()
UAudioComponent* DoorLock = nullptr;
};
And in OpenDoor.cpp:
#include "OpenDoor.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
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();
ClosedYaw = GetOwner()->GetActorRotation().Yaw;
OpenYaw += ClosedYaw;
CurrentYaw = ClosedYaw;
FindPressurePlate();
FindAudioComponent();
DoorLockPlayedOpen = false;
DoorLockPlayedClose = false;
}
void UOpenDoor::FindPressurePlate(){
if(!PressurePlate)
UE_LOG(LogTemp, Error, TEXT("%s has no PressurePlate."), *GetOwner()->GetName());
}
void UOpenDoor::FindAudioComponent(){
DoorLock = GetOwner()->FindComponentByClass<UAudioComponent>();
if(!DoorLock){
UE_LOG(LogTemp, Error, TEXT("%s missing audio component DoorLock."), *GetOwner()->GetName());
}
}
// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction){
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if(TotalActorMass() >= TargetOpenMass){
OpenDoor(DeltaTime);
DoorLastOpened = GetWorld()->GetTimeSeconds();
}
else{
if(GetWorld()->GetTimeSeconds() > DoorLastOpened + DoorCloseDelay){
CloseDoor(DeltaTime);
}
}
}
void UOpenDoor::OpenDoor(float DeltaTime){
FRotator OpenDoor = GetOwner()->GetActorRotation();
CurrentYaw = FMath::FInterpTo(CurrentYaw, OpenYaw, DeltaTime, InterpSpeedOpen);
OpenDoor.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(OpenDoor);
if (!DoorLockPlayedOpen){
DoorLock->Play();
DoorLockPlayedOpen = true;
DoorLockPlayedClose = false;
}
}
void UOpenDoor::CloseDoor(float DeltaTime){
FRotator CloseDoor = GetOwner()->GetActorRotation();
CurrentYaw = FMath::FInterpTo(CurrentYaw, ClosedYaw, DeltaTime, InterpSpeedClose);
CloseDoor.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(CloseDoor);
if (!DoorLockPlayedClose && !DoorLockPlayedOpen){
return;
}
else if (!DoorLockPlayedClose){
DoorLock->Play();
DoorLockPlayedClose = true;
DoorLockPlayedOpen = false;
}
}
float UOpenDoor::TotalActorMass() const{
float TotalMass = 0.f;
//Find all overlapping actors
TArray<AActor*> OverlappingActors;
if(PressurePlate){
PressurePlate->GetOverlappingActors(OverlappingActors);
//Add up the masses
for (AActor* Actor: OverlappingActors){
//UE_LOG(LogTemp, Warning, TEXT("%s on the pressure plate."), *Actor->GetName())
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
}
}
return TotalMass;
}