Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Assigning MassDistribution at runtime causes the RigidBody entity to no longer respond to colissions

Discussion in 'Physics for ECS' started by Senshi, May 14, 2024.

  1. Senshi


    Oct 3, 2010
    Hi all,

    To quickly explain what I'm doing: I am foregoing the baking process and am creating and managing my entities at runtime to facilitate a hybrid approach.

    When I create an RB entity everything works fine. The object gets pushed around by other physics bodies as expected. However, I was looking into freezing certain axes and came across this post that suggested modifying the MassProperties' inertia tensor. I looked at the `PhysicsBodyAuthoring` and accompanying baking system and tried replicating this on my end. However, whenever I try to set the mass distribution the entity stops responding to colissions; the previously pushing body now simply phases through. Comparing the entity's components before and after, everything seems identical.

    What am I missing here?

    Code (CSharp):
    1. MassProperties massProperties = massShape == MassShape.Sphere ? MassProperties.CreateSphere(radius) : MassProperties.CreateBox(size);
    3. // Modifying the massProperties in any of the below two ways causes it to break
    4. // massProperties.MassDistribution.InertiaTensor = inertiaTensor;
    6. massProperties.MassDistribution = new MassDistribution {
    7.     Transform = RigidTransform.identity,
    8.     InertiaTensor = inertiaTensor
    9. };
    11. if(isKinematic) {
    12.     host.AddComponentData(PhysicsMass.CreateKinematic(massProperties));
    13. }
    14. else {
    15.     host.AddComponentData(PhysicsMass.CreateDynamic(massProperties, mass));
    16. }
    I did consult the docs, but "Diagonalized inertia tensor for a unit mass" goes a bit over my head, and didn't provide any answers.

    Thanks in advance!
  2. daniel-holz


    Unity Technologies

    Sep 17, 2021
    Check in the entities inspector (runtime mode of the inspector during PlayMode) what values change when you make these modifications.

    Make sure that all values are set in the MassDistribution struct and don't default to zero causing potential issues.
    If you haven't already, you can have a look at the RigidBodyBakingSystem which creates the body mass properties and make sure you are not missing anything.