FixedUpdate delayed of a frame in relation to Physics, how to avoid that?

Hi everyone,

Here is an image of my problem:

I have a script called EngineManager, this script is attached to an empty game object represented by the purple circle on the image.

The aim of this script is to calculate the centre position of the engines, but as we can see on the picture, the circle is way behind the centre.

This is not a problem at low speeds, but as the speed increases it becomes more noticeable.

I think it is due to the synchronisation between the rigid body and the transformation causing a delay of one frame, but I am not sure.

Before doing that I was calculating the center position in the class without using a transform and I found that better to use transform instead and I had no issues.

Here is the script:

using UnityEngine;

public class EnginesManager : MonoBehaviour
{
    [SerializeField] EngineController[] engines;

    #region MonoBehaviour

    void Awake()
    {
        UpdateTransform();
    }

    void FixedUpdate()
    {
        UpdateTransform();

        AttractEngines();
    }

    void OnDrawGizmos()
    {
        Gizmos.color = Color.magenta;
        Gizmos.DrawSphere(transform.position, 0.2f);
    }

    #endregion

    void UpdateTransform()
    {
        transform.position = CalculatePosition();
        transform.rotation = CalculateRotation();
    }

    Vector3 CalculatePosition()
    {
        Vector3 center = Vector3.zero;
        foreach (var engine in engines)
            center += engine.transform.position;
        return center / engines.Count;
    }

    Quaternion CalculateRotation()
    {
        Vector3 eulerAngles = Vector3.zero;
        foreach (var engine in engines)
            eulerAngles += engine.transform.eulerAngles.To180();
        return Quaternion.Euler(eulerAngles / engines.Count);
    }
}

Use Update instead of FixedUpdate.

I tried but it doesn’t solve the issue.

When I Debug.Raycast() from the the engines center, I still have an offset:

As you can see, the purple circle is correctly placed but the raycast not, it should start where the circle is located.

Also, using Update for that is not a good for optimization since it will be called more that it needs to be.

Do the engine RBs have Interpolation enabled?

Thanks for the reactivity!

No, they don’t

image

From where are you calling Debug.Raycast?

It is called from another script in the FixedUpdate.

Right, so it’s possible that it’s being run before the center calculation has run. A script ordering problem.

However, I’m feeling a little uncomfortable with your whole setup. I’m not saying that this is necessarily the thing you should do (don’t have enough context) but in your place I would try enabling interpolation on your engine RBs, then your center calculation can happen in Update, as can your raycast (taking care, of course to ensure that the center calculation happens early in the fame).

In my experience, it’s best to keep FixedUpdate for manipulating physics stuff only (never touch or look at any transforms of physics objects, only RBs), then let Unity interpolate the correct values into the transforms prior to Update, so that way your code can rely on the transforms and base all calculations on where the physics objects actually are at render time.

The raycast is done in the FixedUpdate because I move the rigid body of the pod to follow the center position of the engines.

I thought it was best to do this in the FixedUpdate to avoid over-calculation because Updates is called more than FixedUpdate. I understand and will change that.

So in the FixedUpdate I apply the forces to the rigid body and the rest should be done in the Update, right?

Edit:

I just made the changes and it got worse, the movement is jerky and I still have the same problem. When I turn off the interpolation, everything goes back to normal.

Like I said, I don’t have enough context to know what other dependencies exist in your project. My rule of thumb in setting up projects is that FixedUpdate is for Rigidbodies, Update is for transforms.

Did you try investigating the script execution order?

Not yet, I will do that.

I made another topic about the jittering when rigibodies are interpolating.