— Relevant code in the Character class —
AActor *ASandboxGameCharacter::raycast()
{
AActor *actorHit = nullptr;
FHitResult *hitResult = new FHitResult();
FCollisionQueryParams *cqp = new FCollisionQueryParams();
FVector start = GetActorLocation();
FVector end = (GetActorForwardVector() * 5000.f) + start;
if (GetWorld()->LineTraceSingleByChannel(*hitResult, start, end, ECC_Visibility, *cqp))
{
// For debugging purposes (Draws a visible line in the direction of the raycast)
DrawDebugLine(GetWorld(), start, end, FColor(255, 0, 0), true);
if (hitResult->GetActor() != NULL)
{
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, FString::Printf(TEXT("Actor: %s"), *hitResult->GetComponent()->GetName()));
actorHit = hitResult->GetActor();
}
}
return actorHit;
}
AActor *ASandboxGameCharacter::returnRaycastActor()
{
AActor *targetActor = nullptr;
if (raycast() != nullptr)
{
targetActor = raycast();
}
return targetActor;
}
*** Please note that the raycast() method is private, hence it acts as a mutator (setter) and returnRaycastActor() acts as an accessor (getter) ***
— Relevant code in Interactable class —
// Defines the behaviour caused by the player when they overlap the props influence cube.
void AInteractableObject::playerInteraction(UPrimitiveComponent *hitComponent, AActor *interactingActor, UPrimitiveComponent *otherComponent, int32 interactingActorBodyIndex, bool isDetected, const FHitResult & detectionResult)
{
toggleGlow(true);
checkingForDirectView = true;
}
// Defines the behaviour caused by the player when they exit the props influence cube.
void AInteractableObject::playerUninteraction(UPrimitiveComponent *hitComponent, AActor *interactingActor, UPrimitiveComponent *otherComponent, int32 interactingActorBodyIndex)
{
toggleGlow(false);
checkingForDirectView = false;
}
// Defines the behaviour caused when the player interacts with the prop.
void AInteractableObject::interactImplementation()
{
if (checkingForDirectView)
{
ASandboxGameCharacter *interactingCharacter = Cast<ASandboxGameCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
if (interactingCharacter->returnRaycastActor() != nullptr)
{
if (interactingCharacter->returnRaycastActor() == UGameplayStatics::GetPlayerCharacter(this, 0))
{
Destroy();
}
}
}
}
void AInteractableObject::BeginPlay()
{
Super::BeginPlay();
}
void AInteractableObject::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
interactImplementation();
}
If possible, can someone suggest a more appropriate way to compare the actor being hit in the raycast and the interactable objects. Not too familiar with the library definitions, methods and functions, so it would be helpful if someone more knowledgeable/someone who’s been through a similar experience and advise me on what to change. Specifically, the third nested if statement in the “interactImplementation()” method does not work at all, as you’d expect due to it being a strange comparison. The desired behavior was to fetch the type of actor that the object hit in the raycast was and compare it to the interactable actor, which we can then use to identify that the player is directly observing the interactable object. This is entirely my fault, I’m too lazy to sift through tonnes of documentation to find what I need, so I resort to asking others (since I’m relatively new to UE4, and C++ (But I come from Java and Objective-C)). In addition to this, in the initial Character class, I’m using the component attached to the root to cast the ray from. I didn’t really want to do this, instead I wanted to cast from the camera directly, but that was yielding strange results (The source/origin of the cast was different to the position of the camera, hence the vector differed from the expected vector, but the final location was the same). The last thing I think I should mention is that the debug message only shows “StaticMeshComponent0” as the name of the collided mesh, regardless of where you look (Which, btw, is very limited since it’s generated from the character model, thus lacks the ability to freely rotate in the x-axis).
Thanks in advance for any help offered! (Senpai, save me from this hell)