How to use physicsVelocity.ValueRW.Angular to rotate in Unity DOTS ECS system?

I am using Unity DOTS ECS to build an RTS game like Warcraft. I was trying to create a system to rotate my unit to my mouse position. Because I am using Unity Physics, I would like to rotate the unit via its physicsVelocity.ValueRW.Angular value. However, I got stuck to the rotation part below:

float3 mousePosition = ...;
float3 unitCurrentPosition = localTransform.ValueRO.Position;

// How to set the ??? value to face my mousePosition?
physicsVelocity.ValueRW.Angular = new float3(0, ???, 0);

Hi!
You would need to start by defining two frames of reference in world space using the current orientation of your object and the target orientation, producing two unit quaternions.
Then you can calculate the delta rotation in world space between the two using quaternion math (conjugate of one times the other).
That resultant quaternion can be turned into an axis and angle using the corresponding conversion function from quaternion to that latter representation. The axis would correspond to the direction of the angular velocity that you want to add to the current angular velocity to induce the change of orientation. If you want to correct the entire orientation difference immediately in the next time step you would need to multiply it with the angle you obtained above and then divide that by the timestep.
If you any to slowly perform the change, just multiple the final result by some factor < 1, e.g. 0.3 or so, and redo the math again with the new orientations next frame and so on until there is no more delta.

Hope this helps.

1 Like