- Home /
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:
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
Answer by oliver-jones · Jul 02, 2011 at 05:51 PM
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.
Answer by Waz · Jul 02, 2011 at 11:38 AM
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);
Answer by AngryOldMan · Jul 02, 2011 at 12:10 PM
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
Your answer
Follow this Question
Related Questions
How can I offset a raycast along the transform's local x/z axis? 1 Answer
Multiple linecasts question 1 Answer
Offset of texture with Raycast 1 Answer
How to get an offset of a RaycastHit? 0 Answers
How can I get back a hit? 1 Answer