Debug.DrawRay from camera

Hi. I’m fairly new to unity and have been on it for a couple weeks now.

So I’m currently trying to draw a ray from my camera to wherever I click. I’ve got the ray barely working.

I see the ray only in scene and not game mode. I’ve tried turning it on in gizmos, setting a time duration for the ray, and already put it in the update function but I still just can’t see it in game. Only scene.

Also when I run it, the ray is already there without me having to click on any point. Regardless of where I click, the ray doesn’t change to where I click as well. Here’s what I have so far:


var ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 50000000,;


Would really appreciate if I could get some advice on this! Thanks!

Unity’s DrawRay function only shows up in the editor view, but if you’re willing to buy a package off the asset store, “Vectrosity” by Starscene Software has a DrawRay function that works in the Game view during runtime.

You need to use Camera.main to get the main camera. Also, the reason the line will never show in the game view even with gizmos on is because it’s oriented directly away from the camera. If you try to look at a 1-dimensional line from the end, you won’t be able to see it, since it would just be an infinitely small dot.

If you want the line to go from Input.mousePosition to a point in front of the camera’s transform, do this:

var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawLine (ray.origin, Camera.main.transform.forward * 50000000,;

In Editor game-mode window press button Gizmos, to draw gizmos.

In camera code add:

    void FixedUpdate()
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        Vector3 debugRayDrawStartPoint = new Vector3();
        debugRayDrawStartPoint = ray.origin;
        debugRayDrawStartPoint.y = ray.origin.y - 0.05f;

        if (Physics.Raycast(ray, out hit, 1))
            Debug.DrawLine(debugRayDrawStartPoint, hit.point,;
            Debug.DrawLine(debugRayDrawStartPoint, ray.GetPoint(1)); // or: Debug.DrawRay(debugRayDrawStartPoint, ray.direction * 1);