- Home /
mouse position on terrain
hi im trying to get a Vector3 of the mouse cursors position on the terrain, and then place an object there. ive tried:
var mousePo = camera.ScreenToWorldPoint (Vector3 (Input.mousePosition.x, Input.mousePosition.y,camera.nearClipPlane));
var ray = camera.ScreenPointToRay(mousePo);
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
Instantiate (object, mousePo, Quaternion.identity);
but the ray is not drawn and although the object appears over the mouse cursor, it is too close to the screen.
thanks, ng93
Answer by Wolfram · Aug 05, 2010 at 04:39 PM
You will not see the ray unless you move the camera, because you're looking along the ray.
If you want to find the intersection of that ray with the terrain (and therefore the length of that ray), you need to do a Physics.Raycast with that ray:
var hit:RaycastHit;
if(Physics.Raycast(ray.origin,ray.direction, out hit)){
print("world point on terrain: "+hit.point+", distance to point: "+hit.distance);
}
Your answer
Follow this Question
Related Questions
Camera.ScreenToWorldPoint with Perspective camera 1 Answer
Mouseclick GameObject through the view of a RenderTexture 0 Answers
Follow mouse cursor (with Y = 0)? 1 Answer
Camera Ray - NullReferenceException: Object reference not set to an instance of an object 0 Answers
Get 2D Collider with 3D Ray 2 Answers