I do have the grabbing functionality from the course working just fine (even better after checking out this post), but what I’m trying to do is get other free-floating objects to follow another free-floating object I intend to grab and use as the key to open the path.
What I do is attach a MagneticBalls component I made to each free-floating object that I want to move towards a target object, as well as attaching a PhysicsHandle component.
In each MagneticBalls component in the editor, I set a target AActor that they should follow (exposed in the header file).
While I can get the PhysicsHandleComponent just fine, the current actor’s position, and the target actor’s position, using the physics handle to set target location doesn’t actually move the actor towards the target. Even if I manually move the target around, while all the location vectors are correctly updating, SetTargetLocation() still doesn’t actually move the actor.
What am I doing wrong?
MagneticBalls.cpp:
#include "MagneticBalls.h"
// Sets default values for this component's properties.
UMagneticBalls::UMagneticBalls()
{
// 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 UMagneticBalls::BeginPlay()
{
Super::BeginPlay();
GetPhysicsHandle();
// Warning if destination object isn't attached via editor.
if (!DestinationObject) {
UE_LOG(LogTemp, Error, TEXT("'%s' is missing a destination object!"), *GetOwner()->GetName());
}
// ...
}
// Called every frame.
void UMagneticBalls::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (DestinationObject) {
Elapsed += DeltaTime;
if (Elapsed > FollowUpdateRate) {
FindAndMoveToTarget();
Elapsed = 0;
}
}
// ...
}
/// Get physics handle component for this component's owner.
void UMagneticBalls::GetPhysicsHandle()
{
BallPhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (!BallPhysicsHandle) {
UE_LOG(LogTemp, Error,
TEXT("%s has no Physics Handle Component! Needed by MagneticBalls component."),
*GetOwner()->GetName()
);
}
}
/// Get owner's location, get target's location, calculate distance, then set target location for physics handle.
void UMagneticBalls::FindAndMoveToTarget()
{
// This object's current position.
CurrentPosition = GetOwner()->GetTransform().GetLocation();
// Our destination actor based on selection in editor.
Destination = DestinationObject->GetTransform().GetLocation();
// How far away this object is from the destination actor.
Distance = GetOwner()->GetDistanceTo(DestinationObject);
// TODO: This doesn't seem to do anything. <-------------
BallPhysicsHandle->SetTargetLocation(Destination);
}
and the header file:
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "PhysicsEngine/PhysicsHandleComponent.h"
#include "MagneticBalls.generated.h"
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class ESCAPE_API UMagneticBalls : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties.
UMagneticBalls();
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
protected:
// Called when the game starts.
virtual void BeginPlay() override;
private:
/// Our Physics Handle Component added to the actor our MagneticBalls component goes on.
UPROPERTY()
UPhysicsHandleComponent* BallPhysicsHandle;
/// The object we want our magnetic ball to move towards.
UPROPERTY(EditAnywhere)
AActor* DestinationObject;
UPROPERTY(EditAnywhere)
float FollowUpdateRate = 0.3f;
float Distance;
float Elapsed = 0;
FVector Destination;
FVector CurrentPosition;
// Functions.
void GetPhysicsHandle();
void FindAndMoveToTarget();
};
Example of balls/objects I want moving towards each other.
Does anyone have any ideas what I’m missing or not understanding?