x


CLick on terrain and record vector3 location I clicked at?

I need to click the terrain and know the location which i clicked at in order to tell a unity to go to that location. how do I click the terrain and retrieve at which location I clicked? should I use a raycast and collision or what? HELP!

more ▼

asked Apr 22 '10 at 03:04 PM

Jordan Miller gravatar image

Jordan Miller
70 6 6 9

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

3 answers: sort voted first

Use a ray, e.g.

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) 
{
  Debug.DrawLine (ray.origin, hit.point);
}

Docs here.

more ▼

answered Apr 22 '10 at 03:15 PM

Molix gravatar image

Molix
4.8k 16 27 66

lol, identical

Apr 22 '10 at 03:18 PM spinaljack

My CopyPaste-Fu is stronger than yours! :)

Apr 22 '10 at 03:21 PM Molix
(comments are locked)
10|3000 characters needed characters left

Send a ray cast from mouse position then you can get all the information you need from the raycast hit

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
    var hitPoint = hit.point;
}

doc

more ▼

answered Apr 22 '10 at 03:16 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

mine has the vector3 of the point under the mouse though

Apr 22 '10 at 03:19 PM spinaljack
(comments are locked)
10|3000 characters needed characters left
var clickPoint : Vector3;

function Update()

{

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

var hit : RaycastHit;



if (Physics.Raycast (ray, hit, 100) && Input.GetButton("Fire1")) 

{

    Debug.DrawLine (ray.origin, hit.point,Color.red);

    clickPoint = hit.point;

}
more ▼

answered Mar 18 '12 at 07:35 PM

hastenshawn gravatar image

hastenshawn
1 1

(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:

x1472
x576
x225
x80
x12

asked: Apr 22 '10 at 03:04 PM

Seen: 3722 times

Last Updated: Mar 18 '12 at 07:35 PM