Creating a Rigibody Entity at runtime results in NaN position and rotation

Hi all,

I am trying to create an entity at runtime that behaves like a Rigidbody. I don’t want to use bakers for this.

Right now I am trying the following (as inspired by various posts and the docs), but this instantly results in the entity’s position and rotation becoming all NaNs.

entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

Entity = entityManager.CreateEntity();
entityManager.SetName(Entity, "__dynamic");

entityManager.AddComponentData(Entity, new LocalTransform {
    Position = transform.position
});

entityManager.AddComponentData(Entity, new PhysicsDamping { Linear = 0.01f, Angular = 0.05f });
entityManager.AddComponentData(Entity, new PhysicsGravityFactor { Value = 1f });
entityManager.AddComponentData(Entity, new PhysicsMass { InverseMass = 1f, InverseInertia = new float3(1f,1f,1f) });
entityManager.AddComponentData(Entity, new PhysicsMassOverride { IsKinematic = 0 });
entityManager.AddComponentData(Entity, new PhysicsVelocity { });
entityManager.AddComponentData(Entity, new Simulate { });

entityManager.AddSharedComponentManaged(Entity, new PhysicsWorldIndex { Value = 0 });

Can anyone tell me what I’m missing here?

Thanks in advance

You didn’t set the rotation and scale. Scale will be 0. Rotation will be an all-zero quaternion, which does not represent a valid rotation.
Either specify values for those like 1 for scale and quaternion.identity for rotation, or use one of the static FromXXX methods on LocalTransform.
https://docs.unity3d.com/Packages/com.unity.entities@1.2/api/Unity.Transforms.LocalTransform.html

Appreciate the reply, but this did not solve my issue. Even with PhysicsMassOverride.IsKinematic set to 1, any physics update immediately gives me NaNs, even if I reset the local transform manually

UPDATE: You were close, turns out I needed to also set the PhysicsMass.Transform. Thanks for the advice!

1 Like

You’re supposed to use the PhysicsMass.CreateXX static methods to set up a PhysicsMass instance, as shown in the page you linked. There’s also MassProperties.UnitSphere/MassProperties.CreateBox for mass properties not derived from a collider.

2 Likes

hi,have you solve this problem ,i have the same problem when adding a PhysicsWorldIndex the the entity’s position and rotation becoming all NaN s