So I’m in the middle of doing my classes for Unreal 5 & C++, but I’m stuck at the part where I have to make the door that opens when a specific tag enters the trigger area. I’ve been following the instructions closely and have been retracing my steps, and I’ve made a step in the right direction: apparently it prints regardless of whether or not my tag-required object enters the tagging zone. I’m also half-considering the fact that the collision presets might be wrong.
Here’s the code I have so far for troubleshooting:
TriggerArea.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "WallMover.h"
#include "TriggerArea.generated.h"
/**
*
*/
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CRYPTRAIDER_API UTriggerArea : public UBoxComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UTriggerArea();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
UFUNCTION(BlueprintCallable)
void SetWallMover(UWallMover* WallMover);
private:
UPROPERTY(EditAnywhere)
FName AcceptableActorTag;
UWallMover* WallMover;
AActor* GetAcceptableActor() const;
};
TriggerArea.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "TriggerArea.h"
UTriggerArea::UTriggerArea()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UTriggerArea::BeginPlay()
{
Super::BeginPlay();
}
void UTriggerArea::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
AActor* Actor = GetAcceptableActor();
if (Actor != nullptr)
{
UE_LOG(LogTemp, Display, TEXT("Unlocking"));
//WallMover->SetShouldMove(true);
}
else
{
UE_LOG(LogTemp, Display, TEXT("Unlocking"));
//WallMover->SetShouldMove(false);
}
}
void UTriggerArea::SetWallMover(UWallMover* NewMover)
{
WallMover = NewMover;
}
AActor* UTriggerArea::GetAcceptableActor() const
{
AActor* ReturnActor = nullptr;
TArray<AActor*> Actors;
GetOverlappingActors(Actors);
for (AActor* Actor : Actors)
{
if (Actor->ActorHasTag(AcceptableActorTag))
{
return Actor;
}
}
return nullptr;
}
WallMover.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "WallMover.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CRYPTRAIDER_API UWallMover : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UWallMover();
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 SetShouldMove(bool ShouldMove);
private:
UPROPERTY(EditAnywhere)
FVector MoveOffset;
UPROPERTY(EditAnywhere)
float MoveTime = 4;
bool ShouldMove = false;
FVector OriginalLocation;
};
WallMover.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "WallMover.h"
#include "Math/UnrealMathUtility.h"
// Sets default values for this component's properties
UWallMover::UWallMover()
{
// 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 UWallMover::BeginPlay()
{
Super::BeginPlay();
OriginalLocation = GetOwner()->GetActorLocation();
}
// Called every frame
void UWallMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (ShouldMove) {
FVector CurrentLocation = GetOwner()->GetActorLocation();
FVector TargetLocation = OriginalLocation + MoveOffset;
float Speed = FVector::Distance(OriginalLocation, TargetLocation) / MoveTime;
FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, Speed);
GetOwner()->SetActorLocation(NewLocation);
}
}
void UWallMover::SetShouldMove(bool NewShouldMove)
{
ShouldMove = NewShouldMove;
}
As you may have noticed for “TriggerArea.cpp”, I’ve commented out what should’ve been what makes the wall move, but unfortunately, I could not get it to work and don’t know where to go from here. Any help is appreciated, thank you in advance.