- Home /
Ray not shooting where I click
Hello,
I am completely need to unity and I am trying to get the coordinates of a mouse click in world space. To do this I am trying to shoot a ray down and return where it hits the game object under the mouse click. The problem I am having is that the ray hits no where close to where I click my mouse.
This is the code I am using, taken straight from examples on the internet.
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit:RaycastHit;
Debug.DrawRay(ray.origin, hit.point, Color.red);
So, for example, if I click somewhere near point (5,-13,32) the ray hits at (98,1,50).
Answer by Eric5h5 · Aug 26, 2012 at 04:23 PM
You haven't done any raycasting in that example. Look up Physics.Raycast in the docs. Also, that's not how DrawRay is used...it takes a point and a direction, not two points. If you have two points, use DrawLine.
Answer by Adamcbrz · Aug 26, 2012 at 04:27 PM
You should be using Debug.DrawRay but instead Debug.DrawLine. DrawRay's second param is a direction while hit.point is the point you hit not the direction. You could use DrawRay and use ray.direction as the second param but I think DrawLine does more what you want.
Edit: Eric beat me to it.
Your answer
Follow this Question
Related Questions
Raycast not visible 1 Answer
Not getting right value; ScreenPointToRay 0 Answers
How to make a minimap camera zone grow bigger and smaller with zoom 0 Answers
Directional Shot (!) 1 Answer