Search Unity

Raycast misses custom mesh collider

Discussion in 'DOTS Physics' started by jonwah00, Jan 16, 2020 at 5:16 PM.

  1. jonwah00


    Jul 21, 2019

    Firstly; I am new to game development, and DOTS/Physics, so I'm not sure if I'm even taking the right approach here, but I would appreciate any help anyone can give.

    I'm attempting to procedurally generate a 3D world which consists of hexagons wrapped into a cylinder. I've tried a few different approaches, but at the moment I'm doing it by generating a unique mesh for each hexagon cell, with the vertices at world positions, and 0 translation. Note, I did try the same with a 'single' mesh and translating/rotating each one correctly, but it gave me small render bugs where each hex met.

    Anyway; that's working, and now I'm attempting to raycast to touch each hex. I have created a mesh collider for each mesh, then do a Unity.Physics collision world CastRay to find the hit entity.

    It does work - but is inaccurate. I've verified the ray direction/length is correct (with Debug.DrawRay), so I suspect the collider mesh might be out of sync with the real mesh?

    If that image doesn't work, link:

    Here's the creation of the mesh collider (vertices, triangles are the verts/indices from each mesh created):

    Code (CSharp):
    2.                     using (var positions = new NativeArray<float3>(vertices, Allocator.Temp)) {
    3.                         using (var indices = new NativeArray<int>(triangles, Allocator.Temp)) {
    5.                             var collider = Unity.Physics.MeshCollider.Create(positions, indices);
    7.                             entityManager.SetComponentData(cell, new PhysicsCollider {
    8.                                 Value = collider
    9.                             });
    10.                         }
    11.                     }

    So; a couple of questions:

    What could be causing this?

    Is this even a remotely sane idea? I would like to be able to support a large map size; so there maybe hundreds of thousands of these meshes in the game - will this just be too much even with ECS/DOTS?

    I'm using a mesh collider as I don't think any of the built in colliders would work with hexagons (for detecting when the mouse is over that hex/click etc) - but am I wrong here? Could a sphere approximate it, and be a lot faster?
  2. steveeHavok


    Mar 19, 2019
    If each hexagon is the same size, then I would certainly only want to create a single shared MeshCollider that is shared across all the bodies. I even recommend using the CylinderCollider rather than the MeshCollider and setting the side count to 6.

    What size are the colliders and the world in general? Having the vertices of the mesh centered around the local origin and translating the PhysicsCollider would be preferable. If the colliders and world space gets big you could well be hitting floating point accuracy issues.

    Can you add the Physics Debug Display component and render the Collider Edges and maybe the Broadphase?


    You should see some red boxes representing the broadphase and green edge lines representing the objects physically representation. If these are drawn along with the green raycast it might help visualize the problem.
  3. jonwah00


    Jul 21, 2019
    Thanks very much for your reply!

    The colliders are each about 10 units radius - the world size can change as it's generated programmatically. Ideally I wanted it to be able to be quite large - thousands/tens of thousands of cells wide/high.

    I did try (and got working) the approach of having a single mesh used for each entity (I hadn't tried colliders at this point in time), and then translating and rotating each cell appropriately to form the world. This did work, but I ended up with small errors where the cells wouldn't quite align. I put it down to floating point accuracy and so abandoned this approach; but perhaps I should try again; with colliders this time.

    Thanks heaps for the pointer about Physics Debug Display component; I was trying to use the collider debugger but couldn't see anything coming up; I think that will help! I'll post again when I've had time to try it out!

    Also thanks for the tip about using a cylinder collider with 6 sides, that sounds like it would be a lot more performant than a mesh collider!
  4. jonwah00


    Jul 21, 2019
    Ok so I can't quite figure out how to get the physics debug display attached to the entity.

    I've tried instantiating it for each entity which has a mesh + collider as below:

    Code (CSharp):
    1. entityManager.SetComponentData(cell, new PhysicsDebugDisplayData {
    2.     DrawColliderEdges = 1
    3. });
    I've tried with just DrawColliders, DrawColliderEdges, and both; but nothing shows up in-game :(

    I've got the Physics Debugger active through Window -> Analysers -> Physics Debugger; everything looks correct; but nothing shows up on the scene/game views..