Update vs FixedUpdate Transform positions

I’m having trouble understanding this problem. I have a Debug.DrawRay function in Update and FixedUpdate, both originating from the player’s transform.position. However, they appear to be at different locations on the screen while the character is in motion. See attached image for details. In this case, my character is falling and moving towards the right. As force/velocity increases, so does the distance between the Update and FixedUpdate rays. The rays under the capsule are being rendered in Update, the rays on the left are rendered in FixedUpdate. What’s going on here?

Quick edit: I’m moving the character by using rigidbody.AddForce; the character is falling due to gravity. Debug.DrawRay is the last thing to be called in both Update and FixedUpdate.

The only valid reason why they’d be different that I can think of is that you have a Rigidbody with inter/extra-polation on. In this case, it’s expected that the transform is different during update as it’s interpolating/extrapolating its position.

However, unless it’s moving really fast, I wouldn’t expect a large difference.

1 Like

Yeah, I’m not sure what’s going on. It doesn’t seem to be affecting the actual physics itself, at least not to the extent that I can observe. Interpolation is currently set to none. This problem is noticeable even at low velocities - you can see it pretty much as soon as the character starts moving.

After looking around online, it seems that other people are having this problem as well. I read somewhere that adding this inside of the FixedUpdate DrawRay can fix the position problem:

Debug.DrawRay(transform.position + m_rigidbody.velocity * Time.fixedDeltaTime, input.moveInput.normalized, Color.green)

This seems to imply that the Draw is somehow one step behind the current FixedUpdate step? I’m not familiar enough with Unity’s updating to work out what’s going on…

EDIT: I typed the code incorrectly before, I have edited it so that it makes sense now.

What do your Debug.DrawRay function calls look like right now?

In both Update and FixedUpdate, they looked like this:

Debug.DrawRay(transform.position, input.moveInput.normalized, Color.green);

With the ‘fix’ I found, the code in my FixedUpdate now looks like this:

Debug.DrawRay(transform.position + rigidbody.velocity * Time.fixedDeltaTime, input.moveInput.normalized, Color.green);

Update still looks the same as before.

Upon reviewing my previous post, I realized I incorrectly typed the function in the post (it is actually correct in my code). I will go back and edit that.

The same problem

Note the last post here was in 2015 and it’s not clear exactly what your problem is or even if you need help. If you require help then I suggest you ellaborate here or create a new thread.