Here is the code in .h
#include "Components/ActorComponent.h"
#include "Grabber.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT_API UGrabber : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UGrabber();
// Called when the game starts
virtual void BeginPlay() override;
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
private:
// How far ahead of the player can we reach in cm
float Reach = 150.f;
UPhysicsHandleComponent* PhysicsHandle = nullptr;
UInputComponent* InputComponent = nullptr;
// Ray-cast and grab what's in reach
void Grab();
void Release();
// Find attached physics handle component
void FindPhysicsHandleComponent();
void SetupInputComponent();
// Return hit for first physics body in reach
const FHitResult GetFirstPhysicsBodyInReach();
// Returns current start of reach line
FVector GetReachLineStart();
// Returns current end of reach line
FVector GetReachLineEnd();
};
Here is the code in .cpp
#include "MyProject.h"
#include "Grabber.h"
#define OUT
// 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.
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UGrabber::BeginPlay()
{
Super::BeginPlay();
FindPhysicsHandleComponent();
SetupInputComponent();
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// if the physics handle is attached
if (PhysicsHandle->GrabbedComponent)
{
PhysicsHandle->SetTargetLocation(GetReachLineStart());
}
}
/// Look for attached Physics Handle
void UGrabber::FindPhysicsHandleComponent()
{
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("%s missing physics handle component"), *GetOwner()->GetName())
}
};
void UGrabber::SetupInputComponent()
{
/// Look for attached Input Component (Only appears at runtime)
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (InputComponent)
{
UE_LOG(LogTemp, Warning, TEXT("Input Component found"))
InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
}
else
{
UE_LOG(LogTemp, Error, TEXT("%s missing Input Component"), *GetOwner()->GetName())
}
}
void UGrabber::Grab()
{
/// LINE TRACE and see if we reach any actors with physics collision channel set
auto HitResult = GetFirstPhysicsBodyInReach();
auto ComponentToGrab = HitResult.GetComponent();
auto ActorHit = HitResult.GetActor();
/// If we hit something then grab a physics handle
if (ActorHit)
{
PhysicsHandle->GrabComponent(
ComponentToGrab,
NAME_None, // no bones needed
ComponentToGrab->GetOwner()->GetActorLocation(),
true // allow rotation
);
}
}
void UGrabber::Release()
{
//TODO release physics handle
PhysicsHandle->ReleaseComponent();
}
const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
{
/// Setup query parameters
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
/// Line-trace (AKA Ray-cast) out to reach distance
FHitResult HitResult;
GetWorld()->LineTraceSingleByObjectType(
OUT HitResult,
GetReachLineStart(),
GetReachLineEnd(),
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParameters
);
/// see what we hit
AActor* ActorHit = HitResult.GetActor();
if (ActorHit)
{
UE_LOG(LogTemp, Warning, TEXT("Line trace hit: %s"), *(ActorHit->GetName()))
}
return HitResult;
}
FVector UGrabber::GetReachLineStart()
{
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint
(
OUT PlayerViewPointLocation,
OUT PlayerViewPointRotation
);
return PlayerViewPointLocation;
}
FVector UGrabber::GetReachLineEnd()
{
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint
(
OUT PlayerViewPointLocation,
OUT PlayerViewPointRotation
);
return PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
}