- Home /
Physics.Raycast not hitting anything
For some reason my Physics.Raycast isn't hitting anything:
Vector3 endPoint = Vector3.zero;
for (int e = 0; e < mesh.vertices.Length; e++)
{
Vector3 StartPoint = mesh.normals[e] * (Radius + MaxHeight + 10.0f);
RaycastHit hit;
if (Physics.Raycast(endPoint, StartPoint, out hit))
{
Debug.DrawLine(StartPoint, hit.point, Color.yellow);
Debug.DrawLine(hit.point, endPoint, Color.red);
colors[e] = new Color(0.5f, 0.5f, 0.5f, 1.0f);
}
else
{
Debug.DrawLine(StartPoint, endPoint, Color.green, 5f);
}
}
I have tried with both a MeshCollider and a spherecollider and none of the rays turn either yellow or red, they remain green. Am I using it wrong?
EDIT: It seems that it doesn't hit because there isn't any collider, even though I have a meshcollider attached to it. I update my meshcollider like so:
mesh.Clear();
mesh.vertices = vert;
mesh.normals = norm;
mesh.triangles = tri;
mesh.uv = uv;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mc0.sharedMesh = null; // nullify the previous sharedMesh
mc0.sharedMesh = mesh;
mc0.enabled = false;
mc0.enabled = true;
ns$$anonymous$$d of using Deb rays, try inserting Debug.Log lines to write the collider, if any, that was hit to console.
Okay, I found out that it hits 4/1536 points, which is nothing when the goal is for all of the points to hit.
EDIT: nvm, it was hitting a small capsule collider, which it wasn't supposed to hit.
Answer by MakakWasTaken · Apr 22, 2016 at 08:18 PM
I actually figured it out, thanks to this post: http://answers.unity3d.com/answers/1153623/view.html