Raycast Fan

I’d like to have rays cast in a plane over a 180 degree angle starting from a point on a moving object. I’m trying to simulate a planar laser scanner. I’m not quite sure how to calculate the angle offset. I’ve tried Vector3.RotateTowards but this doesn’t seem to always rotate in the same direction. The rays are cast behind my laser scanner object depending on the direction it is facing. I would like to simply have a 180 iteration for loop and increment the direction of the ray, however I can’t quite get the math correct.

Jeff

So I ended up just rotating the entire empty with the following code:

private static var ARRAYSIZE=181;
private var ranges : float[];
function Start(){
	transform.Rotate(0,90,0);
}
function Update () {		
	for(var i=0; i<ARRAYSIZE;i++){
		var rotAmt = -181/ARRAYSIZE;
		transform.Rotate(0,rotAmt,0);
		var hit : RaycastHit;		
		Physics.Raycast(transform.position,transform.forward,hit,200.0);
		if(hit!=null)Debug.DrawLine(transform.position,hit.point,Color.green);
   }
	transform.Rotate(0,181,0);
}

However for some reason there is one random ray that shows up no matter where the fan is facing. You can see the issue in the following images:


Think this is a bug in my code or Unity?

Here’s how you could accomplish this without the need to rotate your transform:

var arcAngle : float = 180.0;
var numLines : int = 180;
for (l=0;l<numLines;l++) {
var shootVec : Vector3 = transform.rotation * Quaternion.AngleAxis(arcAngle*(l/numLines) - arcAngle/2, Vector3.up) * Vector3.forward;
var hit : RaycastHit;
if (Physics.Raycast(transform.position, shootVec, hit, 200.0)) {
Debug.DrawLine(transform.position, hit.point, Color.green);
}
}

I’m not sure where that stray ray could be coming from - I think that not relying on the transform to rotate may fix it. Maybe.

I had to make one small change, here’s the code:

function Update(){
	var arcAngle : float = 180.0;
	var numLines : int = 180;
	for (l=0;l<numLines;l++) {
		var shootVec : Vector3 = transform.rotation * Quaternion.AngleAxis(-1*arcAngle/2+(l*arcAngle/numLines), Vector3.up) * Vector3.forward;
		var hit : RaycastHit;
		if (Physics.Raycast(transform.position, shootVec, hit, 8.0)) {
		Debug.DrawLine(transform.position, hit.point, Color.green);
		}
	}
}

Thanks!

In case anyone is interested, here’s two functions that do the raycast scan. Most likely DoScan2 is what you will want.

//Centers scan of arcAngle degrees with numLines rays around "forward". Centering accounts for the "missing" last ray by arcAngle/(2*numLines) degrees.
//Start and end lines are not exactly on 0 and n degrees but offset by arcAngle/(2*numLines) degrees.
function DoScan1(){
	if(timer>1.0/scansPerSec){
		ranges = new float[numLines];
		for (l=0;l<numLines;l++) {
			var shootVec : Vector3 = transform.rotation * Quaternion.AngleAxis(-1*arcAngle/2+(l*arcAngle/numLines)+arcAngle/(2*numLines), Vector3.up) * Vector3.forward;
			var hit : RaycastHit;
				Debug.DrawRay(transform.position,shootVec,Color.red);
			if (Physics.Raycast(transform.position, shootVec, hit, maxDist)) {
				Debug.DrawLine(transform.position, hit.point, Color.red);
				ranges[l]=hit.distance;
			}
			else ranges[l]=maxDist;
		}
		timer=0;
	}
	else timer+=Time.deltaTime;
}

//Centers scan of arcAngle degrees with numLines rays around "forward". Centering accounts for the "missing" last ray by adding the last "extra" ray.
//Start and end lines are exactly on 0 and n degrees but there are numLines+1 rays.
function DoScan2(){
	nLines=numLines+1;
	if(timer>1.0/scansPerSec){
		ranges = new float[nLines];
		for (l=0;l<nLines;l++) {
			var shootVec : Vector3 = transform.rotation * Quaternion.AngleAxis(-1*arcAngle/2+(l*arcAngle/numLines), Vector3.up) * Vector3.forward;
			var hit : RaycastHit;
				Debug.DrawRay(transform.position,shootVec,Color.blue);
			if (Physics.Raycast(transform.position, shootVec, hit, maxDist)) {
				Debug.DrawLine(transform.position, hit.point, Color.blue);
				ranges[l]=hit.distance;
			}
			else ranges[l]=maxDist;
		}
		timer=0;
	}
	else timer+=Time.deltaTime;
}

I’m going to post this to the Wiki at some point.

Jeff

1 Like