Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    160
    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:
    160
    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.