Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Adding/removing components or changing position/rotation/velocity/collide ECS data on dynamic entit

Discussion in 'Physics for ECS' started by Cell-i-Zenit, May 26, 2021.

  1. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    Hi,

    i checked the samples and this is pretty close to what i want to do:

    https://github.com/Unity-Technologi...cripts/ChangeSphereColliderRadiusAuthoring.cs

    i want to change the box size of an entity

    here is my code:


    Code (CSharp):
    1.  [UpdateBefore(typeof(BuildPhysicsWorld))]
    2.   [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    3.   public class EditorCameraSystem : SystemBase
    4.   {
    5.     protected override void OnUpdate()
    6.     {
    7.         Entities.ForEach((ref PhysicsCollider physicsCollider) =>
    8.           {
    9.             unsafe
    10.             {
    11.               BoxCollider* collider = (BoxCollider*) physicsCollider.ColliderPtr;
    12.               var geo = collider->Geometry;
    13.               geo.Size = new float3(1, 1, 1);
    14.               collider->Geometry = geo;
    15.             }
    16.           })
    17.           .WithAll<CursorFlag>()
    18.           .Run();
    19.     }
    but it results in the following error:

    Code (CSharp):
    1. InvalidOperationException: Adding/removing components or changing position/rotation/velocity/collider ECS data on dynamic entities during physics step
    i also tried to add the dependency to the buildPhysicsWorld like this, which works in some other parts of the code but not here:

    Code (CSharp):
    1. Dependency = Entities.ForEach((ref PhysicsCollider physicsCollider) =>
    2.   {
    3.     etc...
    4.   })
    5.   .Schedule(Dependency);
    6.  
    7. _buildPhysicsWorld.AddInputDependency(Dependency);
    Any idea what iam doing wrong here? I think i am really close to the samples, but apparently i have a mistake
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    You aren't scheduling your system, just Running it. The error is coming from the physics integrity checks. Do you have other systems, running between BuildPhysicsWorld and ExportPhysicsWorld that might be changing the ECS structure of the underlying data e.g. removing or adding components or entities?
     
    Hello_hxh likes this.
  3. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    I also tried running the system as i mentioned earlier (with the schedule(Dependency))



    I get this error (and also the failing job error) when scheduling the job:

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Unity.Physics.Collider.get_Type () (at Library/PackageCache/com.unity.physics@0.6.0-preview.3/Unity.Physics/Collision/Colliders/Physics_Collider.cs:95)
    Could it be because i get a wrong reference somehow?


     
  4. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    What does CoreTriggerSystem do?
     
  5. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    coreTriggerSystem never runs when i activate the box size changes.

    But anyway it runs at these times:

    Code (CSharp):
    1.  [UpdateAfter(typeof(StepPhysicsWorld))]
    2.   [UpdateBefore(typeof(EndFramePhysicsSystem))]
    3.   [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    and it is a physics collision job scheduled like this:

    Code (CSharp):
    1.    Dependency = collisionEventJob.Schedule(_stepPhysics.Simulation, ref _physicsWorld.PhysicsWorld, Dependency);
    2.  
    3.       _endFramePhysicsSystem.AddInputDependency(Dependency);
    and has a structure like this:

    Code (CSharp):
    1.  internal struct CollisionEventJob : ITriggerEventsJob
    2. {
    3.     public void Execute(TriggerEvent triggerEvent)
    4.     {
    5.       ProcessEntity(triggerEvent.EntityA, triggerEvent.EntityB);
    6.       ProcessEntity(triggerEvent.EntityB, triggerEvent.EntityA);
    7.     }
    8. }
     
  6. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Does ProcessEntity do something like adding/editing/removing component data?