Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Programatically generated mesh and collider do not register with raycast?

Discussion in 'Physics' started by fungus1487, Jul 30, 2020.

  1. fungus1487

    fungus1487

    Joined:
    Jan 25, 2014
    Posts:
    2
    Hi, I have a project that is generating meshes dynamically from an online api providing 2d polygons (I project these into world coords)

    I appear to have the mesh configured correctly (it does render correctly as shown in the image "mesh.png" below).

    mesh.png

    I create another game object as a child of the game object that has a mesh renderer on it and apply the mesh collider to it using the same mesh. Everything looks correct when I pause the editor (see collider.png below).

    collider.png

    The issue comes with Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, 10.0f), this has worked successfully for me with other objects dynamically created in the scene just not for dynamic poly's. It never registers a "hit", I have tried lifting the mesh collider game object above the camera too to see if it's backfacing but no cigar. I'm at a loss where to take this from here, hoping for a helping hand. Can provide more detail but wanted to avoid a code dump here as a first pass but here is how the mesh generation is taking place (omitting vertices calculation etc. assuming this is correct as of images.)

    Code (CSharp):
    1.  
    2. // create a mesh from the points
    3. var mesh = new Mesh();
    4. mesh.vertices = vertices;
    5. mesh.uv = uvs;
    6. mesh.triangles = triangulator.Triangulate();
    7. mesh.colors = colors;
    8.  
    9. mesh.RecalculateNormals();
    10. mesh.RecalculateBounds();
    11. mesh.Optimize();
    12.  
    13. // make a new game object, we will draw programatically
    14. var assetGameObject = new GameObject("Asset");
    15.  
    16. // make the filter including the mesh we made
    17. var filter = assetGameObject.AddComponent<MeshFilter>();
    18. filter.mesh = mesh;
    19.  
    20. // the way we render the poly
    21. var renderer = assetGameObject.AddComponent<MeshRenderer>();
    22. renderer.material = _roadMaterial;
    23. renderer.material.color = colour;
    24.  
    25. // then use the above mesh for the mesh collider so we can interact with it e.g. look at
    26. var colliderGameObject = new GameObject("AssetCollider");
    27. colliderGameObject.transform.parent = assetGameObject.transform;
    28.  
    29. // use the above mesh for the mesh collider so we can interact with it e.g. look at
    30. var collider = colliderGameObject.AddComponent<MeshCollider>();
    31. collider.sharedMesh = mesh;
    32. collider.convex = false;
    33. collider.enabled = true;
    34. collider.isTrigger = false;
    35.  
    36. assetGameObject.transform.parent = _stage; // add to stage
    37.  
     
  2. fungus1487

    fungus1487

    Joined:
    Jan 25, 2014
    Posts:
    2
    OK we found out what was wrong but unsure why unity behaves this way.

    it was to do with the Physics Raycast.

    Code (CSharp):
    1. Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, 10.0f)
    the component was attached to the FpsController game object and not it's child camera, it never took into account the y axis in the transform.Forward vector thus never saw the mesh below the cam
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,936
    Obviously physics isn't involved in creating arguments you pass to its functions. Sounds like you're saying you were using the wrong "forward" reference from the Transform system so not physics.

    As a side note, you need to be careful about using Transform references when using physics. If you use interpolation then the transform pose and the body pose won't always be the same due to interpolation. Something to watch out for.
     
unityunity