- Home /
RaycastAll will not detect any colliders
Hi I'm trying to use RaycastAll to determine if a line renderer should be able to draw a line between two colliders. The problem is that RaycastAll will not detect anything.
Here is a slightly simplified version of the code I'm using to determine if a linerenderer should draw the line between colliders:
public bool IsValidPlacement() {
Vector3 dir = end.position - start.position;
dir = dir.normalized;
Debug.DrawLine(start.position, end.position, Color.black);
float dist = Vector3.Distance(start.position, end.position);
bool isValid = true;
Ray ray = new Ray(start.position, dir);
int i = 0;
RaycastHit[] hits = Physics.RaycastAll(ray, dist);
print("Found " + hits.Length.ToString() + " hits from start to end.");
foreach (RaycastHit hit in hits) {
Debug.DrawLine(start.position, hit.point, Color.Lerp(Color.green, Color.blue, (float)i/hits.Length));
i++;
string tag = hit.transform.tag;
if (!tag.Contains("Component Input")) {
isValid = false;
} else {
Transform wl = hit.transform.Find("wire location");
if (wl != start && wl != end) {
isValid = false;
}
}
}
}
return isValid;
}
Where start, end
are of type Transform and defined as members.
The problem can be seen in the below picture. The black ends of the rectangular prisms are potential start and end points for the LineRenderer. In this example the black line in the center is being generated by the first Debug.DrawLine in my method. And you can see it is actually going all the way through the prism on the left because the start point is the left most black tip, this black line is going through 3 colliders: the furthest left black end of the prism, the center pink part which has it's own collider, and the right side of the prism.
However I'm still getting "Found 0 hits from start to end" despite using the same start position and distance for RaycastAll that I use to draw the debug line. Where it should find 3 hits.
You can see in the below picture that the prefab is indeed made up of 3 colliders.
I am at a complete loss as to why this is happening, and thus feel like I'm missing something basic about RaycastAll. Any help is very appreciated.
Your answer
Follow this Question
Related Questions
ScreenPointToRay ray not coming through from camera 0 Answers
Linecast questions. 1 Answer
Multiple objects with OnMouseDown 2 Answers