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.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to improve this GroundDetectionSystem?

Discussion in 'Entity Component System' started by Freddx, Aug 22, 2018.

  1. Freddx

    Freddx

    Joined:
    Mar 31, 2017
    Posts:
    5
    I'm new to ECS, I'm trying to do a ground detection system, I don't know a way for use the JobSystem here, I need to access data in the main thread to check for the collision what is not possible from a Job.

    And other question, Why the NativeArray<T> allocates? running this code I have notice that each frame it allocates 0.7kb.

    Code (CSharp):
    1. public class GroundDetectionComponent : ComponentDataWrapper<GroundDetection> { }
    2.  
    3.     [System.Serializable]
    4.     public struct GroundDetection : IComponentData
    5.     {
    6.         public float distance;
    7.         public LayerMask groundLayer;
    8.         public bool1 IsGrounded;
    9.  
    10.         public GroundDetection(LayerMask groundLayer, float distance)
    11.         {
    12.             this.distance = distance;
    13.             this.groundLayer = groundLayer;
    14.             IsGrounded = false;
    15.         }
    16.     }
    Code (CSharp):
    1. public class GroundDetectionSystem : ComponentSystem
    2.     {
    3.         struct Group
    4.         {
    5.             public readonly int Length;
    6.             public TransformAccessArray transforms;
    7.             public ComponentDataArray<GroundDetection> groundDetection;
    8.         }
    9.  
    10.         [Inject] Group group;
    11.  
    12.         protected override void OnUpdate()
    13.         {
    14.             int length = group.Length;
    15.             if(length > 0)
    16.             {
    17.                 NativeArray<RaycastCommand> commands = new NativeArray<RaycastCommand>(length, Allocator.TempJob);
    18.                 NativeArray<RaycastHit> hits = new NativeArray<RaycastHit>(length, Allocator.Temp);
    19.  
    20.                 try
    21.                 {
    22.                     for (int i = 0; i < length; i++)
    23.                     {
    24.                         Transform t = group.transforms[i];
    25.                         GroundDetection groundDetection = group.groundDetection[i];
    26.  
    27.                         commands[i] = new RaycastCommand(t.position, Vector3.down, groundDetection.distance, groundDetection.groundLayer);
    28.                         Debug.DrawRay(t.position, Vector3.down * groundDetection.distance, Color.red);
    29.                     }
    30.  
    31.                     var raycastJob = RaycastCommand.ScheduleBatch(commands, hits, 8);
    32.                     raycastJob.Complete();
    33.  
    34.                     for(int i = 0; i < length; i++)
    35.                     {
    36.                         GroundDetection groundDetection = group.groundDetection[i];
    37.                         groundDetection.IsGrounded = hits[i].transform != null;
    38.                         group.groundDetection[i] = groundDetection;
    39.                     }
    40.                 }
    41.                 finally
    42.                 {
    43.                     commands.Dispose();
    44.                     hits.Dispose();
    45.                 }
    46.             }          
    47.         }
    48.     }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    My guessing is, there is debugging involved behind scene.

    If your ground is flat surface, you can easily calculate the distance altitude to its center.
    However, since at current state there is no ECS Physics as of yet, there is no way to detect shaped ground from mesh. You would need to implement own collision detection for now.
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,624
    You shouldn't profile memory allocations (for code you haven't written) in the editor, there are all sorts of safety checks going on. You need to compile and profile it at runtime, there are no allocations
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Its the leak detection code. You can disable using Jobs -> Leak Detection
     
  5. Freddx

    Freddx

    Joined:
    Mar 31, 2017
    Posts:
    5
    Is this leak detection disable when build or need be disable manually before build?
     
  6. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Does your ground move? If not I would just do this work once or at most periodically like if ground can be structures that come and go. Use a 2d grid implemented as a single NativeArray (info). Now in your other game code checking for ground is calculate the position index and index into the array to get your info.

    I do this for tracking fairly dense environment information. I build up a grid 512x512 around the player and periodically rebuild. It will use too much memory if you try to calculate it for large areas.