Raycasting epic fail :(

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.

If you want a ray from the transform’s position and forward, that’s new Ray(transform.position, transform.forward). Remember that the second argument of the Ray is the direction, not where you’re shooting to.

If you want to shoot in the direction (0, 1, 1) relative to the transform, that’s:

transform.TransformDirection(new Vector3(0, 1, 1))

TransformDirection transforms a direction from local to world space

Thanks, that’s what I was looking for. One thing though, I need to cast few rays at the offset from transform.position (as evidenced by parts of code using leftPos, rightPos, eyeYOffset). How do I solve that part?

The first argument in the Ray is where you shoot from. So you just use TransformDirection (or a combination of transform.up and transform.right) when you’re calculating those positions.

So instead of

leftPos.x-=eyeSeparation;

You’d do

leftPos -= eyeSeparation * transform.right;