I am trying to make a game where i can pick up and move things.
I made a new trace channel and a scene component that i added to the camera component of my BP_FirstPersonCharacter.
Then I made a UE_Log in to display a message in the output log of what did the cahnnel detect.
Using the that scene component, the trace channel and the physics handle that i added to my BP_First person character it was in theory supposed detect the object that my camera component is seeing, print its name it the output log and then i woud left click and pick it up.
However when i look in the direction of the object and left-click it only detects my BP_FirstPersonCharacter.
In some cases if i came very close to an actor like the wall in my scene and than left-click it woud actually print out its name in the output log.
Although If I make the object simulate physics and make it movable it dosent work.
things i have tried: I checked the capsule component's collision presets to see if its blocking the channel, it wasent, it was set on ignore. [as you can see here][1] (GTC the name of the cannnel)
code of the scene component:
#include "Engine/World.h"
#include "DrawDebugHelpers.h"
#include "PhysicsEngine/PhysicsHandleComponent.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();
UPhysicsHandleComponent* PHandle = GetOwner() -> FindComponentByClass<UPhysicsHandleComponent>();
if (PHandle != nullptr)
{
UE_LOG(LogTemp, Display, TEXT("Got Physics Handle: %s"), *PHandle->GetName());
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No Physics Handle Found!"));
}
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
UPhysicsHandleComponent* PHandle = GetOwner() -> FindComponentByClass<UPhysicsHandleComponent>();
if(PHandle == nullptr)
{
return;
}
FVector TargetLocation = GetComponentLocation() + GetForwardVector() * HoldDistance;
PHandle -> SetTargetLocationAndRotation(TargetLocation, GetComponentRotation());
}
void UGrabber::Release()
{
UE_LOG(LogTemp, Display, TEXT("Released grabber"));
}
void UGrabber::Grab(){
UPhysicsHandleComponent* PHandle = GetOwner() -> FindComponentByClass<UPhysicsHandleComponent>();
if(PHandle == nullptr)
{
return;
}
FVector Start = GetComponentLocation();
FVector End = Start + GetForwardVector() * MaxGrabDistance;
DrawDebugLine(GetWorld(), Start, End, FColor::Green);
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_GameTraceChannel1,
Sphere
);
if (HasHit)
DrawDebugSphere(GetWorld(), HitResult.Location, 10, 10, FColor::Green, false, 5);
DrawDebugSphere(GetWorld(), HitResult.ImpactPoint, 10, 10, FColor::Red, false, 5);
AActor *HitActor = HitResult.GetActor();
UE_LOG(LogTemp, Display, TEXT("Hit actor: %s"), *HitActor->GetActorNameOrLabel());
PHandle -> GrabComponentAtLocationWithRotation(
HitResult.GetComponent(),
NAME_None,
HitResult.ImpactPoint,
HitResult.GetComponent() -> GetComponentRotation()
);
}
``
[1]: https://i.stack.imgur.com/6mcsS.png