Hey guys. I’m trying to move a gate to slide up when triggered. I cant work out how to do it. I’m not even sure what to search for anymore. I have tried to pull something together from what we have been shown but I know its not right…
I’m a tad lost. I’m thinking it has something to do with maybe “Vectors” like a vector3.
-------------------- Header -----------------
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/TriggerVolume.h"
#include "BlockMover.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class SKEDADDLE_ROOMS_API UBlockMover : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UBlockMover();
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 BlockNewPos(float DeltaTime);
void BlockOldPos(float DeltaTime);
float TotalMassOfActors() const;
void FindAudioComponent();
void FindpressurePlate();
// Tracks whether the sound has been played.
bool BlockMoveSound = false;
bool BlockBackSound = true;
private:
//to move block (My code eek)
float InitalPos;
float CurrentPos;
UPROPERTY(EditAnywhere)
float PosToBe = 0.f;
float BlockLastMoved = 0.f;
UPROPERTY(EditAnywhere)
float MassToMoveBlock = 50.f;
UPROPERTY(EditAnywhere)
float BlockMoveDelay = .5f;
UPROPERTY(EditAnywhere)
float BlockMoveSpeed = .8f;
UPROPERTY(EditAnywhere)
float BlockReturnSpeed = 2.f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate = nullptr;
UPROPERTY()
UAudioComponent* AudioComponent = nullptr;
};
------------ CPP -------------------
#include "BlockMover.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
UBlockMover::UBlockMover()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UBlockMover::BeginPlay()
{
Super::BeginPlay();
InitialPos = GetOwner()->GetActorTransform().Pos;
CurrentPos = InitialPos;
PosToBe += InitialPos; //OpenAngle = OpenAngle + InitialYaw;
FindpressurePlate();
FindAudioComponent();
}
void UBlockMover::FindAudioComponent()
{
AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
if (!AudioComponent)
{
UE_LOG(LogTemp, Error, TEXT("%s Missing audio component!"), *GetOwner()->GetName());
}
}
void UBlockMover::FindpressurePlate()
{
if(!PressurePlate)
{
UE_LOG(LogTemp, Error, TEXT("%s has the open door component on it, but no pressureplate set!"), *GetOwner()->GetName());
}
}
// Called every frame
void UBlockMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (TotalMassOfActors() > MassToMoveBlock)
{
BlockNewPos(DeltaTime);
BlockLastMoved = GetWorld()->GetTimeSeconds();
}
else
{
if (GetWorld()->GetTimeSeconds() - BlockLastMoved > BlockMoveDelay)
{
BlockOldPos(DeltaTime);
}
}
}
void UBlockMover::BlockNewPos(float DeltaTime)
{
CurrentPos = FMath::Lerp(CurrentPos, PosToBe, DeltaTime * BlockMoveSpeed);
FTransform BlockMovement = GetOwner()->GetActorTransform();
BlockMovement.Pos = CurrentPos;
GetOwner()->SetActorTransform(BlockMovement);
BlockMoveSound = false;
if (!AudioComponent) {return;}
if (!BlockMoveSound)
{
AudioComponent->Play();
BlockMoveSound = true;
}
}
void UBlockMover::BlockOldPos(float DeltaTime)
{
CurrentPos = FMath::Lerp(CurrentPos, InitialPos, DeltaTime * BlockReturnSpeed);
FTransform BlockMovement = GetOwner()->GetActorTransform();
BlockMovement.Pos = CurrentPos;
GetOwner()->SetActorTransform(BlockMovement);
BlockMoveSound = false;
if (!AudioComponent) {return;}
if (!BlockReturnSound)
{
AudioComponent->Play();
BlockReturnSound = true;
}
}
float UBlockMover::TotalMassOfActors() const
{
float TotalMass = 0.f;
// Find All Overlapping Actors.
TArray<AActor*> OverlappingActors;
if (!PressurePlate) {return TotalMass;}
PressurePlate->GetOverlappingActors(OUT OverlappingActors);
// Add Up Their Masses.
for(AActor* Actor : OverlappingActors)
{
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
}
return TotalMass;
}
`
![Screenshot (214)|690x265](upload://xnCnWJ7yNptNA2Wh9eT6XXJbMF.jpeg) ``
iv been on this all day.. zero results :_(