Home > Community > Community > Document: World Making Tutorial worth sharing (colliders)
24 Comments - 67831 Views
From Unity, not my own
Submitted By PanzerAce on 12/06/07
Mars Explorer, PanzerAce, Community 

Mesh Collider

The Mesh Collider takes a Mesh Asset and builds its Collider based on that mesh. It is far more accurate for collision detection than using primitives for complicated meshes. Mesh Colliders that are marked as Convex can collide with other Mesh Colliders.

 

A Mesh Collider used on level geometry
 

Properties

   
Material Reference to the Physic Material that determines how this Collider interacts with others.
Is Trigger If enabled, this Collider is used for triggering events, and is ignored by the physics engine.
Mesh Reference to the Mesh to use for collisions.
Smooth Sphere Collisions When this is enabled, collision mesh normals are smoothed. You should enable this on smooth surfaces eg. rolling terrain without hard edges to make sphere rolling smoother.
Convex If enabled, this Mesh Collider will collide with other Mesh Colliders. Convex Mesh Colliders are limited to 255 triangles.
 

Details

The Mesh Collider builds its collision representation from the Mesh attached to the GameObject, and reads the properties of the attached Transform to set its position and scale correctly.

Collision meshes use backface culling. If an object collides with a mesh that will be backface culled graphically it will also not collide with it physically.

There are some limitations when using the Mesh Collider. Usually, two Mesh Colliders cannot collide with each other. All Mesh Colliders can collide with any primitive Collider. If your mesh is marked as Convex, then it can collide with other Mesh Colliders.

Colliders work with Rigidbodies to bring physics in Unity to life. Whereas Rigidbodies allow objects to be controlled by physics, Colliders allow objects to collide with each other. Colliders must be added to objects independently of Rigidbodies. A Collider does not necessarily need a Rigidbody attached, but a Rigidbody must be attached in order for the object to move as a result of collisions.

When a collision between two Colliders occurs and if at least one of them has a Rigidbody attached, three collision messages are sent out to the objects attached to them. These events can be handled in scripting, and allow you to create unique behaviors with or without making use of the built-in NVIDIA PhysX engine.

 

Triggers

An alternative way of using Colliders is to mark them as a Trigger, just check the IsTrigger property checkbox in the Inspector. Triggers are effectively ignored by the physics engine, and have a unique set of three trigger messages that are sent out when a collision with a Trigger occurs. Triggers are useful for triggering other events in your game, like cutscenes, automatic door opening, displaying tutorial messages, etc. Use your imagination!

Be aware that in order for two Triggers to send out trigger events when they collide, one of them must include a Rigidbody as well. For a Trigger to collide with a normal Collider, one of them must have a Rigidbody attached. For a detailed chart of different types of collisions, see the collision action matrix in the Advanced section below.

 

Friction and bouncyness

Friction, bouncyness and softness are defined in the Physic Material. The Standard Assets contain the most common physics materials. To use one of them click on the Physic Material drop-down and select one, eg. Ice. You can also create your own physics materials and tweak all friction values.

 

Hints

  • Mesh Colliders cannot collide with each other unless they are marked as Convex. Therefore, they are most useful for background objects like environment geometry.
  • Convex Mesh Colliders must be fewer than 255 triangles.
  • Primitive Colliders are less costly for objects under physics control.
  • When you attach a Mesh Collider to a GameObject, its Mesh property will default to the mesh being rendered. You can change that by assigning a different Mesh.
  • To add multiple Colliders for an object, create child GameObjects and attach a Collider to each one. This allows each Collider to be manipulated independently.
  • You can look at the gizmos in the Scene View to see how the Collider is being calculated on your object.
  • Colliders do their best to match the scale of an object. If you have a non-uniform scale (a scale which is different in each direction), only the Mesh Collider can match completely.
  • If you are moving an object through its Transform component but you want to receive Collision/Trigger messages, you must attach a Rigidbody to the object that is moving.
 

Advanced

 

Collider combinations

There are numerous different combinations of collisions that can happen in Unity. Each game is unique, and different combinations may work better for different types of games. If you're using physics in your game, it will be very helpful to understand the different basic Collider types, their common uses, and how they interact with other types of objects.

 

Static Collider

These are GameObjects that do not have a Rigidbody attached, but do have a Collider attached. These objects should remain still, or move very little. These work great for your environment geometry. They will not move if a Rigidbody collides with them.

 

Rigidbody Collider

These GameObjects contain both a Rigidbody and a Collider. They are completely affected by the physics engine through scripted forces and collisions. They might collide with a GameObject that only contains a Collider. These will likely be your primary type of Collider in games that use physics.

 

Kinematic Rigidbody Collider

This GameObject contains a Collider and a Rigidbody which is marked IsKinematic. To move this GameObject, you modify its Transform Component, rather than applying forces. They're similar to Static Colliders but will work better when you want to move the Collider around frequently. There are some other specialized scenarios for using this GameObject.

This object can be used for circumstances in which you would normally want a Static Collider to send a trigger event. Since a Trigger must have a Rigidbody attached, you should add a Rigidbody, then enable IsKinematic. This will prevent your Object from moving from physics influence, and allow you to receive trigger events when you want to.

Kinematic Rigidbodies can easily be turned on and off. This is great for creating ragdolls, when you normally want a character to follow an animation, then turn into a ragdoll when a collision occurs, prompted by an explosion or anything else you choose. When this happens, simply turn all your Kinematic Rigidbodies into normal Rigidbodies through scripting.

If you have Rigidbodies come to rest so they are not moving for some time, they will "fall asleep". That is, they will not be calculated during the physics update since they are not going anywhere. If you move a Kinematic Rigidbody out from underneath normal Rigidbodies that are at rest on top of it, the sleeping Rigidbodies will "wake up" and be correctly calculated again in the physics update. So if you have a lot of Static Colliders that you want to move around and have different object fall on them correctly, use Kinematic Rigidbody Colliders.

 

Collision action matrix

Depending on the configurations of the two colliding Objects, a number of different actions can occur. The chart below outlines what you can expect from two colliding Objects, based on the components that are attached to them. Some of the combinations only cause one of the two Objects to be affected by the collision, so keep the standard rule in mind - physics will not be applied to objects that do not have Rigidbodies attached.

 
Collision detection occurs and messages are sent upon collision
  Static Collider Rigidbody Collider Kinematic 
Rigidbody Collider
Static 
Trigger Collider
Rigidbody 
Trigger Collider
Kinematic Rigidbody 
Trigger Collider
Static Collider   Y        
Rigidbody Collider Y Y Y      
Kinematic Rigidbody Collider   Y        
Static Trigger Collider            
Rigidbody Trigger Collider            
Kinematic Rigidbody Trigger Collider            
 
Trigger messages are sent upon collision
  Static Collider Rigidbody Collider Kinematic 
Rigidbody Collider
Static 
Trigger Collider
Rigidbody 
Trigger Collider
Kinematic Rigidbody 
Trigger Collider
Static Collider         Y Y
Rigidbody Collider       Y Y Y
Kinematic Rigidbody Collider       Y Y Y
Static Trigger Collider   Y Y   Y Y
Rigidbody Trigger Collider Y Y Y Y Y Y
Kinematic Rigidbody Trigger Collider Y Y Y Y Y Y

» Comment on Document Threaded Hybrid Flat 24 Comments
1 day - 4,967v
Posted 2012/06/07 - 0:42 GMT
It look pwetty.
5 days - 10,809v
Posted 2012/06/07 - 5:33 GMT
Nice post PA! :)
1 week - 32,767v
Posted 2012/06/29 - 3:38 GMT
Bam for lamp!
1 week - 32,767v
Posted 2012/08/07 - 20:43 GMT
Huh?
1 week - 32,767v
Posted 2012/08/08 - 3:41 GMT
You were asking me about Colliders...
1 week - 32,767v
Posted 2012/08/08 - 4:43 GMT
you mean for Paradise Tower?
1 week - 32,767v
Posted 2012/08/08 - 15:50 GMT
In general.
1 week - 32,767v
Posted 2012/08/08 - 19:36 GMT
Uh, I already sent to Abister, so i gotta wait till September.
Paradise tower may be a failure.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
8 hours - 855v
Posted 2013/03/18 - 21:03 GMT
bam, how do I apply this to a mesh? I just want an easy, simple one, no fancy stuff.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
1 week - 32,767v
Posted 2013/03/18 - 21:14 GMT
Ok. If I had you on Skype I could answer this easily, but you need to select it in the window and scroll down to where it says "collider". You must click the add collider button and the apply button.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
4 days - 9,791v
Posted 2013/03/19 - 15:20 GMT
Wrong! There never was an 'Apply button'... at least, in the versions I've used (2.4+)
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
1 week - 32,767v
Posted 2013/03/19 - 16:30 GMT
Unity 2.61 has an apply button. Trust me.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
4 days - 9,791v
Posted 2013/03/19 - 16:47 GMT
If it's a prefab you're adding it to there's an 'Add' button. Never an 'Apply' button.
 
I have used Unity 2.6.1 since it was first released. And, I had 2.5 before that.... I think I even had 2.4, but not sure. You tell me how much experience that gives me. ;) And I've never seen 'Apply' except in the inspector, to apply changes to a prefab. Pretty sure it's nowhere else.
 
So... :P Trussstttt innn meeeeee....
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
1 week - 32,767v
Posted 2013/03/19 - 17:15 GMT
That's how I add Colliders. Trust me, there is an apply button somewhere that I have to click to make a collider.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
4 days - 9,791v
Posted 2013/03/19 - 17:44 GMT
For your sake:
Then:
The window - the only button needed!
And then it's done. It's there. Try it yourself!
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
8 hours - 855v
Posted 2013/03/19 - 14:35 GMT
uuh, no apply button anywhere, (I have unity 4) whats your skype name, I'll add you to the cc group.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
4 days - 9,791v
Posted 2013/03/19 - 15:15 GMT
Unity 4? You know you can't make worlds in that, right?
 
The tutorial Panzer's pulled is Unity 2-3 only. For 4:
 
- select the gameobject with the mesh you want as a collider. (It MUST have a 'Mesh Filter' on it)
- at the bottom of the inspector, there's a button that says 'Add Component'. Click it.
- in the window it brings up, click 'Physics'
- then click 'Mesh collider' (it has a little green mesh next to it)
 
It  *should* automatically work. You might need to select 'convex' to make it collide properly, but don't if it works without it. Forgive me if I've missed anything out, this is only from memory, and I generally stick around 3.5.
 
If it doesn't work, drop me a PM and I'll make you a guide with images, or try to solve any problems.
 
Good luck! ;)
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
8 hours - 855v
Posted 2013/03/19 - 20:11 GMT
^I'll get the pro trial once I've done several maps.
 
What's a mesh filter? for me the physics > mesh collider is in the bar along with file and edit, I've already tried that with convex on and off.
 
Also for some reason the mesh collider option is stuck on my screen no matter which window I open/close,
 
Update: found out what a mesh filter is, when I click the add a mesh filter thing and try to select it, it doesn't show my whole track, just some pieces, some don't show, I tried adding one mesh filter and the collider for the same piece but it's still not solid.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
4 days - 9,791v
Posted 2013/03/19 - 22:25 GMT
If you drag a mesh from the Assets pane, it already has a mesh filter. No need to add one.
 
You need to select the mesh, not just the parent. It could be buried, or if you used Sketchup, it could be in thousands of incy bits.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
8 hours - 855v
Posted 2013/03/20 - 13:03 GMT
Thanks, it should work now, do I need to select is trigger or not?
 
Is there anyway to further break down the pieces so that I can put special properties to different pieces to give a more realistic effect? (making tarmac more grippy etc.)
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
8 hours - 855v
Posted 2013/03/20 - 13:26 GMT
uh it works now.... But I've come across a glitch, what I want to know is will it be lik this in CC too?
http://i.imgur.com/KdTLkix.jpg
 
Basically what it is is I've placed a terrain on top of the tunnel to make it look like its going through a mountain, I raised the edges of the terrain so that it doesn't block the way, but for some reason when I try to go through the tunnel it sends me flying up into the terrain above, sometimes making me stuck.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
4 days - 9,791v
Posted 2013/03/20 - 18:13 GMT
It's a weird fact with Unity terrains - everything above 0 height collides. You can see this on some other Mars terrrains. (DVO's candy mountains, for example.)
 
If you want a tunnel, you have to carve out a valley so the road is above the terrain. Then, you have to make a terrrain model (in blender or sketchup, or whatever) to fill the slot in the terrain.
 
To make road more grippy, you need to make a Physic Material then change the friction settings to how you want them, than drag it onto the collider.
 
One last warning - you won't be able to export your world from Unity 4. At all.
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
8 hours - 855v
Posted 2013/03/20 - 18:33 GMT
at all... :( no point then....
» Reply to Comment
Re: World Making Tutorial worth sharing (colliders)
4 days - 9,791v
Posted 2013/03/20 - 20:12 GMT
You can still get Unity 2.6, and that works. I can't remember the URL offhand, but do a google search, you should find it. You can import most Unity 4 assets into 2.6.

» Comment on Document Threaded Hybrid Flat 24 Comments
 

This website is powered by Plexpedia
Usage of this site constitutes agreement to the » Legal Stuff