Sorry for the poor thread title, but I didn’t know a better way to explain it.
I have three raycasts. I need to check which raycasts hit something, then add their positions together to average them. With my sloppy coding skills, I’m just resorting to a bunch of if statements and temporary variables to keep track of how many of the rays hit (as you’ll see), but I have this strong feeling that there’s a much better way to code this in javascript/unityscript.
It already works, but I’m just trying to learn some better programming techniques. Excuse my crappy code.
var raycastL : RaycastHit;
var raycastM : RaycastHit;
var raycastR : RaycastHit;
var rayhitL = Physics.Raycast(Vector3(position.x - 9.0, position.y - 4.0,0.0), Vector3(0.0,-1.0,0.0), raycastL, 32.0);
var rayhitM = Physics.Raycast(Vector3(position.x,position.y - 4.0, 0.0), Vector3(0.0,-1.0,0.0), raycastM, 32.0);
var rayhitR = Physics.Raycast(Vector3(position.x + 9.0, position.y - 4.0,0.0), Vector3(0.0,-1.0,0.0), raycastR, 32.0);
if (rayhitM || rayhitL || rayhitR) {
var temp_hits : int = 0;
var temp_pos : float = 0.0;
if (rayhitL) { temp_hits++; temp_pos += raycastL.point.y; }
if (rayhitM) { temp_hits++; temp_pos += raycastM.point.y; }
if (rayhitR) { temp_hits++; temp_pos += raycastR.point.y; }
temp_pos /= temp_hits;
}