My Task:
I'm trying to replicate this virtual 3d creature evolution project. I'm using unity as my physics engine.
Each limb part has a MonoBehaviour
called an Effector
. The effector is what makes the creature move by applying forces to it using Rigidbody.AddRelativeTorque(Vector3 torquVector)
. I want the effector to act at contant frequency, relative to time (even when scaled). Meaning, if on time scale 1, the effector acted 10 times a second (real-time), a time scale of 10 will cause it to affect 100 times a second (real-time)
I'm changing Time.timeScale
during the evolution to speed things up, but I'm sure I'm missing something in the force calculation.
My code:
void FixedUpdate()
{
// Apply force
if (Time.frameCount % 10 != 0)
return;
float val = GetEffectorValue(); // Some calculated value
float delta = Time.fixedDeltaTime * Time.timeScale;
float force = val * TORQUE_WEIGHT * rb.mass * delta;
Vector3 forceVector = Vector3.zero;
// There are 3 effectors, each one operates on a differet axis
if (axis == Axis.X)
forceVector.x = force;
else if (axis == Axis.Y)
forceVector.y = force;
else if (axis == Axis.Z)
forceVector.z = force;
rb.AddRelativeTorque(forceVector);
}
The problem: The movement changes with the time scale. When the time scale is low, a creature doesn't really move (which is expected), but when the time scale is upped to 10 or more it is obvious that it moves more. In order to be sure that I'm not impatient and don't wait long enough I compared a 30 second period with a time scale of 2, to a 6 second period with a time scale of 10.
I tried a couple of other methods and looked at the unity documentation but couldn't figure this one out.
Thanks in advance :)