Download

[Solved ]Line Trace on Asynchronous thread not working properly

Hi,
I have created an FNonAbandonableTask and I initialized an asynchronous thread. And inside that thread, I’m doing a Line Trace but it’s not working properly.

AtmoicCollision it’s just a struct with some variables shared with the main thread.


void MultithreadTask::DoWork()
{
    FPlatformProcess::Sleep(1);

    while (AtomicCollision->bThreadAlive)
    {
        FPlatformProcess::Sleep(AtomicCollision->deltaTime);

        if (AtomicCollision->bAttack.load(std::memory_order_acquire))
        {
            for (int i = 0; i < ShapeCollisions.Num(); i++)
            {
                if (CheckCollisions(*ShapeCollisions*))
                {
                    DrawDebugSphere(AtomicCollision->world, Hit.Location, 10, 10, FColor::Yellow, false, 1, 0);
                    if (Cast<AAISwordsman>(Hit.Actor) && !bAlreadyHit_AI)
                    {
                        UE_LOG(LogTemp, Warning, TEXT("HIT"));
                        AtomicCollision->gotHit(Hit);
                        bAlreadyHit_AI = true;
                    }
                }
            }
        }
        else
        {
            bAlreadyHit_AI = false;
        }
    }

    FPlatformProcess::Sleep(0.1f);
}

Here I do the LineTrace in the thread by calling this function


bool MultithreadTask::CheckCollisions(const UShapeCollisionComponent& _ShapeCollision)
{
      DrawDebugLine(AtomicCollision->world, myVector, (_ShapeCollision.GetUpVector() * _ShapeCollision.Line.Length) + myVector, _ShapeCollision.Line.Color, false,  _ShapeCollision.Line.LifeTime, 0, _ShapeCollision.Line.Tickness);

       return AtomicCollision->world->LineTraceSingleByChannel(Hit, _ShapeCollision.GetComponentLocation(), (_ShapeCollision.GetUpVector() * _ShapeCollision.Line.Length) + _ShapeCollision.GetComponentLocation(), ECC_GameTraceChannel1, QueryParams);
}

I have checked everything, I checked the values of “GetComponentLocation()” in the thread and from blueprints and they all match. All the values are correct and when I DrawDebugLine the line it’s perfect as it should be. The Line Trace Start Location “GetComponentLocation()” it’s correct as it should be and also the Line Trace End Location.
But the LineTraceSingleByChannel doesn’t work, it always says that it hits the Player, when it shouldn’t.
Here for example in the first image, I draw a sphere in the Hit.Location and it shouldn’t hit, Hit.Actor is the Player, but it’s too far away to hit the capsule component. Also in the second image where it should hit the AI it returns the Hit.Location at the start of the Line Trace and returns the Player.

Only if I put the Start Location of the Line Trace directly inside the AI (so when the yellow sphere is inside the AI), the Hit.Actor will be the AI.
At this point I surrender, I think it’s a problem that I’m doing the LineTrace on an asynchronous thread. Any help it’s appreciated, I have tried to debug everything that I could I have no idea what the problem could be.

I think I replied to your duplicate thread just as you deleted it…

My thought was to check your QueryParams variable to see if it’s set to ignore the player: [TABLE=“cellspacing: 0”]

FCollisionQueryParams

		(

FName InTraceTag,
bool bInTraceComplex,
const AActor* InIgnoreActor
)


FCollisionQueryParams MyParams(FName(TEXT("")), true, this);

Sorry, I had a double post and I deleted the old one, didn’t see your reply sorry.
Ok, I put to ignore the owner and it seems to be working perfectly! I was so sure that it wouldn’t work that I didn’t even try, guess I was wrong.
What I was doing was to move the line trace away from the Player (because the Locations it’s a component attached to the player) and I thought it should work.
But it doesn’t make any sense the fact that it hits the Player when the line trace it’s not even touching the player.
Whatever it works, thank you!!!

EDIT: Do I need to put something like post solved?

You’re welcome. I’m glad it worked. I honestly can’t tell you the technical reasons why it hits the player despite the vector from Start to End not passing through them, but I’ve had this exact problem before.

I don’t think there’s a way to flag solved… All good.