- Home /
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.
I had the same problem once,Debug.DrawRay() and Physics.RayCast() behaves differently.
Initially my code wuz like this
if (Physics.Raycast(transform.position,Ray_Direction,out Ray_Hit))
{
//Get the ray hitting point
Ground_Pos=Ray_Hit.point;
//Draw the ray which is casted by the raycaster
Debug.DrawRay(transform.position,Ground_Pos,Color.green);
}
This give me wierd results.The ray-casting is fine and ground is detecting but the ray ending is drawing somewhere else.
Then I changed my code to like this drawing the ray properly and ray-casting works fine.
if (Physics.Raycast(transform.position,Ray_Direction,out Ray_Hit))
{
//Get the ray hitting point
Ground_Pos=Ray_Hit.point;
//Draw the ray which is casted by the raycaster
Debug.DrawRay(transform.position,Ray_Direction*Ray_Hit.distance,Color.green);
}
Answer by robertbu · Oct 28, 2013 at 05:51 PM
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.
$$anonymous$$eshes are one sided
That's it all it took to say for me to know my problem! The problem was, since I'm casting say, my up ray, (which goes from the bottom of my cube, intersects it and go up) if I now place my cube naturally on the plane it won't detect the hit, just like you said cause the ray is starting from underneath the cube going up, thus going through the outer-side of the plane, not the inner.
Using a cube as a floor, just like @EvilWarren said lets me get away with it, and using only 3 rays.
I didn't wanna use 6 rays and cast from the center, cause I thought I'd get away with 3 ins$$anonymous$$d. But I just discovered that for my purposes, for what I'm trying to do, it's better to use 6 anyway.
Thank you and @EvilWarren for your answers.
Answer by EvilWarren · Oct 28, 2013 at 06:47 PM
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.
Also, I would follow robertu's advice and start the rays from the centre of the cube.
Your answer