Unity DOTS Physics: Swap between kinematic and dynamic

Which Components do I need to add or subtract from a dynamic object to make it kinematic in Unity DOTS?

I have a character that moves using kinematics while they’re grounded but swaps to dynamic when they fall off something or jump. They fall just fine when I add a PhysicsGravityFactor and a PhysicsVelocity but it passes through objects it should be colliding with.

The physics documentation suggests that it’s because it has no Mass so the physics calculations don’t know whether my character should stop moving or whether the thing it hit should get out of the way.

When I add a PhysicsMass too however, my character ends up zooming off to an invalid world position. Am I missing a component or is there a rule somewhere about not changing the kinematic/ dynamic-ness of an entity at runtime?

1 Like

Aha! I did it! This code:

The variable compMass is the PhysicsMass component already applied to the object.

EntityManager.SetComponentData<PhysicsMass>(entity, PhysicsMass.CreateDynamic(new MassProperties()
{
	AngularExpansionFactor = compMass.AngularExpansionFactor,
	MassDistribution = new MassDistribution()
	{
		Transform = compMass.Transform,
		InertiaTensor = compMass.InverseInertia //TODO!!! I don't know if these two things are the same.
	},
	Volume = 1 //1 is arbitrary. I have no idea how to calculate it.
}, newMassValue));

There’s also a CreateKinematic method if you wanted to go the other way. Same parameters, minus the newMassValue at the end there.

If anyone knows how to address those two comments, I’d appreciate the help.