#Getting Components and physicsWorld in job

1 messages · Page 1 of 1 (latest)

soft gust
#

I'm looking at the https://github.com/Unity-Technologies/CharacterControllerSamples/blob/master/_Documentation/Tutorial/tutorial-ai.md AI Controller example.

As a learning task I want to rewrite the systembase into a system with jobs to execute in parallel.

My issue is that I dont know how to access the physicsWorld inside Job and then read the LocalTransform component from the found entity.

does someone have experience with this?

GitHub

Sample projects for the Unity.CharacterController package - Unity-Technologies/CharacterControllerSamples

ruby sigil
#

you can pass the physics world to the job and use a component lookup for the localtransform, something like this
public partial struct SomeJob : IJobEntity (or w/e job type)
{
public PhysicsWorld physicsWorld;
public ComponentLookup<LocalTransform> LTLookup;
public void Execute(somequeryparams)
{
LTLookup.TryGetComponent(someEntity, out var transform);
physicsWorld.CollisionWorld.CalculateDistance(someValues);
}
}

hearty shadow
#

@ruby sigil 's answer above is right

However there's also an alternative in this specific case, if you need readonly access to the transform of an entity found by physics query. Lets say you do some raycast or CalculateDistance and you get a rigidBodyIndex from the hit result. With that index, you can then access the hit rigidbody transform in the PhysicsWorld like this:
physicsWorld.Bodies[rigidBodyIndex].WorldFromBody.pos

Just keep in mind that this will be the world transform at the time when the PhysicsWorld was built on this frame (at the start of the PhysicsUpdateGroup), and not necessarily the most up-to-date version of the transform component

ruby sigil
#

oh sweet i didnt realize you could do that, much cleaner way of doing things, thanks!

soft gust
#

Hi all, first of all thanks for the replys.
I finally have time to check the solutions. I went with reading the world position for now.
But the job is not happy when I run it in parallel any Ideas (Singlethreaded it works)?

[BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            new AIControllerJob()
            {
                physicsWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().PhysicsWorld
            }.ScheduleParallel();
        }    

[BurstCompile]
    public partial struct AIControllerJob : IJobEntity
    {
        public PhysicsWorld physicsWorld;

        [BurstCompile]
        public void Execute(AIDetectionAspect aiDetectionAspect)
        {
            aiDetectionAspect.Detect(physicsWorld);
        }
    }
InvalidOperationException: AIControllerJob.JobData.physicsWorld.CollisionWorld.EntityBodyIndexMap is not declared [ReadOnly] in a IJobParallelFor job. The container does not support parallel writing. Please use a more suitable container type.
Unity.Jobs.LowLevel.Unsafe.JobsUtility.ScheduleParallelFor (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters, System.Int32 arrayLength, System.Int32 innerloopBatchCount) (at <10871f9e312b442cb78b9b97db88fdcb>:0)
ebon meteor
#

The error is telling you exactly what to do

#

Put [ReadOnly] on it