Raycast help: casting 3 rays, out of the faces of a cube

Hey guys, I have a cube that I’m casting 3 rays from.
A graphical representation would be best.

(I don’t know what’s up with the uploading, but there’s no cuts in the vertical ray, they’re all straight lines, at least that’s what I see in my screen shot…)

Anyway, now those are drawn via Debug.DrawRay - Here’s how I’m doing it:

public enum Direction { Up, Right, Forward }
Direction[] dirs = { Direction.Forward, Direction.Right, Direction.Up };
int index = 0;

void Update()
{
	index = (index + 1) % dirs.Length;
	UpdateEndAndStartPoints(dirs[index]);
	Debug.DrawRay(start, -mDir * length * 2, Color.blue);
}

void UpdateEndAndStartPoints(Direction dir)
{
	switch (dir) {
		case Direction.Forward:
			mDir = mTransform.forward;
			break;
		case Direction.Right:
			mDir = mTransform.right;
			break;
		case Direction.Up:
			mDir = mTransform.up;
			break;
	}
	start = mTransform.position + mDir * length;
	end = mTransform.position - mDir * length; // I'm not using end anywhere for the moment, previously I used it to identify the 2nd point of my line renderer
}

Now what I’m trying to do, is to cast real rays, instead of just debug ones, so that if a face of my cube hits a floor or something, I could detect that. So I thought I could just add:

void Update()
{
	index = (index + 1) % dirs.Length;
	UpdateEndAndStartPoints(dirs[index]);
	Debug.DrawRay(start, -mDir * length * 2, Color.blue);

	RaycastHit hit;
	if (Physics.Raycast(start, -mDir, out hit, length * 2)) {
		if (hit.collider.tag == "Floor")
			print("I: " + this + " just hit: " + hit.collider.name);
	}
}

Yes, I didn’t forget to give my cube a “Ignore Raycast” layer, since Physics.Raycast detect one object, if I don’t do that I’d have to go for Physics.RaycastAll.

Now, it’s not working right, in some faces, the ray that’s ‘supposedly’ coming out of it, isn’t seem to hitting the floor, although the visual one is. Although I have the same start, direction and length as in my Debug.DrawRay method.

If I rotate the cube, other faces hit the ground, and gives output, while others don’t.

I’m pretty sure it’s something related to the way I’m casting the rays, but shouldn’t it work, since I’m casting it just like I’m doing in my Debug.DrawRay?

What am I missing?

Thanks for any help.

As a start, just inside your your Raycast() put:

Debug.Log(hit.collider.name + ", " + hit.collider.tag);

This will tell if your raycast is hitting something you don’t expect.

I didn’t forget to give my cube a
“Ignore Raycast” layer, since
Physics.Raycast detect one object

Meshes are one sided. If you Raycast() from inside the cube for example, you will not hit the cube. While I don’t think it is related to your problem, it appears you are moving your Raycast to the outside face of the cube. You don’t have to do that. You can just cast from the center point. In fact moving to the face can cause you some problems since objects (with default Unity settings) are allowed to interpenetrate. This would mean that it is possible for a cube that is face down on a surface to not detect the hit.

You need 6 rays for all your sides. Extending it to start on the other side of your cube won’t work if your floor is a plane, as the collision mesh for a plane is single sided. Also, because your directions are negative, your forward ray is back, your up is down and your left is right. If you are using some sort of volume for your floor collider like a box, then it might work if your collider is thin enough. I wouldn’t suggest using a box collider though. Probably better to add 3 extra rays.