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.
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?