Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by justinduy · Jul 08, 2020 at 08:57 AM · physicsdots

Can not add PhysicsCollider at runtime Unity in ECS

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. 1. Create plane gameobject.

  1. Add CopyTransformFromGameObjectProxy.

  2. Add PhysicsCollider to created entity from gameobject.

  3. 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[i], new PhysicsCollider());// not working
 
             _entityCommandBuffer.AddComponent(hybridEntities[i], new Translation());
             _entityCommandBuffer.AddComponent(hybridEntities[i], new Rotation());
             _entityCommandBuffer.AddComponent(hybridEntities[i], new LocalToWorld());
             _entityCommandBuffer.AddComponent(hybridEntities[i], new HybridEntityTag());
             
             _entityCommandBuffer.SetComponent(hybridEntities[i],collider); // not working
             Debug.Log(_entityManager.GetComponentData<PhysicsCollider>(hybridEntities[i]).ColliderPtr->Type.ToString()); // get error message ArgumentException: A component with type:PhysicsCollider has not been added to the entity.
         }
     }
 }





Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Jul 08, 2020 at 09:57 AM 0
Share

add PhysicsCollider to _queryEntityCacheDsc

avatar image andrew-lukasik · Jul 08, 2020 at 10:04 AM 0
Share

$$anonymous$$ove EntityQuery _entityQuery = GetEntityQuery() call to OnCreate (this needs to be done once, not every frame). $$anonymous$$ake for (int i = 0; i < hybridEntities.Length; i++) loop a Job because iterating over NativeArrays and Dots data outside jobs is very slow.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by NT_Ninetails · Dec 01, 2020 at 02:30 PM

to make a collider you can use the methods in the Unity.Physics namespace. find the Collider you want to create and add the corresponding Geometry.

  BlobAssetReference<Unity.Physics.Collider> box = Unity.Physics.BoxCollider.Create(new BoxGeometry {
             Center = float3.zero,
             BevelRadius = 0.05f,
             Orientation = quaternion.identity,
             Size = new float3(1,1,1)
 });

After that you can create a new Unity.Physics.PhysicsCollider setting the Value to our previously created collider.

     PhysicsCollider physicsCollider = new PhysicsCollider { Value = box};
     if (box.IsCreated)
     {
         commandBuffer.SetComponentData(entity, physicsCollider);
     }
     //or
  PhysicsCollider physicsCollider = new PhysicsCollider { Value = box};
     if (box.IsCreated)
     {
         entityManager.SetComponentData(entity, physicsCollider);
     }





Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity (ECS & DOTS) Entity Sinking 0 Answers

how to capsule cast from within Entities.ForEach job? ECS 1 Answer

2D 360 degress platformer example needed 0 Answers

(DOTS) How to slowly rotate an object to face a point? 1 Answer

Cannot move anything with pure ECS Physics instantiation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges