3
\$\begingroup\$

Here is the relevant code:

protected override void Update()
{
    base.Update();

    Debug.DrawRay(this.transform.position, this.transform.forward * 100f, Color.red, duration: 2f);
}

When running the game, I can see the ray being drawn in Scene View.

However, in Game View, no ray is displayed.

As you can see from the screenshot below, I have already enabled gizmos in Game View.

enter image description here

What should I do to ensure that rays are being drawn in Game View when debugging?

\$\endgroup\$
2
  • \$\begingroup\$ Are you sure the camera is looking at where the ray should be ? Can you see it in the editor ? \$\endgroup\$
    – Shashimee
    Commented Aug 2, 2018 at 7:33
  • \$\begingroup\$ @Shashimee Yes. I can see the rays in Scene View. But I cannot see them in Game View. The camera is pointing towards the game object that implements Debug.DrawRay(...) in the Update() call. \$\endgroup\$
    – M.Y. Babt
    Commented Aug 2, 2018 at 7:36

3 Answers 3

7
\$\begingroup\$

Debug.DrawRay (and other such functions) never draw in the game view

They're debug drawings and debug info and output should never appear in the "live" program view, which is supposed to mimic the compiled executable's behavior, which strips all references to Debug. If you want to draw in the game window you need top use a LineRenderer or other runtime solution (UI objects, etc).

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Turns out you can view it in the Scene view: launch game which puts it into Game view and then switch back to Scene view to see the raycast. This was driving me crazy until I read this answer. \$\endgroup\$ Commented Feb 3, 2021 at 22:02
0
\$\begingroup\$

Without an actual screenshot showing the problem, I can only make suggestions that may or may not apply to your situation.

First, make sure that the camera is not pointed directly in the same direction as the ray being drawn. Otherwise it might only show up as infinitely in line.

Second, by default, Debug.DrawRay draws lines with depth testing enabled, so if the camera is behind the object, and the ray is being drawn mostly away from the camera, the object itself would obscure the line. Or there could be something else in the way. If this is the only problem, then you could simply disable depth testing:

Debug.DrawRay(this.transform.position, this.transform.forward * 100f, Color.red, duration: 2f, depthTest: false);

As a sidenote, is there a reason duration is set to 2? Since this is being called in Update, no duration is required unless you want it to continue to be drawn after the object is disabled, or if the object is moving or rotating and you want a visual record of where it was in previous frames.

\$\endgroup\$
0
\$\begingroup\$

Make sure your component that draws the ray is expanded when viewed from the Inspector. If it's collapsed, it won't draw Debug stuff. I've waisted countless hours debugging my * caugh * debugging tools only to find out it's all down to a component being expanded or not.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .