Export game-mesh with custom collision for UnityProblem
Although Unity has a number of way to add collision to an environment using Unity Editor, placing hundreds or thousands of such objects can be tedious and time-consuming for large levels, or projects with a lot of model assets in use. To alleviate this, rather than using the editors inbuilt systems, collision can be included alongside models when they are exported from Blender (Max, Maya or other 3D software). - For learn more about collision read "Collision meshes & game models".How to add collision to mesh
The collision mesh is basically a simplified version of the 'game object', the item visible to the player in-game. Generally speaking the collision mesh will be constructed from 'primitive' shapes, usually cubes and other solid volume shapes in differing configuration, be 'closed' or 'sealed'
(must not have any open faces or gaps), and be convex. For accuracy both collision and visible game-mesh should similarly position in 3D space, and use the same Origin point (shown below separated for clarity).
Shown below for example, a simple kitchen chair (right) has a very simple collision mesh (left) made from to cubic volumes; one encompasses the main body of the chair - seat and legs, the other covers the back. Both shapes are sealed so the bottom of the back, and the top of the body are coincidental or co-planar - they sit on the plain, ensuring there are no gaps between. The two shapes are also aligned and sit flush on all sides except for the 'seat' allowance (for a character to 'sit' in the chair). Both meshes also use the exact same Origin point (important).Exporting collision with game-mesh in Blender
Exporting a game-mesh with collision included is relatively straightforward, simply "Shift+RMB
" multi-select each mesh to be exported as normal and from the "File
" menu select "Export » FBX (.fbx)
". In the "File Browser
" that appears select the options to the lower-left - typically only "Selected Objects
" is necessary when working with a lot of content in the same file (else everything is exported) - and then click "Export FBX
" top-right.Setting Collision up in Unity
In Unity import the mesh as a typical asset WITHOUT
setting Unity to auto generate collision data. Once the model is available drop an instance into the Editor (main viewport) and in the "Hierarchy
" select the mesh corresponding to the collision hull () and left-click select it. In the "Inspector
" click the "Add Component
" button () bottom of the panel and select "Physics
" then "Mesh Collider
". A set of properties will appear in the "Inspector
" headed "Mesh Collider
" (). Make sure its activated (click the checkbox
to the left of the heading). Finally still in the "Inspector
", disable "Mesh Renderer
" for the collision object, so its not drawn on screen and visible to the player, by clicking the checkbox to the left of the heading clearing the tick/check mark (). If both properties are set up correctly the object will appear as a pale-green outline in the view, as an object disappearing when deselected.Issues to watch out for
A few export notes to be aware of;
- prior to exporting rotate the meshes 90 degree on the "X" axis
and apply before uprighting and exporting to ensure they are orientated correctly in Unity (which uses a differ axis orientation - models imported from Blender are often flipped on their back), although this can be fixed to a certain degree setting the appropriate export option it's not always reliable, especially if the objects have been rotated around the "Z" axis before exporting.
- even though the mesh may import with a collision element, the mechanics of the function still have to be added manually to each instance of the object. To avoid or mitigate this, place an initial instance in the Editor, set the collision mesh properties and then duplicate that object rather than dragging a new copy into place - this duplicates the object and its properties saving the need to add collision.
- there isn't a specific naming convention to follow for Unity as collision is processed based on selection rather than flagged automation. For practicalities however, it's recommended a prepend be used to name each object for clarity, e.g. "c_
" or similar - in Blender select the collision mesh and click "Object
" Properties, type a new name in the DataBlock ID
field at the top of the panel.
- when selecting the objects for export from Blender, selection order is not specifically important except for sake of consistency; the active Object becomes the primary element in Unity so selecting the game-mesh last per export instance means that particular element always taking priority in Unity Editor panels (Materials, Hierarchy etc.).
- although not specifically necessary to work in Unity, for purposes of exporting it may be necessary to unwrapped the collision mesh, assign a basic Material and Texture to ensure error-free export (some error may not be picked up in the absence of Materials etc.).
- depending upon the version of Blender and FBX export script used, prior to output objects may need to be moved so their respective Origin points are located at grid centre in Blender ("0,0,0"). This avoids issues with FBX exporting meshes 'in place', i.e. where they are situated whilst being worked on, which tends to result in the final export being off-centre.