|
I am debugging a function that uses a RayCast from my vehicle position to the world plane. To help me see what is going on, I use
The problem is that when the vehicle is moving, the line is drawn as if it lags behind - that is, it is not rendering from the vehicle's position, but rather some point away from it (and in the opposite direction of the vehicle's movement). As soon as the vehicle stops, the line is rendered at the vehicle center again as it should. It looks like the transform.position does not coincide with the position where the vehicle is rendered. As you can imagine, this makes debugging very difficult, as I cannot trust any information obtained with a Debug.DrawLine. Is there a way to have it always render it from the right position, so that it starts from the vehicle position?
(comments are locked)
|
|
I believe Skovacs has the explanation right. The physics engine and scripts tend to change the position of the object, often in Update(). If your code is in Update() you are simply calculating using the original position, which is in the process of getting recalculated. Try putting your line render code in LateUpdate instead. It should read the position after it's being updated. That's where you'd typically put code to implement a camera that follows an object, for example. See also the Unity manual's notes about update order. Thanks, I have used this to solve the problem.
Jul 13 '10 at 06:40 AM
Herman Tulleken
(comments are locked)
|
|
I've found transform.position tends to lag a little behind the current position when moving with forces - generally I tend to find using rigidbody.position a lot better I will remember this, thanks!
Jul 13 '10 at 06:39 AM
Herman Tulleken
(comments are locked)
|

Are you moving the vehicle before the debug statement? If not, that could be your problem. If you draw a line and then move the vehicle, the line would obviously be in the wrong position at that frame.
Essentially, that was the problem. I assumed that the rendered image is the same as the physics at the beginning in the frame. I used the marked answer below to resolve the issue. (What makes it a little weird is that I need to render the line for debugging the next frame of physics).