Unity-Technologies / EntityComponentSystemSamples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Closest Target Scene with KD Tree Algorithm not always finds the nearest target

houshuo opened this issue · comments

Apprently, in the screenshot below, the green boxes find wrong nearest target. With None and Simple algorithms, such error won't happen.
image

Hello,

I've noticed a potential issue in the TargetingSystem.cs file on line 141 (TargetingSystem.cs). The GetEntriesInRangeWithHeap method is called with unfilteredChunkIndex as the first parameter. This parameter is intended to be an entity index within kdQuery, to ensure the algorithm does not find the entity that is performing the search. However, in this sample, the seeker entities are not included in kdQuery, so there is no need to skip any entity. Passing unfilteredChunkIndex seems arbitrary here, and we could instead pass -1 as a quick fix for the sample:

Tree.GetEntriesInRangeWithHeap(-1, transforms[i].Position, float.MaxValue, ref Scratch.Neighbours);
If the intention is to include all entities in range and if you need the seeker entities' query index within IJobChunk, it would be more appropriate to first create an index mapping to the job using kdQuery.CalculateBaseEntityIndexArrayAsync.