I followed the videos, as exact as I could, but for some really odd reason, when I go to pick up the Gargoyle, I can move it left and right, but not up and down. What have I done wrong here?
EDIT: NEVER MIND. I attached the C++ Class to the wrong part of the mesh and it wasn’t attached to the First-Person Camera, it was attached to the Capsule Component.
#include "Grabber.h"
#include "Engine/World.h"
#include "DrawDebugHelpers.h"
// 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();
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
UPhysicsHandleComponent *PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle == nullptr)
{
return;
}
if (PhysicsHandle->GetGrabbedComponent() != nullptr)
{
FVector TargetLocation = GetComponentLocation() + GetForwardVector() * HoldDistance;
PhysicsHandle->SetTargetLocationAndRotation(TargetLocation, GetComponentRotation());
}
}
void UGrabber::Grab()
{
UE_LOG(LogTemp, Display, TEXT("Grabber Pressed"));
UPhysicsHandleComponent *PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle == nullptr)
{
return;
}
FHitResult HitResult;
bool HasHit = GetGrabbableInReach(HitResult);
if (HasHit)
{
UPrimitiveComponent* HitComponent = HitResult.GetComponent();
HitComponent->WakeAllRigidBodies();
PhysicsHandle->GrabComponentAtLocationWithRotation(HitComponent, NAME_None, HitResult.ImpactPoint, GetComponentRotation());
}
}
void UGrabber::Release()
{
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if (PhysicsHandle == nullptr)
{
return;
}
if (PhysicsHandle->GetGrabbedComponent() != nullptr)
{
PhysicsHandle->ReleaseComponent();
}
UE_LOG(LogTemp, Display, TEXT("Released Grabber"));
}
UPhysicsHandleComponent* UGrabber::GetPhysicsHandle() const
{
UPhysicsHandleComponent* Result = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (Result == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("Grabber requires a UPhysicsHandleComponent!"));
}
return Result;
}
bool UGrabber::GetGrabbableInReach(FHitResult& OutHitResult) const
{
FVector Start = GetComponentLocation();
FVector End = Start + GetForwardVector() * MaxGrabDistance;
FCollisionShape Sphere = FCollisionShape::MakeSphere(GrabberRadius);
return GetWorld()->SweepSingleByChannel(OutHitResult, Start, End, FQuat::Identity, ECC_GameTraceChannel2, Sphere);
}