- Home /
How can I raycast a mesh collider baked with LineRenderer.BakeMesh() ? Ty
I have two classes, one for raycasting and one for baking the mesh, here they are:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100f))
{
print(hit.transform.gameObject.name);
if (hit.transform != null)
{
print(hit.transform.gameObject.name);
}
}
}
}
and second this:
void Start () {
LineRenderer lineRenderer = gameObject.GetComponent<LineRenderer>();
MeshCollider meshCollider = gameObject.AddComponent<MeshCollider>();
Mesh mesh = new Mesh();
lineRenderer.BakeMesh(mesh, Camera.main, true);
meshCollider.sharedMesh = mesh;
}
At runtime the mesh does appear and I can confirm that it takes the shape that i want, the one of the line rendered, the problem is that while the raycast class works fine with all other colliders it isnt working with the one generated this way. A couple of users used this code to bake their mesh, the code as I said seems to work fine and is nearly identical to that shown on the documentation for meshCollider.sharedMesh, still they didnt appear to use it for raycasting. Some other user said it may be related to which side is the mesh facing but I'm pretty sure that's what the Camera.main is there for.
Hope you can help me understand the issue, thanks
Answer by lgarczyn · Jan 06, 2020 at 08:10 PM
Unfortunately, Raycast only works on concave mesh, but your line renderer is surely convex.
You could try to use multiple box colliders forming a chain along the line
If your line is flat, you could have a BoxCollider containing the entire line, then use the collision point to compare it to every quad, and check if it is inside.
If your line is flat, you could try to convert it to a 2d polygon collider, and use its API to check if the previous point falls inside it
Ty, for the explanation and the suggestions, I'll try them out :)
I'm having the same issue but your answer seems to be opposite of what I'm experiencing. If I set the mesh collider to be convex the ray cast will hit it, but if it's not set to convex (making it concave) the ray cast will not hit it. But I can't set it to convex because then instead of a mesh following the line I just get a large rectangle which will not work for my purposes.
I've deter$$anonymous$$ed that a concave mesh renderer will in fact received ray casts, however, the mesh baked by the line renderer has a 'front' and 'back'. If the ray hits it from behind it will pass through without registering. This is my issue. For some reason the line renderer is baking a mesh facing directly away from my camera (where the ray is co$$anonymous$$g from with a mouse click). I know because if I turn the object around then the ray cast hits.
Your answer
Follow this Question
Related Questions
RaycastHit Triangle Index Problem 0 Answers
Simple wire/cable 1 Answer
How to scan a surface of a GameObject and get the distance of every edge to camera 1 Answer
RayCast Line Not Rotating with Gun? 0 Answers
Fill area defined by a contour 1 Answer