x


Obtain a vector3 point in space from a ray hit

forst srry for my english,

im trying to get a point in the space from a ray, but im casting the ray from a touch, so i seach in the scrpt reference and get this code:

    for (var i = 0; i < Input.touchCount; ++i) {
    if (Input.GetTouch(i).phase == TouchPhase.Began) {
        // Construct a ray from the current touch coordinates
        var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
        if (Physics.Raycast (ray)) {
               //do something
        }
    }

i try to use: RaycastHit.Point for get the point in the space, but this dosnt work...

any can help me whit this?

i really dont understan this line of code, and there is the problem:

            if (Physics.Raycast (ray)) {

thanks

more ▼

asked Apr 24 '12 at 08:14 PM

rubenpvargas gravatar image

rubenpvargas
54 6 8 9

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

1 answer: sort voted first

You should define the origin and direction of the ray you've created otherwise the raycast will not have a start and a direction Vector3. Also, you should use a RaycastHit in order to obtain the collision details.

The code will look like so:

for (var i = 0; i < Input.touchCount; ++i) {

     if (Input.GetTouch(i).phase == TouchPhase.Began) {

            // Construct a ray from the current touch coordinates
            var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
            var collider: RaycastHit;

            if (Physics.Raycast (ray.origin, ray.direction, collider)) {
                   var collisionPoint: Vector3 = collider.point; // this will hold the exact position of the collision

            }
        }
more ▼

answered Apr 24 '12 at 08:24 PM

GutoThomas gravatar image

GutoThomas
613 32 46 47

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

x1579
x606
x155

asked: Apr 24 '12 at 08:14 PM

Seen: 577 times

Last Updated: Apr 24 '12 at 08:28 PM