Download

my code is acting weird

alright so what i want do is raycast into the world and try to reach actors that have a specific tag and if the tag matches I wanna display an image on the screen. this kinda works except that its not consistent all the time sometimes the image shows up sometimes it doesn’t whats wrong here that I don’t get?

also I’m calling the function in the tick.

void AMain:DetectItems()
{
FVector Location = FirstPersonCameraComponent->GetComponentLocation();
FVector End = (FirstPersonCameraComponent->GetForwardVector() * CastingRange) + Location;

FHitResult Hit;
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);

GetWorld()->LineTraceSingleByChannel(Hit, Location, End, ECollisionChannel::ECC_Visibility, Params);

DrawDebugLine(GetWorld(), Location, End, FColor::Red, false, .3f);

if (Hit.GetActor())
{

bool HitResult = Hit.GetActor()->ActorHasTag(TEXT(“InteractableItems”));

if (HitResult && !InteractCrosshair->IsInViewport())
{
InteractCrosshair->AddToViewport();
}
else if (InteractCrosshair->IsInViewport())
{
InteractCrosshair->RemoveFromViewport();
}
}
}

guys i need your help please I’m stuck in my development

have a look at your if statements . you are going to flip flop every frame for a valid target

i guess it could look something like this … but you might want to look at a case where you do not get a valid actor from your linetrace
if (Hit.GetActor())
{

bool HitResult = Hit.GetActor()->ActorHasTag(TEXT(“InteractableItems”));

if (HitResult )
{
if(!InteractCrosshair->IsInViewport())
InteractCrosshair->AddToViewport();
}
else if (InteractCrosshair->IsInViewport())
{
InteractCrosshair->RemoveFromViewport();
}
}

yep that was the problem thanks ^^