the code works fine , I have outputed logs everywhere to see if the code is not running in some place but nothing.
when i press play i can see the object grabbed output but the object doesnt even move even if i try to move it by pushing it.
Grabber.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Grabber.h"
#define OUT //marking out params
// 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();
// Physics Handle
CheckPhysicsComponent();
// Input Component
CheckInputComponent();
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (!PhysicsHandle) return;
//set location of the grabbed component
if (PhysicsHandle->GrabbedComponent) {
PhysicsHandle->SetTargetLocation(GetLineEndPoint());
//UE_LOG(LogTemp, Warning, TEXT("location set %s!"), *PhysicsHandle->GrabbedComponent->GetName());
}
}
// CHECKER FUNCTIONS -------------
void UGrabber::CheckPhysicsComponent() {
PhysicsHandle = Cast<UPhysicsHandleComponent>(GetOwner()->GetComponentByClass(UPhysicsHandleComponent::StaticClass()));
if (PhysicsHandle == nullptr) {
UE_LOG(LogTemp, Error, TEXT("this object doesnt have a physics handle, please add physics handle !"));
}
}
void UGrabber::CheckInputComponent() {
InputComponent = Cast<UInputComponent>(GetOwner()->GetComponentByClass(UInputComponent::StaticClass()));
if (InputComponent) {
UE_LOG(LogTemp, Warning, TEXT("COMPONENT FOUND!"));
//InputActions
InputComponent->BindAction("GrabButton", IE_Pressed, this, &UGrabber::Grab);
InputComponent->BindAction("ReleaseButton", IE_Released, this, &UGrabber::Release);
}
else {
UE_LOG(LogTemp, Warning, TEXT("this object doesnt have an Input component handle, please add physics handle !"));
}
}
// FUNCTIONS ----------------------
void UGrabber::Grab() {
UE_LOG(LogTemp, Warning, TEXT("Button Pressed !"));
// Get Body in reach
FHitResult HitResult = GetFirstObjectWithPhysicsBody();
// Grabbing Logic
if (HitResult.GetActor()) {
UPrimitiveComponent* ComponentToGrab = HitResult.GetComponent();
PhysicsHandle->GrabComponent(
ComponentToGrab,
NAME_None,
ComponentToGrab->GetOwner()->GetActorLocation(),
true);
UE_LOG(LogTemp, Warning, TEXT("objectGrabbed"));
}
}
void UGrabber::Release() {
UE_LOG(LogTemp, Warning, TEXT("Button Released !"));
PhysicsHandle->ReleaseComponent();
}
FHitResult UGrabber::GetFirstObjectWithPhysicsBody() {
//ray casting
FHitResult Hit;
FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());
GetWorld()->LineTraceSingleByObjectType(
OUT Hit,
GetLineStartPoint(),
GetLineEndPoint(),
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParams
);
return Hit;
}
// UTILITY
FVector UGrabber::GetLineEndPoint()
{
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerLocation,
OUT PlayerRotation);
return PlayerLocation + PlayerRotation.Vector() * Reach;
}
FVector UGrabber::GetLineStartPoint()
{
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerViewPointLocation,
OUT PlayerViewPointRotation
);
return PlayerLocation;
}