Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple Collider Assigning

Discussion in 'Scripting' started by o0_ICE_0o, Nov 7, 2016.

  1. o0_ICE_0o

    o0_ICE_0o

    Joined:
    Apr 3, 2014
    Posts:
    21
    Ok so i made a pretty complex object and Using primitive colliders are going to be very time consuming even if i break down the complex model into easier shapes.
    But i can use blender to create the basic colliders i want and then import the whole lot into unity using a .fbx with submeshes.

    So i made a simple script that i can use to get the submeshes and simple drop them where i want and thats where i hit a bump, how can i tell unity to use a script to get the submeshes in a .fbx/obj at a specified location and assign them as a collider?

    This is the code i have so far.
    Code (CSharp):
    1. Private Mesh RootMesh;
    2.  
    3. void StartFortressColliderAssigner(){
    4. gameObject.AddComponent<MeshCollider> ().sharedMesh = RootMesh;
    5.        
    6. }
    I know i must use a for loop or for each and iterate till the end of the files list of submeshes and assign each one as a collider, but im not exactly sure how to do that.
    Thanks :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    You've almost got this right, but there are a few details still to iron out:

    What you need is NOT submeshes. Submeshes in Unity are actually collected triangles that refer to only a subset of vertices in a single Mesh.

    For instance you could have triangle list 0 and triangle list 1 which are two separate submeshes of a single mesh object. They might share vertices, they might not, but in either case their numeric indices refer to the single list of vertices in a mesh.

    The "single mesh view" of this is what the MeshCollider uses. It is the collected set of all triangles of all submeshes, which is accessible with the .triangles property, NOT the .GetTriangles() method.

    See: https://docs.unity3d.com/ScriptReference/Mesh.html

    That is why a MeshCollider accepts a reference to the entire Mesh, not just a submesh within it.

    SO... to solve your problem: you need to create multiple Blender Objects, each which when imported by Unity produces a separate mesh, and then you can put a reference to each of those smaller meshes into each of the MeshColliders in your scene/prefab.

    Now I have used Blender extensively, but I have almost never exported manually to FBX. Instead I use the built-in import step in Unity which recognizes a .blend file, fires up Blender silently at import time, creates a transient FBX I never need to see, and imports the mesh.

    From this I know that a single Blender mesh object will import to a single Unity Mesh at import time. If you use multiple materials on a single Blender mesh, then it will import as a single Unity Mesh with multiple submeshes.

    If you have your object as one big thing in Blender3D, you can select parts of it and "Part" it out with the p command in Blender, which will break it off and make a new mesh object. Lather rinse repeat until you have decomposed it all.

    When I use this workflow, I still keep ALL the objects in a single Blender file, and then the importer adds an extra "root" object that they all parent up to at import time.
     
    florianhanke and o0_ICE_0o like this.
  3. o0_ICE_0o

    o0_ICE_0o

    Joined:
    Apr 3, 2014
    Posts:
    21
    So if i understand right, I have to take the triangles present in the .fbx/obj/blend and assign it to the collider?

    Code (CSharp):
    1. Private Mesh RootMesh;
    2. // loop
    3. gameobject.AddComponent<MeshCollider>().sharedMesh = RootMesh.triangles;
    4. //End loop after file = file end of file
    ^Something like that inside the for loop?
    Hmm probably im probably missing some things here, this doesnt just click in, must take a look at documentation.
     
    Last edited: Nov 8, 2016
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    When you import an FBX/Blender file, Unity produces a bunch of assets: Mesh, Material, Textures, Animation Clips, etc. depending on what was originally in it.

    You want to assign each Mesh asset to the correct MeshCollider component.

    Try this: make a plane primitive and inspect it in the editor. Note this:

    - it has a MeshFilter
    - that MeshFilter points at a pre-made Plane Mesh
    - it has a MeshRenderer that implicitly renders the MeshFilter's Mesh
    - it has a MeshCollider
    - that MeshCollider happens to point to the same pre-made plane Mesh.

    Now try and set the Mesh for the MeshCollider to be (for example) the Capsule Mesh.

    Now you have a visible plane with an invisible capsule-shaped Collider.

    That should help you understand how the process works in a more general sense.
     
    florianhanke likes this.