//Look for PhysicsHandle Component
void UGrabber::FindPhysicsHandleComponent()
{
  PhysicsHandle = GetOwner()->FindComponentByClass();
//System report
if (PhysicsHandle == nullptr) {
   UE_LOG(LogTemp, Error,
   TEXT("Couldn't find PhysicsHandleComponent in %s"), *GetOwner()->GetName());
  }
}

//Look for Input Component
void UGrabber::SetupInputComponent() {
//Get Input Component
  InputComponent = GetOwner()->FindComponentByClass();
if (InputComponent) {
   UE_LOG(LogTemp, Warning,
   TEXT("Found InputComponent"));
   ///Bind the input axis
   InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
   InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
  }
else {
   UE_LOG(LogTemp, Error,
   TEXT("Couldn't find InputComponent in %s"), *GetOwner()->GetName());
  }
}

//Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

///If the Actor is holding an Object
if (PhysicsHandle->GrabbedComponent) {
   PhysicsHandle->SetTargetLocation(LineTraceEnd);
  }

}

//Grab object
void UGrabber::Grab() {
//System report
UE_LOG(LogTemp, Warning, TEXT("Grabbing"));

///Raycast
auto HitResult = GetFirstPhysicsBodyInReach();
auto ComponentToGrab = HitResult.GetComponent();
auto ActorHit = HitResult.GetActor();

///If we hit something then attach a physics handle
if (ActorHit) {
   PhysicsHandle->GrabComponent(ComponentToGrab, NAME_None,
   ComponentToGrab->GetOwner()->GetActorLocation(), true);
  }


}

//Release object
void UGrabber::Release() {
//System report
UE_LOG(LogTemp, Warning,
  TEXT("Releasing Grab"));

///Release physics
  PhysicsHandle->ReleaseComponent();
}