Collision management

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.

Getting ready

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!

How to do it...

  1. Let's begin by creating a new Scene in the Unity project and adding a Plane game object to it.
  2. Move Plane to the bottom of your game scene and resize it as you see fit so it acts as the floor for your scene.
    How to do it...
  3. Create a Cube and move it to the left and above the plane. You will notice that Unity automatically adds a Box Collider to it:
    How to do it...
  4. Add a Rigidbody component to the cube; we described how to achieve that in be specific.
  5. Play your scene: the cube falls on the plane due to gravity and stops as it comes in contact with the plane because when we added the plane to the scene, Unity automatically added the Collider component to it, too.
  6. To detect the collision, we need a script. Create a new C# script in the Scripts folder of the Project panel and name it CollisionDetection.
  7. Double-click on the script to open it in Monodevelop.
  8. Reach for the bottom of the script, outside the Start and Update functions, and add the following function:
    void OnCollisionEnter(Collision collision)
    {
      Debug.Log("Collision detected!");
    }
  9. Save the file, go back to Unity, and drag it from the Project panel to the Cube in the Hierarchy panel. The script should appear in the Inspector panel as a component of the Cube:
    How to do it...
  10. Run the application: as the cube touches the plane, the log message confirming that the collision has been detected is displayed.
  11. Now add another GameObject to your scene, a sphere, and move it to the right side of the scene, with the same y and z coordinates as the cube.
    How to do it...
  12. Go back to the 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.

  13. Go back to Unity, uncheck the UseGravity flag on the Rigidbody component of the Cube, and play the scene. The cube moves right and stops as it touches the sphere. The debug message is played as well.

Tip

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.

How it works...

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!

There is more...

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!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.128