Mesh collider not working after rotation

Avatar
  • updated
  • Under Review

I would like to create a polygon on a GameObject at runtime.

I do the following at runtime:

1. Create GameObject (GO)

2. Add polygon component to GO

3. Define points with .AddPoints --> Creates polygon on the XY plane of the GO

4. Add a mesh collider

5. Rotate the GO so the polygon is pointing up (Y world). Because I need it on the XY world plane

6. Trying to raycast on the polygon doesn't work. The meshcollider gets a hit only in the original position (GO euler angles = (0,0,0)) and not when rotated ((90,0,0)).

I tried to call the UpdateMesh(true) after step 3, but it doesn't work.

I tried to define a GO (not at runtime), adding a meshcollider and it still doesn't work. It works only with a boxcollider.


I am not sure it's a bug or I am missing something with the colliders and how the mesh is defined in a polygon.


Is this an expected behaviour?

Thanks

Reporting a bug? please specify Unity version:
2019.4.9f1
Reporting a bug? please specify Shapes version:
4.1.0
Reporting a bug? please specify Render Pipeline:
Built-in render pipeline
Avatar
Freya Holmér creator
  • Under Review

there's nothing special about what happens when rotating a mesh - it sounds like you might simply always be getting the raycast hit location in local space? in which case you'll have to transform it back

it shouldn't be a problem if you're using Physics.Raycast, but you might have to transform the coordinate with the way you're doing it now?

one way to check would be to test with another mesh collider using a different mesh

Avatar
Mitch753

After some more testing I think I better identified the problem: If I create a GO in the editor and then add a "polygon" and then add a mesh collider, the field of the mesh collider "Mesh" is null and there is no way of assigning it manually, since the "polygon" component doesn't expose a mesh in the editor! For this reason I wasn't getting a hit from the raycast. So it has nothing to do with the rotation. There is a workaround for adding a mesh collider not at runtime?


When doing it procedurally everything works fine. Initially I made a mistake because the "polygon" by default render the 2 sides and I was raycasting from the negative side, and when I wasn't applying the rotation the positive face was facing the camera, so the raycast worked fine, sorry about that.

Anyways I just wanted to thank you for the quick support!