While describing the differences between Character Controller and Rigidbody, we have mentioned several times that both these components provide collision detection for the game objects they are attached to. Since collision detection is a fundamental task that must be performed by any game (at least, any game we can think of!) and it can be a hard dude to deal with, in the following recipe, we provide a few examples to show you how collisions actually work in Unity.
If you want to detect whether a collision happens between two game objects, you need both of them to be equipped with a Collider. A Collider is a sort of cage that contains the game object to check whether another game object enters its boundaries. When this happens, a collision message is sent to the system. Unfortunately, two colliders hitting each other won't generate a detection!
The minimal requirement for a collision detection in Unity is that one has both a Collider and a Rigidbody, and the other has at least the Collider. The Unity manual offers a table to show the minimal requirements for collision detection in Unity at http://docs.unity3d.com/Manual/CollidersOverview.html.
We use an entire new scene for our next recipe: create a new empty scene in your existing Unity project and be ready to follow our instructions!
CollisionDetection
.Start
and Update
functions, and add the following function:void OnCollisionEnter(Collision collision) { Debug.Log("Collision detected!"); }
CollisionDetection
script in Monodevelop
and add the following line inside the Update
function:void FixedUpdate () { rigidbody.AddForce(8*Vector3.right,ForceMode.Acceleration); }
With this line, we are instructing our cube to slowly move right on the x axis, towards the sphere we just added.
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
What happens is that the cube and the sphere collide, but upon collision, the cube stops, because the sphere has no physical properties, except in its physical existence as an obstacle in the path of the cube.
If we want the sphere to be influenced by the collision, a Rigidbody component must be added to the sphere, too. Do that and you'll see that when the cube hits the sphere, the latter will start moving as a consequence of the force it received from the collision!
We don't fear repetitions: collision detection is a crucial component of games and it can be pretty harsh to deal with. Unity forums are exuberant with knowledge on the matter, so please refer to it whenever you have difficulties with collisions and the OnCollisionEnter()
function in general at http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html.
Also, remember that no solution is always good, so the best thing to do is to learn how to take advantage of the assets provided by Unity, depending on each specific condition.
We extensively discussed the differences between Character Controller and Rigidbody, so you can now decide which one better fits your needs. Finding the optimal balance between realism and fun is hard work and requires smart intuition. It is not by chance that this is one of the most important aspects in game design and balancing, which means that a lot of practice and experimentation are key to find that balance!
3.144.17.128