Hi everyone, I’m creating unity tests to verify that the way I apply forces to my objects is correct in Unity 2019.1.10f1.
I cannot seem to get the RigidBody2D.addTorque right in my classes. When that identical setup works for add force.
In my unity test in play mode, I create a RigidBody2D and make sure that gravity is not applied to it and that angular drag is 0.
Therefore my RigidBody has an inertia of 1 which is the default.
Here is the setup code
[OneTimeSetUp]
public void Init()
{
//Init runs once before running test cases.
go = new GameObject();
rb = go.AddComponent<Rigidbody2D>();
// Don't forget this, it seems like gravity is not the same as the
// Project settings.
rb.gravityScale = 0;
rb.angularDrag = 0;
}
[SetUp]
public void SetUp()
{
//SetUp runs before all test cases
rb.velocity = Vector2.zero;
rb.angularVelocity = 0;
rb.position = Vector2.zero;
rb.rotation = 0;
}
I would expect that if I apply a force of 50 N.m and my FixedDelta time is 0.02s, my rotational speed would be of 1 degree per second after that force has been applied and that the tick has passed.
I then apply a torque the following way :
[UnityTest]
public IEnumerator RotationTest2()
{
Debug.Log($"Initial angular speed is : {rb.angularVelocity}.");
rb.AddTorque(50, ForceMode2D.Force);
yield return null; // do simulation frame
Debug.Log($"Angular acceleration is : {50}.");
Debug.Log($"Final angular speed is : {rb.angularVelocity}.");
Debug.Log($"Expected angular speed is : {50 * Time.fixedDeltaTime / rb.inertia}.");
Assert.Less(Mathf.Abs(rb.angularVelocity - 50 * Time.fixedDeltaTime / rb.inertia)), 0.00001f);
}
The output of my unit test is the following :
Initial angular speed is : 0.
Angular acceleration is : 50.
Final angular speed is : 57.29578.
Expected angular speed is : 1.
Does someone see the mistake that I am currently doing?
Thank you in advance guys!
Elliot.