x


Multiple Raycasts

Hello,

I'm trying to get 5 raycast systems to work but I'm having difficultly trying to offset them.

What I'm trying to do is to have a raycast come from my Input.mousePosition, but also have 4 other raycasts around that, that are slightly offset. But I suck when it comes to vectors.

So basically, this is how I want my set up:

var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var layerMask = 1 << 14;

Physics.Raycast (ray, hit, 200, layerMask); // center line

Physics.Raycast (ray, hit.(z + 2) (x -2), 200, layerMask); // upper left

Physics.Raycast (ray, hit.(z + 2) (x +2), 200, layerMask); // upper right

Physics.Raycast (ray, hit.(z - 2) (x -2), 200, layerMask); // lower left

Physics.Raycast (ray, hit.(z - 2) (x +2), 200, layerMask); // lower right

This obversely isn't going to work, but thats what I am trying to achieve. So each raycast that is being shot from the Input.mousePosition is to be offset by +2/-2. I also have a feeling that it has nothing to do with the 'hit', but is to do with the ray that I need to be adjusting?

I would appreciate any help with this.

------ UPDATE ------

Okay, so I'm making some progress. I have 5 raycasts that are shot with an offset from the camera, but towards the other end of the shot, they are angling towards each other. Here is a screen shot to help explain:

alt text

Here is my current code:

var hit : RaycastHit;
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var layerMask = 1 << 14;
    var hitPoint : RaycastHit;

    if(debugClick){//chooseLocation){
       layerMask = ~layerMask;
       // center line
       Physics.Raycast (ray, hit, 200, layerMask);
       Debug.DrawLine (ray.origin, hit.point);

       // line 1
       var offset = Vector3(ray.origin.x + 2, ray.origin.y, ray.origin.z);
       var direction = transform.position;
       Physics.Raycast (offset, direction, hit, 200);
       Debug.DrawLine (offset, hit.point, Color.green);

       // line 2
       var offset2 = Vector3(ray.origin.x - 2, ray.origin.y, ray.origin.z);
       var direction2 = transform.position;
       Physics.Raycast (offset2, direction2, hit, 200);
       Debug.DrawLine (offset2, hit.point, Color.red);

       // line 3
       var offset3 = Vector3(ray.origin.x, ray.origin.y, ray.origin.z + 2);
       var direction3 = transform.position;
       Physics.Raycast (offset3, direction3, hit, 200);
       Debug.DrawLine (offset3, hit.point, Color.blue);

       // line 4
       var offset4 = Vector3(ray.origin.x, ray.origin.y, ray.origin.z - 2);
       var direction4 = transform.position;
       Physics.Raycast (offset4, direction4, hit, 200);
       Debug.DrawLine (offset4, hit.point, Color.yellow);123

123

more ▼

asked Jul 02 '11 at 11:28 AM

oliver-jones gravatar image

oliver-jones
2.5k 205 226 254

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

3 answers: sort voted first

I believe I have finally done it - I needed to also give each raycast a unique RaycastHit variable:

    var hit : RaycastHit;
    var hit1 : RaycastHit;
    var hit2 : RaycastHit;
    var hit3 : RaycastHit;
    var hit4 : RaycastHit;

    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var layerMask = 1 << 14;
    var hitPoint : RaycastHit;

    if(debugClick){//chooseLocation){
       layerMask = ~layerMask;
       // center line
       var offsetCen = Vector3(ray.origin.x, ray.origin.y, ray.origin.z);
       var directionCen = transform.TransformDirection (Vector3.forward);
       Physics.Raycast (offsetCen, directionCen, hit, 200, layerMask);
       Debug.DrawLine (offsetCen, hit.point);

       // line 1
       var offset = Vector3(ray.origin.x + 2, ray.origin.y, ray.origin.z);
       var direction = transform.TransformDirection (Vector3.forward);
       Physics.Raycast (offset, direction, hit1, 200, layerMask);
       Debug.DrawLine (offset, hit1.point, Color.green);

       // line 2
       var offset2 = Vector3(ray.origin.x - 2, ray.origin.y, ray.origin.z);
       var direction2 = transform.TransformDirection (Vector3.forward);
       Physics.Raycast (offset2, direction2, hit2, 200, layerMask);
       Debug.DrawLine (offset2, hit2.point, Color.red);

       // line 3
       var offset3 = Vector3(ray.origin.x, ray.origin.y, ray.origin.z + 2);
       var direction3 = transform.TransformDirection (Vector3.forward);
       Physics.Raycast (offset3, direction3, hit3, 200, layerMask);
       Debug.DrawLine (offset3, hit3.point, Color.blue);

       // line 4
       var offset4 = Vector3(ray.origin.x, ray.origin.y, ray.origin.z - 2);
       var direction4 = transform.TransformDirection (Vector3.forward);
       Physics.Raycast (offset4, direction4, hit4, 200, layerMask);
       Debug.DrawLine (offset4, hit4.point, Color.yellow);

I'm not 100% if its working, but it certainly looks like it according to the debug lines.

more ▼

answered Jul 02 '11 at 05:51 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 226 254

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

Change ray.direction, not the hit variable. Give it a try yourself, that way you'll learn how unscary vectors are. To change a vector:

v += Vector3(1,-2,3);
more ▼

answered Jul 02 '11 at 11:38 AM

Waz gravatar image

Waz
6.5k 22 33 71

Okay, please look at my updated answer.

Jul 02 '11 at 11:51 AM oliver-jones
(comments are locked)
10|3000 characters needed characters left

hello. I believe you may need to change these lines:

var direction = transform.position;
var direction2 = transform.position;
var direction3 = transform.position;

as these are the other end of your line and they all point to the same location. I'm not 100% sure as I dont have any of my scripts to hand but that's what I would try first. Good luck

more ▼

answered Jul 02 '11 at 12:10 PM

AngryOldMan gravatar image

AngryOldMan
2.6k 12 21 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:

x1535
x293
x109
x31

asked: Jul 02 '11 at 11:28 AM

Seen: 1717 times

Last Updated: Jul 02 '11 at 05:51 PM