0
\$\begingroup\$

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 :)

\$\endgroup\$
2
  • \$\begingroup\$ Note that timeScale gets factored in by calling FixedUpdate more times per second of wall clock time. So you should not need to multiply anything inside FixedUpdate by timeScale. \$\endgroup\$
    – DMGregory
    Apr 16, 2020 at 19:48
  • \$\begingroup\$ However Time.frameCount counts displayed frames, not physics ticks. You should absolutely never reference this inside FixedUpdate, as it will give you different physics behaviour depending on your rendering framerate. If you want to fire this effector only periodically, you should do so using Time.fixedTime (or Time.time, which returns fixedTime when called inside FixedUpdate), to ensure your physics behaviour is not coupled to your time scale or rendering framerate. \$\endgroup\$
    – DMGregory
    Apr 16, 2020 at 19:57

0

You must log in to answer this question.

Browse other questions tagged .