Hey folks,
Please promt where am I failing to get the Udemy Building Escape tutorial to work?
My Grabber Component crashes when reaching this line:
PhysicsHandle->SetTargetLocation(DebugLineEnd);
I have attached the .cpp and .h of the grabber component;
Here’s the .h file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/Actor.h"
#include "Public/DrawDebugHelpers.h"
#include "Classes/PhysicsEngine/PhysicsHandleComponent.h"
#include "Classes/Components/InputComponent.h"
#include "Classes/Components/PrimitiveComponent.h"
#include "Grabber.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TEDDY_API UGrabber : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UGrabber();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
float Reach = 100.f;
UPhysicsHandleComponent* PhysicsHandle = nullptr;
UInputComponent* Input = nullptr;
FString Object;
void Grab();
void Release();
void GetPhysicsHandle();
void SetupInput();
const FHitResult MakeCast();
};
And here’s the .cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#define OUT
#include "Grabber.h"
UGrabber::UGrabber()
{
PrimaryComponentTick.bCanEverTick = true;
bWantsBeginPlay = true;
}
// Called when the game starts
void UGrabber::BeginPlay()
{
Super::BeginPlay();
void GetPhysicsHandle();
void SetupInput();
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
FRotator PlayerRot;
FVector PlayerPos;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerPos, OUT PlayerRot);
FVector DebugLineEnd = PlayerPos + PlayerRot.Vector() * Reach;
if (PhysicsHandle->GrabbedComponent)
{
PhysicsHandle->SetTargetLocation(DebugLineEnd);
}
}
//Move an object while its held
void UGrabber::Grab()
{
auto HitResult = MakeCast();
auto ComponentToGrab = HitResult.GetComponent();
auto ActorHit = HitResult.GetActor();
UE_LOG(LogTemp, Warning, TEXT("GrabPressed"))
if (ActorHit)
{
PhysicsHandle->GrabComponent(
ComponentToGrab,
NAME_None,
ComponentToGrab->GetOwner()->GetActorLocation(),
true
);
}
}
//Put the object down when its held
void UGrabber::Release()
{
PhysicsHandle->ReleaseComponent();
}
void UGrabber::GetPhysicsHandle()
{
///Find physics component
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle)
{
UE_LOG(LogTemp, Error, TEXT("Physics Handle Found"))
}
else{}
}
void UGrabber::SetupInput()
{
Input = GetOwner()->FindComponentByClass<UInputComponent>();
if (Input)
{
Input->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
Input->BindAction("Grab", IE_Released, this, &UGrabber::Release);
}
else
{
UE_LOG(LogTemp, Error, TEXT("No input handle found on: %s"), *Object)
}
}
const FHitResult UGrabber::MakeCast()
{
FRotator PlayerRot;
FVector PlayerPos;
FString Pos;
FString Rot;
///Get the player view point this tick
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerPos, OUT PlayerRot);
FVector DebugLineEnd = PlayerPos + (PlayerRot.Vector() * Reach);
FHitResult Hit;
/// Ray-cast out to reach distance
GetWorld()->LineTraceSingleByObjectType(
OUT Hit,
PlayerPos,
DebugLineEnd,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
FCollisionQueryParams(FName(TEXT("")), false, GetOwner())
);
return Hit;
}