Search Unity

Using Jobs to Edit Unity PhysicsVelocity on Entities

Discussion in 'Entity Component System' started by guerman2008, Nov 18, 2020.

  1. guerman2008

    guerman2008

    Joined:
    Nov 11, 2016
    Posts:
    1
    I'm trying to add some velocity to physics entities once their velocity drops below a certain threshold. I'm having some trouble understanding the Jobs System and how it relates to ECS and Entity Components. From what little information I can find, getting Entity Components in a Job requires some steps that don't have many examples.

    Here is how I'm scheduling the Job:

    Code (CSharp):
    1.         PhysicsJob.AddRandomPhysicsJob job = new PhysicsJob.AddRandomPhysicsJob()
    2.         {
    3.             entityArray = test,
    4.             velocities = foundVelocities,
    5.             minVel = m_MinVel,
    6.             randomXPos = randomX,
    7.             randomYPos = randomY,
    8.             randomZPos = randomZ,
    9.         };
    10.  
    11.         JobHandle jobHandle = job.Schedule(entityArray.Length, 32);
    12.         jobHandle.Complete();

    And here is how the job is structured:

    Code (CSharp):
    1.  
    2. public class PhysicsJob : JobComponentSystem
    3. {
    4.     private EndSimulationEntityCommandBufferSystem endSimulationEntityCommandBufferSystem;
    5.     protected override void OnCreate()
    6.     {
    7.         endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    8.         base.OnCreate();
    9.     }
    10.     public struct AddRandomPhysicsJob : IJobParallelFor
    11.     {
    12.         public NativeArray<Entity> entityArray;
    13.         public NativeArray<Unity.Mathematics.float3> velocities;
    14.         public float minVel;
    15.         public float randomXPos;
    16.         public float randomYPos;
    17.         public float randomZPos;
    18.         public EntityCommandBuffer.Concurrent entityCommandBuffer;
    19.         public void Execute(int index)
    20.         {
    21.             Entity entity = entityArray[index];
    22.             Unity.Mathematics.float3 vel = velocities[index];
    23.             Vector3 tempVel = new Vector3(vel.x, vel.y, vel.z);
    24.             if ((tempVel.magnitude < minVel))
    25.             {
    26.                 entityCommandBuffer.SetComponent(index, entity, new PhysicsVelocity() { Linear = new Unity.Mathematics.float3(randomXPos, randomYPos, randomZPos) });
    27.                 entityCommandBuffer.SetComponent(index, entity, new PhysicsDamping() { Linear = 0.01f, Angular = 0.05f });
    28.             }
    29.         }
    30.     }
    31.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    32.     {
    33.         AddRandomPhysicsJob job = new AddRandomPhysicsJob()
    34.         {
    35.             entityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
    36.         };
    37.         endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(inputDeps);
    38.         return inputDeps;
    39.     }
    40. }
    41.  
    42.  
    Currently, my understanding is that I construct the job and pass it the needed info with the "new" keyword.

    However, I'm getting confused with the difference between defining an IJobparallelFor struct in a monobehavior, and sticking it in a class that inherits from JobComponentSystem. What's the difference? Is this even the right approach for checking the component velocities and adding a random velocity?