Trouble Moving Cube with Physics Handle

Hello,

I’m having trouble getting the cube to move when using the PhysicsHandle. The object has a PhysicsActor Collision Preset selected. The default pawn has both the Grabber and the Physics Handle components. Here is my code…

// Called when the game starts
void UGrabber::BeginPlay()
{
	Super::BeginPlay();

	FindPhysicsHandle();
	SetupInputComponent();
}

void UGrabber::FindPhysicsHandle()
{
	// Checking for Physics Handle Component
	PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();

	if (!PhysicsHandle)
	{
		UE_LOG(LogTemp, Error, TEXT("Physics Handle Component is required for use by the Grabber Component. Missing on: %s"), *GetOwner()->GetName());
	}
}

void UGrabber::SetupInputComponent()
{
	InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
	if (!InputComponent)
	{
		return;
	}
	
	InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
	InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
}

void UGrabber::Grab()
{
	FHitResult Hit;
	if (!GetFirstPhysicsBodyInReach(OUT Hit)) return;

	PhysicsHandle->GrabComponentAtLocation(Hit.GetComponent(), NAME_None, Hit.Location);
}

void UGrabber::Release()
{
	PhysicsHandle->ReleaseComponent();
}


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

	if (!PhysicsHandle->GrabbedComponent) return;
	
	// Get players viewpoint
	FVector PlayerLocation;
	FRotator PlayerRotator;
    	
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerLocation, OUT PlayerRotator);
    
	// Ray-cast out to a certain distance (Reach)
	const FVector LineTraceEnd = PlayerLocation + PlayerRotator.Vector() * Reach;
    
	PhysicsHandle->SetTargetLocation(LineTraceEnd);
}

bool UGrabber::GetFirstPhysicsBodyInReach(FHitResult& HitResult) const
{
	// Get players viewpoint
	FVector PlayerLocation;
	FRotator PlayerRotator;
	
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerLocation, OUT PlayerRotator);

	// Ray-cast out to a certain distance (Reach)
	const FVector LineTraceEnd = PlayerLocation + PlayerRotator.Vector() * Reach;
	const FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());
	
	return GetWorld()->LineTraceSingleByObjectType(OUT HitResult, PlayerLocation, LineTraceEnd, FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), TraceParams);
}

Let me know if there is any more information that you need.

Thank you!

P.S. I’m using UE 4.26.2 on Windows 10.

How far is your Reach? Are you sure you’re not just too far away?

The Grabber Reach is set to 100.0. I’ve set it to 300.0 and it still doesn’t do anything. I had logging set up in the code and I could see that the cube was being grabbed, but it wouldn’t actually move. I can put the logging back in and update the code snippet if you’d like. I’ll also include the output of the logs.

I figured out what the issue was. It looks like I didn’t have the ‘Simulate Physics’ option enabled on the cube. :man_facepalming: Once I updated it, everything seems to be working now. Sorry about that, thanks for your help!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms