I’m trying to use UnityPhysics ECS system on gameObject, my goal is sync gameObject’s position with entity translation and to add PhysicsCollider to gameObject to interact with other entities.
My steps.
-
Create plane gameobject.
-
Add CopyTransformFromGameObjectProxy.
-
Add PhysicsCollider to created entity from gameobject.
-
Change entity’s collider by get collider from cached entity.
Thanks from helping.
[UpdateBefore(typeof(BuildPhysicsWorld))]
public class AddingPhysicsSystem : SystemBase
{
EntityQueryDesc _queryEntityCacheDsc;
EntityQueryDesc _queryEntityHybricDsc;
EntityCommandBuffer _entityCommandBuffer;
EntityManager _entityManager;
protected override void OnCreate()
{
base.OnCreate();
_queryEntityCacheDsc = new EntityQueryDesc()
{
All = new ComponentType[] { typeof(EntityCacheTag) }
};
_queryEntityHybricDsc = new EntityQueryDesc()
{
All = new ComponentType[] { typeof(Transform) },
None = new ComponentType[] { typeof(HybridEntityTag) }
};
_entityCommandBuffer = World.DefaultGameObjectInjectionWorld.GetExistingSystem<EndSimulationEntityCommandBufferSystem>().CreateCommandBuffer();
_entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
}
protected override unsafe void OnUpdate()
{
// get cached entity
EntityQuery _entityQuery = GetEntityQuery(_queryEntityCacheDsc);
var cachedEntity = _entityQuery.ToEntityArray(Allocator.Temp)[0];
// get collider preset
var collider = _entityManager.GetComponentData<PhysicsCollider>(cachedEntity);
// get gameObject entity with CopyTransfromFromGameObjectProxy component
_entityQuery = GetEntityQuery(_queryEntityHybricDsc);
var hybridEntities = _entityQuery.ToEntityArray(Allocator.Temp);
// add PhysicsCollider at runtime to gameObject entity
for (int i = 0; i < hybridEntities.Length; i++)
{
_entityCommandBuffer.AddComponent(hybridEntities*, new PhysicsCollider());// not working*
_entityCommandBuffer.AddComponent(hybridEntities*, new Translation());
_entityCommandBuffer.AddComponent(hybridEntities, new Rotation());
_entityCommandBuffer.AddComponent(hybridEntities, new LocalToWorld());
_entityCommandBuffer.AddComponent(hybridEntities, new HybridEntityTag());*
entityCommandBuffer.SetComponent(hybridEntities*,collider); // not working*
Debug.Log(entityManager.GetComponentData(hybridEntities*).ColliderPtr->Type.ToString()); // get error message ArgumentException: A component with type:PhysicsCollider has not been added to the entity.*
}
}
}