Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Issues with Debug.DrawRay

Discussion in 'Editor & General Support' started by benjrose89, Jun 17, 2020.

  1. benjrose89

    benjrose89

    Joined:
    Jun 17, 2020
    Posts:
    8
    Newbie here! I'm working on a simple game based around a lighthouse. I want to be able to tell if an object is in the lighthouse's view or not. Figured the best way would be to raycast.

    Trouble is, I can't even get Debug.DrawRay to show anything in the scene view. I've googled around the issue, but mostly the fix has been to turn on Gizmos - which I already have!

    Does anyone have any insight into why this might not be working? Any great ideas on my overall goal also greatly appreciated.

    Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LensBehaviour : MonoBehaviour
    6. {
    7.     public float lensRotationSpeed;
    8.     public float debugRayLength;
    9.     private RaycastHit vision;
    10.  
    11.     void Update()
    12.     {
    13.         // Simple rotation controls
    14.         float rotation = Input.GetAxis("Horizontal") * lensRotationSpeed;
    15.         rotation *= Time.deltaTime;
    16.         transform.Rotate(0, -rotation, 0);
    17.  
    18.         //Light ray detection
    19.         Debug.DrawRay(transform.position, transform.forward * debugRayLength, Color.red); //Debug code
    20.  
    21.     }
    22. }
    23.  
     
  2. TomasKucinskas

    TomasKucinskas

    Unity Technologies

    Joined:
    Dec 20, 2017
    Posts:
    60
    Hello, Ben

    I've tried your script and it seems to work. I've set `Lens Rotation Speed` to 1 and `Debug Ray Length` to 10 and it's quite visible that when you press arrow keys you get that line to shift to one or the other side.

    The most probable catch here is that you must be focused on the `Game` tab for your script to work and the debug line will only show up in `Scene` view tab.

    Get them side by side and you'll see that your script works perfectly.
     
  3. benjrose89

    benjrose89

    Joined:
    Jun 17, 2020
    Posts:
    8
    Ah, that makes sense. Thanks!