- Home /
Debug.DrawRay - Can't get it to work
Hello,
what I actually want to do is placing a building, however I can't get Debug.DrawRay nor Debug.DrawLine to work.
I've tried numerous things so far, I'd be happy with seeing a Ray / Line at all so I recently changed the code to the following: (within the camera script)
Vector3 forward = transform.forward;
Debug.DrawRay(transform.position, forward);
Debug.DrawLine(transform.position, transform.position + forward * 10000);
Before that I tried the code from another question (http://answers.unity3d.com/questions/14474/add-building-in-rts) without success: ("Hit" would only be logged if the mouse is way off screen)
Ray ray = camera.ViewportPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity))
{
Debug.Log("Hit");
}
I really hope that I'm just stupid and missing something obvious. (First time I have to do with 3D in general, implementing a working Dijkastra-Pathfinder is the only thing I managed so far in Unity ^_^)
Answer by pyro · Jul 18, 2010 at 09:26 PM
From the script reference:
// Draws a 10 meter long green line from the position
// forward along the transform's z-axis.
function Update () {
var forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay (transform.position, Vector3.forward * 10, Color.green);
}
It needs to be an update or it will draw and be finished faster than you can see it on the editor.
Thanks for the answer, but it is not working. I just changed the camera position to 0/10/0 but even then it is not working.
@sebastian: It works fine: http://starscenesoftware.com/stuff/pics/drawray.png Did you put the script on some object?
I added it to my camera scripts update, before everything else (movement etc.)
Interesting, I just recreated the camera object, now it works.
the ray is only visible in scene view during play. how do i make it visible within the game view...?
Answer by Tolling87 · Jan 04, 2014 at 12:22 AM
The reason it would not show is that Debug.DrawRay does not draw anything when assigned the mathf.infinity. So remember when debugging, always assign values on it.
Answer by Geejayz · Oct 28, 2017 at 10:03 AM
It could be something as simple as you don't have Gizmos turned on in the GUI.
Hopefully given the dates above the OP has already resolved this. Just adding this as it turned out to be my issue.
Your answer
Follow this Question
Related Questions
Problems with raycast 1 Answer
Debug.DrawRay Issue? 0 Answers
Raycast feelers not reading in all four directions. 0 Answers
Debug.DrawRay not drawing since upgrading my project to 3.3.* 1 Answer