Trying to move a door (Block) to a new transform when triggered. I'm stuck :_(

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 :_(

iv been on this all day… zero results :_(

BlockMovement.Pos = CurrentPos;

FTransform doesn’t have a Pos member. There’s the following

Presuming you want to move in the Z for up/down. You would need

BlockMovement.Translation.Z = ...

However, since you aren’t using anything other than the location of the transform, it would make more sense to use GetActorLocation rather than Transform. Then that would just be

BlockMovement.Z = ...

Thank you Dan, ill give that a try. see what i come up with.

AHHHH…!!! It worked! I did it! I followed instructions and it worked!!
Thank you Thank you Thank you Dan… Legend! AMAZBALLS!

Take a look at this handsome bit of code.

#include "ObjectMover.h"
#include "Components/AudioComponent.h"
#include "Components/PrimitiveComponent.h"
#include "Engine/World.h"
#include "GameFramework/Actor.h"
#include "GameFramework/PlayerController.h"

#define OUT

UObjectMover::UObjectMover()
{
	PrimaryComponentTick.bCanEverTick = true;
}

void UObjectMover::BeginPlay()
{
	Super::BeginPlay();
	
	InitialPos = GetOwner()->GetActorLocation().Z; 
	CurrentPos = InitialPos;                         
	NewObjectPos += InitialPos;                         

	FindpressurePlate();
	FindAudioComponent();
}

void UObjectMover::FindAudioComponent()
{
	AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();

	if (!AudioComponent)
	{	
		UE_LOG(LogTemp, Error, TEXT("%s Missing audio component!"), *GetOwner()->GetName());
	}
}

void UObjectMover::FindpressurePlate()
{
	if(!PressurePlate)
	{
		UE_LOG(LogTemp, Error, TEXT("%s MoveObject component, No pressureplate"), *GetOwner()->GetName());	
	}
}

void UObjectMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	if (TotalMassOfActors() > MassToMoveObject)
	{
		MoveObject(DeltaTime);
		OldObjectPos = GetWorld()->GetTimeSeconds();
	}
	else
	{
		// if the Object has been moved longer than MoveBackObjectDelay
		if (GetWorld()->GetTimeSeconds() - OldObjectPos > MoveBackObjectDelay)
		{
			MoveBackObject(DeltaTime);
		}	
	}	
}

void UObjectMover::MoveObject(float DeltaTime)
{
	CurrentPos = FMath::Lerp(CurrentPos, NewObjectPos, DeltaTime * MoveObjectSpeed);
	FVector ObjectPosition = GetOwner()->GetActorLocation();
	ObjectPosition.Z = CurrentPos;
	GetOwner()->SetActorLocation(ObjectPosition);	

	MoveBackObjectSound = false;
	if (!AudioComponent) {return;}
	if (!MoveObjectSound)
	{
		AudioComponent->Play();
		MoveObjectSound = true;
	}

}
void UObjectMover::MoveBackObject(float DeltaTime)
{
	CurrentPos = FMath::Lerp(CurrentPos, InitialPos, DeltaTime * MoveBackObjectSpeed);
	FVector ObjectPosition = GetOwner()->GetActorLocation();
	ObjectPosition.Z = CurrentPos;
	GetOwner()->SetActorLocation(ObjectPosition);	

	MoveObjectSound = false;
	if (!AudioComponent) {return;}
	if (!MoveBackObjectSound) 
	{
		AudioComponent->Play();
		MoveBackObjectSound = true;
	}
}

float UObjectMover::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;
}

Is it possible to move object on the Y, Z and X on this same script? and have that edit anywhere?

Imagine the power that would give a level designer like me muhahaha! xD

Yes, make your variable am FVector instead of float.

Privacy & Terms