In this lesson, you will learn how to draw lines and rays directly in your Scene view to make your invisible game logic visible.
When you write code for physics or AI, such as checking if an enemy has a clear path to the player, you are often working with invisible "math" vectors. If your calculation is slightly off, it can be nearly impossible to debug by looking at the Console alone. Unity provides the Debug class, which allows you to "draw" these calculations so you can see exactly what your code is doing in real-time.
Visualizing Lines and Rays
The most common tools for this are Debug.DrawLine and Debug.DrawRay.
Debug.DrawLine(start, end): Draws a line between two specific points in world space.Debug.DrawRay(start, direction): Draws a line starting at a point and extending in a specific direction for a set length.
These lines only appear in the Scene view (and the Game view if "Gizmos" is toggled on) while the game is running. They are not part of your final game build, making them perfect for temporary troubleshooting.
Here is how you might visualize a line-of-sight check in your Update() method:
void Update() {
Vector3 start = transform.position;
Vector3 direction = transform.forward * 5f;
// Draw a green line showing the forward path of the object
Debug.DrawRay(start, direction, Color.green);
}
Why Use Debug Visualizations?
By drawing these vectors, you can instantly see if your transform.forward is pointing where you expect it to, or if a raycast is hitting an object at the wrong angle. If you were testing a jump trajectory or a projectile’s path, you could use Debug.DrawLine to connect the points calculated by your movement logic. This turns abstract vector math into a physical shape that helps you identify errors immediately, rather than guessing why your object isn't moving as intended.
Key Takeaway: Use Debug drawing methods to turn invisible vector calculations into visible geometry, allowing you to visually verify your logic directly in the Scene view.