- Home /
Why does Debug.DrawLine offset sometimes?
I was testing a simple Linecast operation and decided to use Debug.DrawLine to draw line between my objects. I notice that when I select one of the objects while the editor is playing the line draw is offset. Here's the script:
var playerPosition: Transform; var subjectPosition: Transform; var hit: RaycastHit;
function Update () {
if(Physics.Linecast(playerPosition.position, subjectPosition.position, hit))
{
if(subjectPosition.collider == hit.collider)
Debug.DrawLine(playerPosition.position, subjectPosition.position, Color.red);
else
Debug.DrawLine(playerPosition.position, subjectPosition.position, Color.green);
}
}
I just setup a cube (player) and sphere (subject) and attached the script to the cube. When I don't touch anything, it behaves as expected:
When I manipulate the player object(cube), the line draw shifts all over the place:
If I move the sphere around it behaves as I would expect.
It looks ok; I don't see anything in that code which would cause the offset; I did a quick test and I didn't see any offset.
Just a note though, if you start the line at the center of the player/cube, it will likely collide with the player/cube first (i.e. hit.collider will be the player, rather than the subject). You may want to project the start point forward a bit in front of the player, or explicitly ignore the player's layer with a layermask.
Yes, I believe the Linecast is behaving properly. It does return the correct result. But the Debug.Drawline seems to behave erratically only when I select the source object. Unfortunately I was trying to look at some values in the inspector so I wanted that object selected but I've just worked around the problem now that I'm aware it happens.
Answer by Azound · Dec 04, 2010 at 10:31 PM
You were probably comparing the line endpoints to where you saw the translate gizmo in the scene view. Where the translate gizmo appears in the scene view is dependent on what your "Gizmo Position" is set to. If it is set to "Center", it will appear at the center of it's bounding box. It sounds like you want it set to "Pivot".
See the Gizmo Display Toggles portion of this guide: http://unity3d.com/support/documentation/Manual/Positioning%20GameObjects.html
Your answer
Follow this Question
Related Questions
OnDrawGizmos Debug.DrawLine position is pinching when rotated. 0 Answers
Figuring out status (sleep/awake in this example) of objects 1 Answer
Is there a way to debug the physics forces acting upon an object? 1 Answer
how to debug physics simulation? 0 Answers
2D 360 degress platformer example needed 0 Answers