Chapter 5. Catching the Prey in AR

If you recall from our Foody GO storyline, the players needed to hunt down the escaped experimental cooking monsters and catch them. As of the end of last chapter, the player can now track and see monsters around them on the map. What we need now is for the player to be able to interact with and attempt to catch the monsters they can see. In order to keep our game immersion deep, we want the player to catch the monsters in an alternate-reality view. Thus, we also want to incorporate the device's camera to provide the backdrop to our catch activity. Adding the AR component will now classify our game in the real-world adventure or location-based AR genre.

In this chapter, we will be adding a number of new features to our game that will require us to touch on a few new concepts. Unlike the previous chapters, we won't get too heavily into the theory since most of these new concepts are fundamental to game development and Unity. Instead, we will review how these things work within Unity and provide some references for those wanting to learn more about a particular concept. Here is the list of items we will be covering in this chapter:

  • Scene management
  • Introducing the Game Manager
  • Loading a scene
  • Updating touch input
  • Colliders and rigidbody physics
  • Building the AR catch scene
  • Using the camera as our scene backdrop
  • Adding the catching ball
  • Throwing the ball
  • Checking for collisions
  • Particle effects for feedback
  • Catching the monster

Just as in the previous chapters, if you have Unity open from the previous chapter, with the game project loaded, then move on to the next section. Otherwise, open up Unity and load the FoodyGO game project or open the Chapter_4_End folder from the downloaded source code. Then, ensure that the Map scene is loaded.

Tip

When you open up one of the saved project files, you will likely also need to load the starting scene. Unity will often create a new default scene rather than trying to guess the scene that should be loaded.

Scene management

Before we jump into adding some new features to the game, we should step back a bit and address how we will transition from scene to scene. Currently, we have two scenes developed for our game: the Splash and Map scenes. Also, in this chapter, we will add two more scenes, Game and Catch. However, we currently have no way of managing scene transitions or game-object lifetime. Ideally, we want some master object and/or script that can do all that for us. This is exactly what we will build, and we will call it Game Manager.

Hold your excitement for a minute. In order to seamlessly load and transition between scenes, we want to do a little housekeeping with our current scenes. Open up Unity and perform the following instructions to clean and reorganize the current game scenes:

  1. Ensure that the Map scene is loaded in the Hierarchy window. Create a new empty game object by selecting the GameObject | Create Empty menu.
  2. Rename the new object MapScene and reset the transform to zero.
  3. Drag and drop the Player object onto the Map object in the Hierarchy window. This will make the Player a child of Map. Do this again for Map_Tiles, Services, DualTouchControls, and Directional Light, as shown in the following screenshot:

    Scene management

    MapScene child objects

  4. Save the scene by selecting menu item File | Save Scene.
  5. Save the scene as a new scene called Game by selecting menu item File | Save Scene As..., and in the Save Scene dialog, enter Game for name and click on the Save button.
  6. Ensure that you follow the next sequence of steps closely in order to avoid frustration. If you do delete something you shouldn't and accidently save it, then you can always restart from the source code Chapter_5_Start folder.
  7. In the Hierarchy window, delete the EventSystem object by selecting the object and pressing the delete key. The scene should only contain the MapScene and children now.
  8. From the menu, select File | Save As... and name the scene Map and then click on the Save button. You will be prompted to overwrite the scene; click on Yes, as follows:

    Scene management

    Confirmation prompt

  9. Open the new Game scene by selecting the Assets folder in the Project window and then double-clicking on the Game scene.
  10. Delete the MapScene object by selecting it and pressing delete. After you delete the MapScene object, you will notice that the Game window goes black and now displays a No cameras rendering message. Try not to be distracted, panic, or go into repair mode and try to add a new camera to the scene. Everything is OK, so just carry on.
  11. The Game scene should now just have an EventSystem object. We could actually delete this object, as Unity would automatically add it back in if a UI component was added to the scene. In order to better manage our scenes and objects though, we will keep the EventSystem object as it is.
  12. Save the scene by selecting the menu item File | Save Scene.
  13. Open the Splash scene by selecting the Assets folder in the Project window and then double-clicking on the Splash scene to open it.
  14. Select the menu item GameObject | Create Empty. Rename the new object SplashScene and reset the transform to zero in the Inspector window.
  15. Drag and drop the Main Camera, Directional Light, and Canvas to the SplashScene object. They should all now be children of the SplashScene object.
  16. Delete the EventSystem object by selecting it and pressing delete. The Splash scene in the Hierarchy window should not look like the following screen excerpt:

    Scene management

  17. Save the scene by selecting menu item File | Save Scene.

You should now be able to open the Splash and Map scenes and play them without any issues. Try this now as a test to make sure that you moved everything around and it has all saved fine. As you run the scenes, you will notice that an EventSystem object gets dynamically added to the Map scene; that is fine and expected.

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

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