- Home /
Raycast Lag - DebugDrawRay always one frame behind
Hi, i noticed some issue with my wheel collider setup, physics for my wheel are based on raycast from wheel collider center, but i guess its messed up because the center of wheel is registered behind actual center of the collider. Seems like it is previous frame position.
I am doing raycast in fixed update. I lowered the timestep significantly. Its raycasting down in local direction. I.e. inversed transformed direction, so is always aiming down properly, but this is probably quite issue if the position is registered behing the actual center of the collider.
Edit: When i put Debug Draw ray in Update() it is not lagging. Thats quite weird, i would expect opossite result, since as far as i know all physics calculations/raycasts should be done in FixedUpdate().
Anyway a bit of the requested code:
This is the stuff that basicly concernes rigidoby and raycast, there is much more code, but everything else doesnt relate directly to moving rigidbody.
void FixedUpdate()
{
rigidbody.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
if(Physics.Raycast(frontWheelCenter, Vector3.down, out hit)
groundPoint = hit.point;
Debug.DrawRay(frontWheelCenter, Vector3.down, Color.red);
}
I tried creating simple cube and adding only this code and it was lagging(1 frame offseted ). But its not happening when its in Update().
void FixedUpdate()
{
rigidbody.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
Debug.DrawRay(this.transform.position, Vector3.down, Color.red);
}
Any help greatly appreciated.
We need a lot more of your code (actually, any at all would be helpful) to help here. The problem may occur due to when you're doing the raycast, and when you're applying the movement. Also, Unity's physics does have a "margin of error" for colliders, to allow for smoother collisions, so it may be that you're seeing if, for example, you're drawing a ray and then moving the object.
it seems completely strange, and as Hoe explains it is meaningless without the code! Well done on posting an image.
sorry, can post code, i got 1500 lines of the code, everything works pretty well, but i dont understand why the rays are registered one frame behind and if thats normal. Seems as it aint :) Well, i got rigidbody, i move it basicly with AddRelativeForce. I draw the rays after this, but i tried to swap the functions and the result is still same, still one frame behind. Everything is in fixed update. In the code there is nothing realy non standard concerning moving the rigidbody. Also i am quite positive on the one frame lag, since with slow speed the ray position is just little bit behind wheel while at high speeds the position difference is quite significant.
just add Debug.Log lines at the relevant points and you'll soon figure out what is going wrong. Recall you can pause in the editor, and actually step through one frame at a time. Hope it helps
thanks, i did all those things tho, i got also three different debug rays, because i am rotating wheel collider to always face to ground with its ray, getting inverse normals to rotate wheel shadows etc. But although i went over each line roughly million times i never noticed any issue with the debug ray ( although its one frame behind:)). I am also quite positive that the bebug ray is called at right spot, since thats where the physics happen and it works well. I just wanted to know if this is something that happens with higher fixed time step or if this issue occurs at some cases, but it does seem like its some bug in my code then :/
Your answer
![](https://koobas.hobune.stream/wayback/20220613114058im_/https://answers.unity.com/themes/thub/images/avi.jpg)