I’m doing some experiments with the rotational motors from the EntityComponentSystemSamples. I would like to control the target angle of the motor at runtime.
I setup a scene with a static box and a dynamic box like the example with a joint, which I control with this system for testing:
[UpdateInGroup(typeof(BeforePhysicsSystemGroup))]
partial class MyMotorSystem : SystemBase
{
protected override void OnUpdate()
{
Entities.ForEach((ref PhysicsJoint joint) =>
{
float target = 0f;
if (Input.GetKey(KeyCode.Space))
{
target = -math.radians(45f);
}
if (joint[0].Target.x != target)
{
Debug.Log($"Changing target to {target}");
var constraints = joint.GetConstraints();
var constraint = constraints[0];
constraint.Target = new float3(target, 0f, 0f);
constraints[0] = constraint;
joint.SetConstraints(constraints);
}
}).Run();
}
}
It works with UnityPhysics, I can see the box rotating on a side by 45 degrees when I press Space, and go back to 0 degrees when I release Space.
However I tried switching simulation to Havok, and it stopped working, the box doesn’t move. If I run the original Unity example, motors do rotate to their target angle, but that angle is set at authoring time. I don’t understand what I’m missing in my code in order to change that target at runtime using Havok, when it otherwise works with UnityPhysics.