Hi everybody, I’ve been strugling to find how the syntax to this works. I want to check if a line trace I made hits a component with the tag “human”. What’s the best way for me to achieve that? Here’s how the code looks like so far.
AUGhost::AUGhost()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
for (int32 I = 0; I < 6; ++I)
{
// Line Trace
float CurrentAngle = I * (360/20.f);
FVector StartPoint = GetActorLocation();
FVector Forward = GetActorForwardVector();
FRotator Rotation = FRotator(0.f, CurrentAngle - 45.f, 0.f);
FVector EndPoint = StartPoint + Rotation.RotateVector(Forward) * 80.0f;
FHitResult Hit;
GetWorld()->LineTraceSingleByChannel(Hit, StartPoint, EndPoint, ECC_Visibility);
DrawDebugLine(GetWorld(), StartPoint, EndPoint, FColor(52,220,239), true);
// Check if component with Tag "Human" was hit
if (FHitResult Hit->ActorHasTag(TEXT("Human")))
{
}
}
Is “Human” an actor or a component? (I just wanted to make sure.)
There are two common *HasTag methods: ActorHasTag and ComponentHasTag.
You can access the first Actor or Component that was hit by using GetActor() or GetComponent() methods on FHitResult variable.
Every Actor and Component has a TArray<FName> member variable (Actor → Tags, Component → ComponentTags) that contains all the given tags.
Let’s suppose your target is an Actor. But first put the for loop in/after BeginPlay().
FHitResult HitResult;
if (GetWorld()->LineTraceSingleByChannel(HitResult, /*...*/, /*...*/, ECC_Visibility))
{
AActor* Target = HitResult.GetActor();
if (Target->ActorHasTag(FName(TEXT("Human"))))
{
// do something
}
}
One more thing: be sure Trace Response is correct of targeted Actor!