I am not sure if someone will reply or not as i have tried posting this on many forum and none of them helped. Whenever i try to use physics handle in my Grabber.cpp my unreal crashes. I don’t know why. I have tried adding ensure() but even then it doesn’t works. I am following Unreal C++ Beginner Course by Ben and Sam and i’ve come here with one last hope. Any help is hugely appreciated. I am posting a part of my code where the unreal crashes below:
if (ActorHit)
{
if(ensure(PhysicsHandle!=nullptr))
{
// Attach a physics handle
PhysicsHandle->GrabComponentAtLocationWithRotation(
ComponentToGrab,
NAME_None,
ActorHit->GetOwner()->GetActorLocation(),
ActorHit->GetOwner()->GetActorRotation() // Allow rotation
);
}
}
in the above code, as son as unreal reaches
PhysicsHandle->GrabComponentAtLocationWithRotation
it crashes, If i comment out that section, everything works fine. I am also attaching my header and .cpp file just in case you need a look.
Grabber.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “Components/ActorComponent.h”
#include"Engine/Public/DrawDebugHelpers.h"
#include"Runtime/Engine/Classes/PhysicsEngine/PhysicsHandleComponent.h"
#include"Runtime/Engine/Classes/Engine/EngineTypes.h"
#include"Engine/Classes/Components/InputComponent.h"
#include"Runtime/Engine/Classes/GameFramework/Actor.h"
#include “Grabber.generated.h”
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_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:
// How far ahead of the player can we reach in cm
float Reach = 100.f;
UPhysicsHandleComponent* PhysicsHandle = nullptr;
UInputComponent* Inputcomponent = nullptr;
//Ray-cast and grab what's in reach
void Grab();
// Called when grab is released
void Release();
// Find (assumed) attached physics handle
void FindPhysicsHandleComponent();
// Setup (assumed) attached input component
void SetupInputComponent();
// Return hit for first physics body in reach
const FHitResult GetFirstPhysicsBodyInReach();
};
Grabeer.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include “Grabber.h”
#include"Engine/World.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();
FindPhysicsHandleComponent();
SetupInputComponent();
}
/// Look for attched Physics Handle
void UGrabber::FindPhysicsHandleComponent()
{
PhysicsHandle = GetOwner()->FindComponentByClass();
if (PhysicsHandle)
{
// Physics Handle is found
UE_LOG(LogTemp, Warning, TEXT("%s physics handle component available"), *GetOwner()->GetName());
}
else
{
UE_LOG(LogTemp, Error, TEXT("%s missing physics handle component"), *GetOwner()->GetName());
}
}
/// Look for input component (Only appears at runtime)
void UGrabber::SetupInputComponent()
{
Inputcomponent = GetOwner()->FindComponentByClass();
if (Inputcomponent)
{
UE_LOG(LogTemp, Warning, TEXT("Input Component available"))
///Bind the input action
Inputcomponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
Inputcomponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Error!! %S missing input Component"), *GetOwner()->GetName())
}
}
void UGrabber::Grab()
{
UE_LOG(LogTemp, Warning, TEXT(“Grab Pressed”))
/// LINE TRACE and see if we reach any actor with physics body collision channel set
auto HitResult = GetFirstPhysicsBodyInReach();
auto ComponentToGrab = HitResult.GetComponent();
auto ActorHit = HitResult.GetActor();
/// If we hit something then attcah a physics handle
if (ActorHit)
{
if(ensure(PhysicsHandle!=nullptr))
{
// Attach a physics handle
PhysicsHandle->GrabComponentAtLocationWithRotation(
ComponentToGrab,
NAME_None,
ActorHit->GetOwner()->GetActorLocation(),
ActorHit->GetOwner()->GetActorRotation() // Allow rotation
);
}
}
}
void UGrabber::Release()
{
UE_LOG(LogTemp, Warning, TEXT(“Grab Released”))
PhysicsHandle->ReleaseComponent();
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
/// Get player viewpoint this tick
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);
// Log out to Test
/*
UE_LOG(LogTemp, Warning, TEXT(“Location : %s , Rotation : %s”) , *PlayerViewPointLocation.ToString() , *PlayerViewPointRotation.ToString());
*/
FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
// If the physicshnadle is attached
if (PhysicsHandle->GrabbedComponent)
{
// move the object we're holding
PhysicsHandle->SetTargetLocation(LineTraceEnd);
}
}
const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
{
/// Get player viewpoint this tick
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);
// Log out to Test
/*
UE_LOG(LogTemp, Warning, TEXT(“Location : %s , Rotation : %s”) , *PlayerViewPointLocation.ToString() , *PlayerViewPointRotation.ToString());
*/
FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;
/// Setup query params
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
/// Line-trace (AKA ray-cast) out to reach distance
FHitResult Hit;
GetWorld()->LineTraceSingleByObjectType(
OUT Hit,
PlayerViewPointLocation,
LineTraceEnd,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParameters
);
// See what we hit
AActor* ActorHit = Hit.GetActor();
if (ActorHit)
{
UE_LOG(LogTemp, Warning, TEXT("Line Trace Hit : %s "), *(ActorHit->GetName()))
}
return Hit;
}
Thank you in advance guys,i have been trying to find out the cause of error since past 3 days and finally I have come here with very high hope.



