Raycasting to a GameObject's Bounds

I'm trying to perform 3 raycasts against a gameobject; one dead center, one to the left and one to the right, all at the same vertical position. I'm making use of WorldToScreenPoint in the Camera class and the Bounds struct from the Renderer class that is attached to the gameobject.

For objects aligned to the x-axis, I perform these calculations:

var targetPosition : Vector3 = targetObjectTransform.position;
var targetRenderer : Renderer = targetObjectTransform.gameObject.renderer;
var targetBounds : Bounds = targetRenderer.bounds;
var targetLeftPosition : Vector3 = targetPosition;
var xLeftDiff = targetPosition.x - targetBounds.min.x;
targetLeftPosition.x -= xLeftDiff;
var targetLeftScreenPosition : Vector3 = Cam.WorldToScreenPoint(targetLeftPosition);
var ray : Ray = Cam.ScreenPointToRay(Vector3(targetLeftScreenPosition.x,targetLeftScreenPosition.y,0));

This gives me the leftmost bound (blue ray in screenshot). I reverse the calculations for the rightmost bound (green ray), and just use targetPosition for the middle bound (yellow ray).

The problem is that I can't work out how to do this for objects along the z-axis. My existing code works from the x-axis and therefore I get very little ray spread. I've tried changing the z parameter in CamScreenPointToRay but this makes no difference (I imagine in that function it refers to depth). Changing the x parameter of CamScreenPointToRay from targetLeftScreenPosition.x to z doesn't appear to fix the problem either.

I get the feeling I'm being very stupid but I just can't figure it out. Any ideas? (Also open to a better suggestion for raycasting to the bounds of an object) I've attached a screenshot to illustrate. Wall 1 is the problem wall, aligned along the z-axis. Wall 2 works fine.

alt text

I think the reason you haven't gotten an answer is that your description is somewhat general. You should consider being more specific. What exactly are you trying to do? Raycasting to the bounds of an object, but to what end?

Bounds are axis-aligned bounding boxes (AABB) representing the maximum size of the object along a world axis. If you were to rotate the object, the AABB will not rotate, but will rescale to represent the new distribution of the geometry in the space.

To cast to a different axis, you would merely use that axis of the min and then add it to that axis of the center to which you are casting.

Your current code raycasts to the x bounds always and in a fairly convoluted way. You could accomplish essentially the same thing with:

var targetObjectTransform : Transform;
var Cam : Camera; //?? Not sure where you got Cam from...

function Update() {
    var targetBounds : Bounds = targetObjectTransform.renderer.bounds;
    var extents : Vector3 = Vector3(targetBounds.extents.x,0.0f,0.0f);
    var center : Vector3 = targetBounds.center;
    var start : Vector3 = Cam.transform.position;
    Debug.DrawRay(start,(center-start),Color.yellow);
    Debug.DrawRay(start,((center-extents)-start),Color.blue);
    Debug.DrawRay(start,((center+extents)-start),Color.green);
}

This will always point to the x. If you want the widest axis, you would simply get that:

var targetObjectTransform : Transform;
var Cam : Camera; //?? Not sure where you got Cam from...

function Update() {
    var targetBounds : Bounds = targetObjectTransform.renderer.bounds;
    var extents : Vector3 = targetBounds.extents;
    //Max extents
    if(extents.x > extents.y) {
        extents.y = 0.0f;
        if(extents.x > extents.z) extents.z = 0.0f;
        else extents.x = 0.0f;
    }
    else {
        extents.x = 0.0f;
        if(extents.y > extents.z) extents.z = 0.0f;
        else extents.y = 0.0f;
    }
    var center : Vector3 = targetBounds.center;
    var start : Vector3 = Cam.transform.position;
    Debug.DrawRay(start,(center-start),Color.yellow);
    Debug.DrawRay(start,((center-extents)-start),Color.blue);
    Debug.DrawRay(start,((center+extents)-start),Color.green);
}

If you want the axis most perpendicular to the camera, then you would check the camera's right axis to see which world axis it most coincides with and pick that axis's extents to use, much like the code above.