Ray not shooting where I click


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).

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.

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.