- Home /
Mouse raycast not working/not seeing line
I am trying to get a 3d position in my game when i click but when i try to Debug.DrawRay the ray nothing shows up in scene mode. Here is what i have:
void Update(){
if(Input.GetMouseButtonDown (0)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction, Color.red);
}
}
Note this ray is on 1 unit long and will never be seen in the Game view. If length is the issue, do:
Debug.DrawRay(ray.origin, ray.direction * 1000.0f, Color.red);
I just tested your code it just works. but check this. 1) your script attack to any game object. 2) try this
Debug.DrawRay(ray.origin, ray.direction, Color.white,5);
Oh, I should have taken a closer look. @yashpal's solution will work since it displays the line for 5 seconds. The problem is this:
if(Input.Get$$anonymous$$ouseButtonDown (0)){
...it is only true for a single frame. You could switch to using Get$$anonymous$$ouseButton(), but to see the line in Scene view, you'd have to display both Scene and Game views with the focus in the Game view and the left mouse button down.
Your answer
Follow this Question
Related Questions
click on a game object that script isn't attached to 1 Answer
Checking if object stopped being hit by Raycast while overlapping another object of the same type. 0 Answers
Clicked object is stored in a variable? 1 Answer
C# mouse Raycast question 3 Answers
How do I find the centroid of 4 points in 3D space? 1 Answer