Search Unity

Debug.DrawRay() does not draw rays in game view

Discussion in 'Scripting' started by MiaoBolverk, Aug 2, 2018.

  1. MiaoBolverk

    MiaoBolverk

    Joined:
    Feb 6, 2018
    Posts:
    26
    Here is the relevant code:
    Code (csharp):
    1. protected override void Update()
    2.     {
    3.         base.Update();
    4.  
    5.         Debug.DrawRay(this.transform.position, this.transform.forward * 100f, Color.red, duration: 2f);
    6.     }
    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.



    What should I do to ensure that rays are being drawn in Game View when debugging?
     
    Last edited: Aug 2, 2018
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    You can't see the ray in GameView. Instead you can use a LineRenderer.
     
  3. MiaoBolverk

    MiaoBolverk

    Joined:
    Feb 6, 2018
    Posts:
    26
    Thank you for your reply! Using the line renderer means that the line will also show up in the deployed build, no? I can of course delete those parts again, but it is a hassle.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "You can't see the ray in GameView. Instead you can use a LineRenderer."

    @LilFire

    You can show the debug draw stuff - in editor that is.
    For example, this works just fine in editor's game view if Gizmos button is pressed:

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4. Ray ray = new Ray(transform.position, transform.forward);  
    5. Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
    6. }
    7.  
    There must be something else going on.
     
    S1NATB, d23z, OikoumE and 6 others like this.
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Debug is designed for debug purposes, in development. Not as if player should see it. So in case of Debug.DrawLine, you look into scene, not the game itself. Change tab to scene at runtime, or add another scene window panel.

    LineRenderer is an game object, with defined number of vertices and their potions. It is not strictly for debugging, but can be used for it too. Then you do with that LineRenderer GameObject, whatever you need. Show hide, set color texture, sqeez, change vertices positions etc.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Another option is the GL API.
     
    Antypodish likes this.
  7. flecheria

    flecheria

    Joined:
    Apr 25, 2019
    Posts:
    17
    Gauvater likes this.
  8. ertbaran

    ertbaran

    Joined:
    Feb 22, 2019
    Posts:
    11
    Thanks. Gizmos button solved my problem :)