I noticed I could grab a chair and walk into a different room while the chair was stuck in the door way. I could walk very far away and come back to find I was still holding it.
I added this code to make it auto-release if you get separated too far from your grabbed object.
In Grabber.h - I have a “MaxGrabDistance” that is slightly larger than my “ReachDistance”
Private:
UPROPERTY(EditAnywhere)
float ReachDistance = 100.0f;
UPROPERTY(EditAnywhere)
float MaxGrabDistance = 150.0f;
//
AActor* GrabbedActor = nullptr;
// Return distance from player to object
float GetDistanceFromGrabbed();
In Grabber.cpp, inside the TickComponent - If there is a grabbed component then each tick will evaluate whether it is too far away:
//If physics handle is attached
if (PhysicsHandle->GrabbedComponent)
{
//Move the object that we're holding
PhysicsHandle->SetTargetLocation(LineTraceEnd);
// If LineTraceEnd is too far from grabbed object, release it.
if (GetDistanceFromGrabbed() > MaxGrabDistance)
{
Release();
}
}
In Grabber.cpp - This is the definition of the function that gets the distance between the grabbed component and the LineTraceEnd:
float UGrabber::GetDistanceFromGrabbed()
{
float Distance = 0.0f;
if (!GrabbedActor) {return Distance;} /// ENSURE THERE IS A PRESSURE PLATE
FVector GrabbedActorLocation = GrabbedActor->GetActorLocation(); // Determine the location of the grabbed actor
FVector Between = GrabbedActorLocation - LineTraceEnd; // Determine the vector difference between the GrabbedActorLocation and the LineTraceEnd
//This function needs to return a Direction, but we aren't actually using it.
FVector Direction;
Between.ToDirectionAndLength(Direction, Distance);
return Distance;
}
So basically if the distance between GrabbedActorLocation and LineTraceEnd is greater than the MaxGrabDistance then it will release the object.