x


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

more ▼

asked Aug 05 '10 at 03:58 PM

ng93 gravatar image

ng93
173 4 4 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered Aug 05 '10 at 04:39 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

works great thanks

Aug 05 '10 at 05:14 PM ng93
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3014
x1535
x151
x56

asked: Aug 05 '10 at 03:58 PM

Seen: 3244 times

Last Updated: Aug 05 '10 at 03:58 PM