Hi,
My code is quite different from the lecture (maybe because I have a BP background)
// Copyrights by Raed Abbas
#include "Grabber.h"
#include "DrawDebugHelpers.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.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.
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UGrabber::BeginPlay()
{
Super::BeginPlay();
FindPhysicsHandle();
CallInput();
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (PhysicsHandle->GrabbedComponent)
{
PhysicsHandle->SetTargetLocation(GetPlayerReachLocation());
}
}
//Finding the physics component
void UGrabber::FindPhysicsHandle()
{
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("No Physics Handle found!"));
}
}
void UGrabber::CallInput()
{
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (InputComponent)
{
UE_LOG(LogTemp, Warning, TEXT(" Input component found!"));
InputComponent->BindAction("Interact", IE_Pressed, this, &UGrabber::Grap);
InputComponent->BindAction("Interact", IE_Released, this, &UGrabber::Release);
}
}
FVector UGrabber::GetPlayerReachLocation()
{
//Get player View point
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint
(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);
return PlayerViewPointLocation + PlayerViewPointRotation.Vector() * PlayerMaxReach;
}
FHitResult UGrabber::LineTracing(FVector PlayerLocation, FRotator PlayerRotation, float PlayerReach) const
{
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint
(OUT PlayerLocation, OUT PlayerRotation);
FVector TraceEnd = PlayerLocation + PlayerRotation.Vector() * PlayerReach;
FHitResult Hit;
FCollisionQueryParams TraceParam(FName(TEXT("")), false, GetOwner());
GetWorld()->LineTraceSingleByObjectType
(OUT Hit,
PlayerLocation,
TraceEnd,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParam);
return Hit;
}
void UGrabber::Grap()
{
UE_LOG(LogTemp, Warning, TEXT("Grabbing!!!!!"));
FHitResult HitResultActor = LineTracing(PlayerViewPointLocation,
PlayerViewPointRotation,
PlayerMaxReach);
AActor* PickableActor = HitResultActor.GetActor();
if (PickableActor)
{
UPrimitiveComponent* HitComponent = HitResultActor.GetComponent();
FVector PickableActorLocation = PickableActor->GetActorLocation();
PhysicsHandle->GrabComponentAtLocation
(
HitComponent,
NAME_None,
GetPlayerReachLocation()
);
UE_LOG(LogTemp, Warning, TEXT("Grabbing %s"), *PickableActor->GetName());
}
}
void UGrabber::Release()
{
PhysicsHandle->ReleaseComponent();
UE_LOG(LogTemp, Warning, TEXT("Releasing grab!"));
}