No Ideal what Happened here it was working fine and then I made those modifications from this video and suddenly I can no longer pick up the object even though it’s registering that it’s being picked up.
This is firing under UGrabber::GrabObject() but the object doesn’t seem to be attaching the the physics object for some reason.
// Called when the game starts
void UGrabber::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
// Set The PhysicsHandle To A Location Offset In Front Of The Owning Actor
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle != nullptr)
{
return;
}
if (PhysicsHandle->GetGrabbedComponent() != nullptr)
{
const FVector TargetLocation = GetOwner()->GetActorLocation() + GetOwner()->GetActorForwardVector() * HoldDistance;
PhysicsHandle->SetTargetLocationAndRotation(TargetLocation, GetOwner()->GetActorRotation());
}
}
void UGrabber::GrabObject()
{
// Find The Physics Handle And Return If It Isn't Found.
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle == nullptr)
{
return;
}
// Set The Start And End Location
const FVector Start = GetComponentLocation();
const FVector End = Start + GetForwardVector() * MaxGrabDistance;
// Draw Debug Visuals
DrawDebugLine(GetWorld(), Start, End, FColor::Red);
DrawDebugSphere(GetWorld(), End, 10, 10, FColor::Blue, false, 5);
// Create Collision Shape And Hit Result For Trace
FCollisionShape Sphere = FCollisionShape::MakeSphere(GrabRadius);
FHitResult HitResult;
// Trace For the object
bool HasHit = GetWorld()->SweepSingleByChannel(
HitResult,
Start,
End,
FQuat::Identity,
ECC_GameTraceChannel1,
Sphere);
if (HasHit)
{
UPrimitiveComponent* HitComponent = HitResult.GetComponent();
HitComponent->WakeAllRigidBodies();
// If The Component Was Hit Attach It To The Physics Handle
PhysicsHandle->GrabComponentAtLocationWithRotation(
HitComponent,
NAME_None,
HitResult.ImpactPoint,
GetOwner()->GetActorRotation()
);
UE_LOG(LogTemp, Display, TEXT("%s Was Picked Up!"), *HitComponent->GetName());
}
}
void UGrabber::ReleaseObject()
{
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle == nullptr)
{
return;
}
if (PhysicsHandle->GetGrabbedComponent() != nullptr)
{
PhysicsHandle->GetGrabbedComponent()->WakeAllRigidBodies();
PhysicsHandle->ReleaseComponent();
}
UE_LOG(LogTemp, Display, TEXT("Grabber Released!"));
}
UPhysicsHandleComponent* UGrabber::GetPhysicsHandle() const
{
UPhysicsHandleComponent* PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("Grabber Requires A UPhysicsHandleComponent."));
}
return PhysicsHandle;
}