Using the Physics Handle to Grab Objects in Unreal

FHitResult HitResult = GetFirstPhysicsBodyInReach();
void GrabComponentAtLocation(
class UPrimitiveComponent * Component,
FName InBoneName,
FVector GrabLocation
);
UPrimitiveComponent* ComponentToGrab = HitResult.GetComponent();
if (PhysicsHandle->GrabbedComponent)
{
PhysicsHandle->SetTargetLocation(LineTraceEnd);
}
void UGrabber::TickComponent(....)
{
Super::TickComponent(....);
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint
(
PlayerViewPointLocation,
PlayerViewPointRotation
);
FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach; if (PhysicsHandle->GrabbedComponent)
{
PhysicsHandle->SetTargetLocation(LineTraceEnd);
}
}
void UGrabber::ReleaseFunction()
{
PhysicsHandle->ReleaseComponent();
}
void UGrabber::FindPhysicsHandle()
{
// Checking for Physics Handle Component
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("No Physics Handle component found on %s"), *GetOwner()->GetName();
}
}
private:
// Return LineTraceEnd
FVector GetPlayersReach() const;
// Get Players Position in the World
FVector GetPlayersWorldPosition() const;
FVector UGrabber::GetPlayersReach() const
{
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint
(
PlayerViewPointLocation,
PlayerViewPointRotation
);
return PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
}
void UGrabber::GrabFunction()
{
FHitResult HitResult = GetFirstPhysicsBodyInReach();
UPrimitiveComponent* ComponentToGrab = HitResult.GetComponent();
AActor* ActorHit = HitResult.GetActor();
// If we hit something then attach the physics handle
if (ActorHit)
{
if (!PhysicsHandle) {return;}
{
PhysicsHandle->GrabComponentAtLocation
(
ComponentToGrab,
NAME_None,
GetPlayersReach()
);
}
}
}
void UGrabber::TickComponent(....)
{
Super::TickComponent(....);
if (PhysicsHandle->GrabbedComponent)
{
// Move the object that we are currently holding
PhysicsHnadle->SetTargetLocation(GetPlayersReach());
}
}
FVector UGrabber::GetPlayersWorldPosition() const
{
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint
(
PlayerViewPointLocation,
PlayerViewPointRotation
);
return PlayerViewPointLocation;
}
FHitResult UGrabber::GetFirstPhysicsBodyInReach() const
{
FHitResult Hit;
// Ray Cast out to a certain distance (Reach)
FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());
GetWorld()->LineTraceSingleByObjectType
(
Hit,
GetPlayersWorldPosition(),
GetPlayersReach(), FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParams
);
return Hit;
}

--

--

--

Full stack web developer with experience in Ruby on Rails, JavaScript, React and Redux and learning more!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Implement Scratch Card(Reward Card) in Android Studio without using library.

5 Benefits of Load Balancer in Digital Ocean: Very Cheap

Get your projects done with chops!

QA Strategy in Agile Model

Introducing CNAME flattening: Multi CDN on steroids

So you want to be a great developer? 5 inspiring stories

Running multiple MySQL instances on Ubuntu

How can custom ERP software help your business grow?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Danny Padron

Danny Padron

Full stack web developer with experience in Ruby on Rails, JavaScript, React and Redux and learning more!

More from Medium

Piano Concerto in C minor Rachmaninoff

npx and npm

Down with Big Brother