// Fill out your copyright notice in the Description page of Project Settings.
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())
{
FVector TargetLocation = GetComponentLocation() + GetForwardVector() * HoldDistance;
PhysicsHandle->SetTargetLocationAndRotation(TargetLocation, GetComponentRotation());
}
}
void UGrabber::Grab()
{
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if(PhysicsHandle == nullptr)
{
return;
}
FHitResult HitResult;
bool HasHit = GetGrabbedInReach(HitResult);
if(HasHit)
{
UPrimitiveComponent* HitComponent = HitResult.GetComponent();
HitComponent->SetSimulatePhysics(true);
HitComponent->WakeAllRigidBodies();
AActor* HitActor = HitResult.GetActor();
HitActor->Tags.Add("Grabbed");
HitActor->DetachFromActor(FDetachmentTransformRules::KeepWorldTransform);
PhysicsHandle->GrabComponentAtLocationWithRotation
(
HitComponent,
NAME_None,
HitResult.ImpactPoint,
HitResult.GetComponent()->GetComponentRotation()
);
}
}
void UGrabber::Release()
{
UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
if(PhysicsHandle == nullptr)
{
return;
}
if(PhysicsHandle->GetGrabbedComponent() != nullptr)
{
AActor* GrabbedActor = PhysicsHandle->GetGrabbedComponent()->GetOwner();
PhysicsHandle->GetGrabbedComponent()->WakeAllRigidBodies();
PhysicsHandle->ReleaseComponent();
GrabbedActor->Tags.Remove("Grabbed");
}
}
UPhysicsHandleComponent* UGrabber::GetPhysicsHandle() const
{
UPhysicsHandleComponent* Result = GetOwner()->FindComponentByClass();
if(Result == nullptr)
{
UE_LOG(LogTemp, Error, TEXT(“Grabber requires a UPhysicsComponent”));
}
return Result;
}
bool UGrabber::GetGrabbedInReach(FHitResult& OutHitResult) const
{
FVector Start = GetComponentLocation();
FVector End = Start + GetForwardVector() * MaxGrabDistance;
DrawDebugLine(GetWorld(), Start, End, FColor::Red);
DrawDebugSphere(GetWorld(), End, 10, 10, FColor::Blue, false, 5);
FCollisionShape Sphere = FCollisionShape::MakeSphere(GrabRadius);
FHitResult HitResult;
bool HasHit = GetWorld()->SweepSingleByChannel(HitResult, Start, End, FQuat::Identity, ECC_GameTraceChannel2, Sphere);
if(HasHit)
{
OutHitResult = HitResult;
}
return HasHit;
}
and when I try to grab item, i start flying