I’m currently using 4.19.2. Basically, here is my code for raycasting:
— the raycast method —
AActor *ASandboxGameCharacter::raycast()
{
FHitResult hitResult;
FCollisionQueryParams collisionParams;
FVector start;
FRotator direction;
GetActorEyesViewPoint(start, direction);
AActor *actorHit = nullptr;
FVector end = start + (direction.Vector() * 500.f);
if (GetWorld()->LineTraceSingleByChannel(hitResult, start, end, ECC_Visibility, collisionParams))
{
// 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)
{
// For debugging purposes (Logs help text to the top left of the screen, to provide visual identification of what actor has been hit)
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, FString::Printf(TEXT("The item being hit is a: %s"), *hitResult.GetComponent()->GetName()));
actorHit = hitResult.GetActor();
}
}
return actorHit;
}
— The object being hit’s method —
void AInteractableObject::interactImplementation()
{
if (checkingForDirectView)
{
ASandboxGameCharacter *interactingCharacter = Cast<ASandboxGameCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
if (interactingCharacter->returnRaycastActor() != nullptr)
{
if (Cast<AActor>(this) == interactingCharacter->returnRaycastActor())
{
Destroy();
}
}
}
}
The first set of code is from my character class, where I’ve defined the raycast method. This method is then used in another class, where it is made to be called each tick. Now, when I change the end FVector’s float value from whatever I set it to the first time I compiled the code, the behaviour changes ENTIRELY. So, the normal behaviour would be that the linetraces are ALWAYS visible and begin when I enter the box collider of the “interactableObject” (defined elsewhere, but it’s simply a boolean value being set, which is used in the excerpt here). Now, this happens infinitely when I dont change the float value, as well as the printing of the hit object in the top left of the screen while I’m within the box. However, AS SOON as I change the float value to anything else, like 500.f, suddenly the behaviour changes entirely. In my first project, what it would do was only cast in a 180 degree radius. In my coming projects, it essentially only renders the line when I hit the interactable object, which makes it disappear. I cant revert this back, either, it literally only shows the log or renders a trace when the object in the box is hit (the interactableObject) and destroys it. This makes zero sense…