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.