- Home /
Raycast without Colliders!?
Hi Folks,
Does anyone know of alternatives to Unity's existing system for Raycasts? I'm looking for one that would seek out MeshRenderer components instead of Colliders.
Reason why is that we want a raycast to effect stage geometry, but the way our stages are set up features colliders contained in totally different parents than the actual art assets for the stage.
Thanks,
Kevin
Answer by Baste · Nov 13, 2015 at 01:49 PM
We're doing this in some instances by adding the mesh colliders to the mesh renderers, doing the raycasts, and removing the mesh colliders again.
You'll have to notify the physics system that the collider has been created, though. Usually you'd do that by setting the object dirty, but if that's not wanted, you can just wiggle the transform. Here's a snippet from how I solve this issue:
MeshCollider tempCollider = gameObject.AddComponent<MeshCollider>();
transform.position += Vector3.one;
transform.position -= Vector3.one;
//RAYCAST
DestroyImmediate(tempCollider);
Use layers and layermasks to avoid hitting the level colliders you're using. You can also raycast directly against a single collider with the Collider.Raycast method.
Your answer
Follow this Question
Related Questions
Help with a field of sight 3 Answers
Custom Collision Detection 4 Answers
Collision Detection If Raycast Source Is inside A Collider? 4 Answers
Camera raycast? 2 Answers