• 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
1
Question by wiiiteekk · Oct 14, 2020 at 06:50 AM · physicsdots

[ECS] How to dynamically update body friction

Hello,

I have a problem with updating ECS PhysicsCollider material.

For now, while I am creating new phycis entity I am simply doing:

 Unity.Physics.Material someMaterial = new Unity.Physics.Material {
     Friction = 1,
     Restitution = 0.5f
 };
 
 BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.CapsuleCollider.Create(new CapsuleGeometry {
     Radius = baseDiameter/2,
     Vertex0 = new float3(0, 0.0f, 0),
     Vertex1 = new float3(0, height, 0)
 }, CollisionFilter.Default, someMaterial);
 
 EntityManager.SetComponentData<PhysicsCollider>(e, new PhysicsCollider { Value = collider });

And this is working perfectly fine. But the problem is when I want to change body collider, but use the same material. Here is some system to update colliders:

 protected override void OnUpdate()
 {
     var ecb = m_EndSimulationEcbSystem.CreateCommandBuffer().AsParallelWriter();
     
     List<RenderMesh> renderes = new List<RenderMesh>();
     EntityManager.GetAllUniqueSharedComponentData<RenderMesh>(renderes);
     foreach (RenderMesh render in renderes)
     {
         if (render.mesh == null) continue;
         var meshBounds = render.mesh.bounds;
         var center = (float3) meshBounds.center;
         
         Entities.WithSharedComponentFilter(render)
             .WithAll<UpdateColliderByMesh_TAG>()
             .ForEach((Entity e, int entityInQueryIndex, ref PhysicsCollider colliderComponent) => {
                 var collider = Unity.Physics.BoxCollider.Create(new BoxGeometry {
                     Center = meshBounds.center,
                     Size = meshBounds.size,
                     Orientation = quaternion.Euler(0, 0, 0),
                     BevelRadius = 0.01f
                 }); // <--- how to pass material from previous collider here????
                 colliderComponent.Value = collider;
                 ecb.RemoveComponent<UpdateColliderByMesh_TAG>(entityInQueryIndex, e);                       
             })
             .ScheduleParallel();
     }
 
     m_EndSimulationEcbSystem.AddJobHandleForProducer(this.Dependency);
 }

And this is also working fine BUT it resets collider material to default one, so it means body has no friction. So the question is how to get previous material and apply it to the new collider?

Any help will be more than welcome ;)

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 · Oct 17, 2020 at 11:31 PM 0
Share

quaternion.identity >= quaternion.Euler(0, 0, 0)

avatar image andrew-lukasik · Oct 17, 2020 at 11:42 PM 0
Share

this is sidenote too, but blob assets alloc memory so don't leave a leak there

 colliderComponent.Value.Dispose();
 colliderComponent.Value = collider;

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by NT_Ninetails · Dec 26, 2020 at 07:45 PM

@wiiiteekk the easy way would be to setup "prefabs for the material to be called later

  [ReadOnly] static Unity.Physics.Material[] MaterialPrefabs = new Unity.Physics.Material /*continue here*/

But if you're trying to access the data dynamically then you would have to cast the PhysicsCollider pointer to the matching collider you used to create it.

something like...

 unsafe{
 Unity.Physics.CapsuleColider* capsule_p = (Unity.Physics.CapsuleColider*)colliderComponent.ColliderPtr;            
 var collider = Unity.Physics.BoxCollider.Create(new BoxGeometry {
                      Center = meshBounds.center,
                      Size = meshBounds.size,
                      Orientation = quaternion.Euler(0, 0, 0),
                      BevelRadius = 0.01f
                  },capsule_p->Filter,capsule_p->Material); // <--- add pointer values
                  colliderComponent.Value = collider;
                  ecb.RemoveComponent<UpdateColliderByMesh_TAG>(entityInQueryIndex, e);                     
 }  

Though i don't recommend this unless you know what your doing.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

262 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 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

How do you detect collisions in DOTS Physics? 0 Answers

Unity.Physics Pure ECS/DOTS Implementation Causes Transforms.Translation to error into NaN 2 Answers

Entities.Foreach.Run causes Invalid IL code 0 Answers

Must include HavokSimulation:SyncJob as a dependency 1 Answer

[ECS] Move a collider to another collider 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