2
\$\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 Aug 2 '18 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\$ – MY_G Aug 2 '18 at 7:36
4
\$\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
  • \$\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\$ – loganjones16 Feb 3 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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.