Here’s the code:
public GameObject getSensorObject(AISensor ais){
Vector3 leftPos = this.transform.position;
Vector3 rightPos = this.transform.position;
leftPos.x-=eyeSeparation;
rightPos.x+=eyeSeparation;
leftPos.y +=eyeYOffset;
Ray ray;
RaycastHit hit;
rightPos.y +=eyeYOffset;
GameObject go = null;
switch (ais) {
case AISensor.upEyeLeft:
ray = new Ray(leftPos,new Vector3(0,1,1));
hit = new RaycastHit();
Physics.Raycast(ray,out hit,maxEyeSensorLength);
if (hit.collider!=null) {go = hit.collider.gameObject;}
if (debugdraw){Debug.DrawRay(leftPos,new Vector3(0,1,1),Color.red,maxEyeSensorLength);}
break;
case AISensor.upEyeRight:
ray = new Ray(rightPos,new Vector3(0,1,1));
hit = new RaycastHit();
Physics.Raycast(ray,out hit,maxEyeSensorLength);
if (hit.collider!=null) {go = hit.collider.gameObject;}
if (debugdraw){Debug.DrawRay(rightPos,new Vector3(0,1,1),Color.red,maxEyeSensorLength);}
break;
case AISensor.eyeLeft:
ray = new Ray(leftPos,new Vector3(0,0,1));
hit = new RaycastHit();
Physics.Raycast(ray,out hit,maxEyeSensorLength);
if (hit.collider!=null) {go = hit.collider.gameObject;}
if (debugdraw){Debug.DrawRay(leftPos,new Vector3(0,0,1),Color.red,maxEyeSensorLength);}
break;
case AISensor.eyeRight:
ray = new Ray(rightPos,new Vector3(0,0,1));
hit = new RaycastHit();
Physics.Raycast(ray,out hit,maxEyeSensorLength);
if (hit.collider!=null) {go = hit.collider.gameObject;}
if (debugdraw){Debug.DrawRay(rightPos,new Vector3(0,0,1),Color.red,maxEyeSensorLength);}
break;
case AISensor.downEyeLeft:
ray = new Ray(leftPos,new Vector3(0,-1,1));
hit = new RaycastHit();
Physics.Raycast(ray,out hit,maxEyeSensorLength);
if (hit.collider!=null) {go = hit.collider.gameObject;}
if (debugdraw){Debug.DrawRay(leftPos,new Vector3(0,-1,1),Color.red,maxEyeSensorLength);}
break;
case AISensor.downEyeRight:
ray = new Ray(rightPos,new Vector3(0,-1,1));
hit = new RaycastHit();
Physics.Raycast(ray,out hit,maxEyeSensorLength);
if (hit.collider!=null) {go = hit.collider.gameObject;}
if (debugdraw){Debug.DrawRay(rightPos,new Vector3(0,-1,1),Color.red,maxEyeSensorLength);}
break;
case AISensor.bottom:
ray = new Ray(this.transform.position,new Vector3(0,-1,0));
hit = new RaycastHit();
Physics.Raycast(ray,out hit,maxEyeSensorLength);
if (hit.collider!=null) {go = hit.collider.gameObject;}
if (debugdraw){Debug.DrawRay(this.transform.position,new Vector3(0,-1,0),Color.red,maxTopBottomSensorLength);}
break;
case AISensor.top:
ray = new Ray(this.transform.position,new Vector3(0,1,0));
hit = new RaycastHit();
Physics.Raycast(ray,out hit,maxEyeSensorLength);
if (hit.collider!=null) {go = hit.collider.gameObject;}
if (debugdraw){Debug.DrawRay(this.transform.position,new Vector3(0,1,0),Color.red,maxTopBottomSensorLength);}
break;
}
return go;
}
I can’t really use this.transform.forward*something in place of something I’m supposed to use, because I need that specific setup of “rays”, because I can’t multiply vector by vector. The problem is that rays should go from object’s front (“aligned” to Z axis), no matter of object’s rotation. Unfortunately that’s not what is happening.
As for code’s purpose, this is auxiliary code for my AI (simple AI of koopa/goomba from super mario 64 type, but not blocking on walls, jumping low obstacles and avoiding holes) used for detection of various things, including the player.