- Home /
Need some help with Physics.Raycast hit
In the Unity UI, I created a new layer (index=10) and assigned it to a GameObject with a Mesh Collider. In my code, I am doing drawing a ray from the player to the enemy and rendering it for Debug purposes. I tried this in both Update and FixedUpdate. This seems to work okay, generally, but there are cases where the raycast hit is saying that there is a null collider when clearly I can see the ray passing through the collider.
This can be seen in the attached image where the aqua color is the collider mesh (rendering on) and the blue ray represents the cast. The code is below as well. It's driving me crazy! What can I do to remedy this? Thanks.
![var d1 = Vector3.Distance(v1, v2);
var ray = new Ray(v1, v2);
RaycastHit hit;
Physics.Raycast(ray, out hit, d1, 1 << 10);
Debug.DrawLine(v1, v2, new Color(0, 0, 255));
if (Vector3.Distance(player.transform.position, transform.position) <= ChaseRadius && hit.collider == null)][1]
[1]: /storage/temp/41796-capture.png
I don't see the mesh collider being gizmo drawn, so check if the mesh is assigned to it.
Have you tried replacing the mesh collider with a box collider and seeing if it works then (this is better performance wise anyway)?
Thanks - The collider is definitely there. I believe the issue is the rendering is drawing over top of the other gizmo. If I uncheck the collider at runtime, the enemy immediately comes running for me. Otherwise it's intermittent.
I did not try a box collider. Although it would work here there are obvious other spots in the level where this wouldn't work.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to correctly save a screenshot in a custom path? 1 Answer
Receiving UDP data in Unity? 0 Answers