Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question [SOLVED] DOTS How to get closest entity position with Overlap sphere

Discussion in 'Entity Component System' started by emirkivrak-dev, Nov 11, 2022.

  1. emirkivrak-dev

    emirkivrak-dev

    Joined:
    Jan 14, 2020
    Posts:
    10
    Hello, we are developing a game with dots. I want to take the position of closest enemy entity around my character with using overlap sphere, in dots physics Can somebody help?
     

    Attached Files:

  2. scottjdaley

    scottjdaley

    Joined:
    Aug 1, 2013
    Posts:
    130
    Yup, you can do sphere overlaps using the physics package:

    Code (CSharp):
    1. float3 position;
    2. float radius;
    3. var hits = new NativeList<DistanceHit>(100, Allocator.Temp);
    4. CollisionFilter filter;
    5. GetSingleton<PhysicsWorldSingleton>().OverlapSphere(position, radius, ref hits, filter);
     
    emirkivrak-dev likes this.
  3. scottjdaley

    scottjdaley

    Joined:
    Aug 1, 2013
    Posts:
    130
    And if you only want the closest, you can call OverlapSphereCustom instead and pass it a ClosestHitCollector.
     
  4. emirkivrak-dev

    emirkivrak-dev

    Joined:
    Jan 14, 2020
    Posts:
    10
    Much thanks for your reply @scottjdaley.
    Btw, i'v solved this problem using CalculateDistance collision query defined in PhysicsWorld.cs.

    further read for those dealing with this kind of work : Spatial Queries And Distance
     
    Antypodish likes this.