// Fill out your copyright notice in the Description page of Project Settings.
#include “TriggerComponent.h”
#include “Mover.h”
UTriggerComponent::UTriggerComponent()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UTriggerComponent::BeginPlay()
{
Super::BeginPlay();
}
void UTriggerComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
PrimaryComponentTick.bCanEverTick = true;
if (Mover == nullptr)
{
return;
}
AActor* Actor = GetAcceptableActor();
if (Actor != nullptr)
{
UPrimitiveComponent* Component = Cast<UPrimitiveComponent>(Actor->GetRootComponent());
if (Component != nullptr)
{
Component->SetSimulatePhysics(false);
}
Actor->AttachToComponent(this, FAttachmentTransformRules::KeepWorldTransform);
Mover->SetShouldMove(true);
}
else
{
Mover->SetShouldMove(false);
}
}
void UTriggerComponent::SetMover(UMover* NewMover)
{
Mover = NewMover;
}
AActor* UTriggerComponent::GetAcceptableActor() const
{
TArray<AActor*> Actors;
GetOverlappingActors(Actors);
for (AActor* Actor : Actors)
{
bool HasAcceptableTag = Actor->ActorHasTag(AcceptableActorTag);
bool IsGrabbed = Actor->ActorHasTag(“Alija”);
if (HasAcceptableTag && !IsGrabbed)
{
return Actor;
}
}
return nullptr;
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “Components/BoxComponent.h”
#include “Mover.h”
#include “TriggerComponent.generated.h”
/**
*
*/
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class OLD_TALES_API UTriggerComponent : public UBoxComponent
{
GENERATED_BODY()
public:
UTriggerComponent();
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 SetMover(UMover* Mover);
private:
UPROPERTY(EditAnywhere)
FName AcceptableActorTag;
UMover* Mover;
AActor* GetAcceptableActor() const;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include “Grabber.h”
#include “Engine/World.h”
#include “DrawDebugHelpers.h”
// Sets default values for this component’s properties
UGrabber::UGrabber()
{
// 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 UGrabber::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle && PhysicsHandle->GetGrabbedComponent())
{
FVector TargetLocation = GetComponentLocation() + GetForwardVector() * HoldDistance;
PhysicsHandle->SetTargetLocationAndRotation(TargetLocation, GetComponentRotation());
}
}
void UGrabber::Grab()
{
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle == nullptr)
{
return;
}
FHitResult HitResult;
bool HasHit = GetGrabbableInReach(HitResult);
if (HasHit)
{
UPrimitiveComponent* HitComponent = HitResult.GetComponent();
HitComponent->SetSimulatePhysics(true);
HitComponent->WakeAllRigidBodies();
AActor* HitActor = HitResult.GetActor();
HitActor->Tags.Add("Grabbed");
HitActor->DetachFromActor(FDetachmentTransformRules::KeepWorldTransform);
PhysicsHandle->GrabComponentAtLocationWithRotation(
HitComponent,
NAME_None,
HitResult.ImpactPoint,
GetComponentRotation());
}
}
void UGrabber::Release()
{
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle && PhysicsHandle->GetGrabbedComponent())
{
AActor* GrabbedActor = PhysicsHandle->GetGrabbedComponent()->GetOwner();
GrabbedActor->Tags.Remove("Grabbed");
PhysicsHandle->ReleaseComponent();
}
}
UPhysicsHandleComponent* UGrabber::GetPhysicsHandle() const
{
UPhysicsHandleComponent* Result = GetOwner()->FindComponentByClass();
if (Result == nullptr)
{
UE_LOG(LogTemp, Error, TEXT(“Grabber requires a UPhysicsHandleComponent.”));
}
return Result;
}
bool UGrabber::GetGrabbableInReach(FHitResult& OutHitResult) const
{
FVector Start = GetComponentLocation();
FVector End = Start + GetForwardVector() * MaxGrabDistance;
DrawDebugLine(GetWorld(), Start, End, FColor::Red);
DrawDebugSphere(GetWorld(), End, 10, 10, FColor::Blue, false, 5);
FCollisionShape Sphere = FCollisionShape::MakeSphere(GrabRadius);
return GetWorld()->SweepSingleByChannel(
OutHitResult,
Start, End,
FQuat::Identity,
ECC_GameTraceChannel2,
Sphere);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “Components/SceneComponent.h”
#include “PhysicsEngine/PhysicsHandleComponent.h”
#include “Grabber.generated.h”
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class OLD_TALES_API UGrabber : public USceneComponent
{
GENERATED_BODY()
public:
// Sets default values for this component’s properties
UGrabber();
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 Grab();
UFUNCTION(BlueprintCallable)
void Release();
private:
UPROPERTY(EditAnywhere)
float MaxGrabDistance = 400;
UPROPERTY(EditAnywhere)
float GrabRadius = 100;
UPROPERTY(EditAnywhere)
float HoldDistance = 200;
UPhysicsHandleComponent* GetPhysicsHandle() const;
bool GetGrabbableInReach(FHitResult& OutHitResult) const;
};
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “Components/ActorComponent.h”
#include “Mover.generated.h”
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class OLD_TALES_API UMover : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component’s properties
UMover();
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;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include “Mover.h”
#include “Math/UnrealMathUtility.h”
// Sets default values for this component’s properties
UMover::UMover()
{
// 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 UMover::BeginPlay()
{
Super::BeginPlay();
OriginalLocation = GetOwner()->GetActorLocation();
}
// Called every frame
void UMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
FVector TargetLocation = OriginalLocation;
if (ShouldMove)
{
TargetLocation = OriginalLocation + MoveOffset;
}
FVector CurrentLocation = GetOwner()->GetActorLocation();
float Speed = MoveOffset.Length() / MoveTime;
FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, Speed);
GetOwner()->SetActorLocation(NewLocation);
}
void UMover::SetShouldMove(bool NewShouldMove)
{
ShouldMove = NewShouldMove;
}
I literally copied the code and my mover in blueprints is set accordingly, I have no idea what to do