Chapter 7. Forge a Destructible and Interactive Virtual World

Most games need an environment to create the feeling and experience for the players of the game's world. In games, we can also call it a level. It can interact with the player as well. The level will create the difficulty and challenges for the players to play through and finish the game. Each level will include static and non-static objects. The static objects include houses, buildings, bridges, trees, and so on, and won't be movable. On the other hand, non-static objects will interact with the player, and include rocks, doors, switches, and so on.

In many cases, we will see that games use the non-static objects to make the level much more fun to play by adding the events or triggers to the objects and making them interact with the players. For example, the players have to push the switch to open the door, or get blocked because the bridge was destroyed when the player triggered the event. We can also add physics to the non-static objects to make the objects behave realistically, such as adding physics to the rocks when they are falling down to the ground.

In this chapter, we will continue from the last chapter to optimize our AIController script and add the ragdoll object to replace the AI when it dies. Then, we will create a destructible rock that will trigger when the player gets close to it, and a destroyable rock that the player and AI can shoot and destroy. We will also create the ragdoll character object for the AI to behave realistically when he is killed by the player.

Mission briefing

We will start by optimizing the AIController script to make it run faster and attaching the ragdoll object to replace the AI with the ragdoll character object when the AI is dead.

Next, we will create simple, non-static destructible rocks, which will be triggered when the character gets close to it, and a simple destructible wall that the player can shoot the rocket to destroy. By making both of the destructible objects fall down, we will need to have a physics calculation in Unity by attaching the rigidbody to each object and activating it when it is triggered.

What does it do?

First, we will open our old AIController script (from Project 6, Creating Smart AI Enemies) to optimize some parameters and add the ability to replace the AI with the ragdoll object when it dies. Then, we will create the destructible wall from four multiple cube objects, which will each have a rigidbody . Of course, we will need to create the script to make the object break apart into ParentRocks and Rock scripts when the character's shot hits it. We will also create another destructible rock from multiple cubes, which will fall when the player gets close to it. To make this rock fall down, we will create a trigger area and a TriggerArea script, which will trigger and make the rock fall down when the player enters this area.

Why Is It Awesome?

Unity has the NVIDIA PhysX physics engine built-in, which is very powerful for creating the realistic physics simulation for our game world. In this chapter, we will learn how to apply physics to our game by applying ragdoll physics to the character as well as attaching the rigidbody to make the objects or environment react to the player whenever we want. This technique will add more variety to the game and will make it very challenging to play.

Your Hotshot Objectives

We will start by importing the chapter 7 package, and then we will go to each topic as follows:

  • Optimizing the AIController script
  • Creating a ragdoll
  • Creating a destructible wall
  • Creating a Rockslide and trigger area
  • Creating ParentRocks and rock script

Mission Checklist

First, we need the chapter 7 package. Go to the book website—http://www.packtpub.com/support?nid=8267—to download the Chapter7.zip package. The package will contain all the necessary resources— assets, scripts, and prefabs—that we will be using in this chapter.

Import the package to Unity as you did for the previous chapters, as shown in the following screenshot:

Mission Checklist

In the Project view, we will see the Chapter5 folder, Chapter6 folder, Chapter7 folder, Gizmos folder, Standard Assets, and VirtualWorld scene. The Chapter5 and Chapter6 folders will contain the entire assets of Chapter 5 and Chapter 6. The Chapter7 folder will contain all the scripts, assets, terrain, and prefabs that will be used in this chapter. The Gizmos folder will contain the wayIcon.png. The Standard Assets folder contains all the necessary built-in assets that will be used in the chapter. Finally, we will use the VirtualWorld scene to create the destructible objects in this chapter. VirtualWorld scene includes the old work from Chapter 5 and Chapter 6, and some new scripts to make it more suitable for this project. Let's double-click on it to open the scene.

Mission Checklist
..................Content has been hidden....................

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