Connecting the pieces

With the Places scene complete, now we need to integrate the scene back into the Gamescene. Perform the following instructions to configure the Places scene so that it can be loaded by the GameManager:

  1. From the Assets folder in the Project window, drag the Game scene into the Hierarchy window.
  2. Select the Inventory service in the Places scene, and note what you set the Inventory Service - Database Version to in the Inspector window. Then, select the Inventory service in the Game scene and change the Inventory Service - Database Version to match.

    Note

    If we did not do this step, the database may get corrupted and just wipe itself clean with the updated objects. This is not so critical at this point, but would be very inconvenient to any users if you were creating an update of the game.

  3. Since we only need one Inventory service, select the Inventory service object in the Places scene and press delete to remove it. Select and delete the Event System object in the Places scene also.
  4. Select the PlacesScene object in the Hierarchy window and deactivate it by unchecking the checkbox beside the object name in the Inspector window. We will disable the root object because we will be loading the scene on start.
  5. Right-click (press Ctrl and click on a Mac) on the Places scene, and select Remove Scene from the context menu. Ensure that you save the changes we just made to the scene.
  6. Select the _GameManager object in the Hierarchy window. Set the Game Manager - Places Scene Name property to Places in the Inspector window.
  7. From the menu, select File | Build Settings to open the Build Settings dialog. Just confirm that all the scenes have been added and are checked to be included, as shown in the following screenshot:

    Connecting the pieces

    Build Settings with Places scene added

If you run the game in the editor, unfortunately, you will still be unable to click on a place to open the Places scene. That is because we need to allow our PlaceMarker to act as a collider, something we omitted in the last chapter. Perform the following instructions to add the collider and other settings to the PlaceMarker:

  1. Drag the PlaceMarker prefab we constructed in the previous chapter from the Assets/FoodyGo/Prefabs folder into the Hierarchy window. Note that we are altering the PlaceMarker and not the PlacesMarker prefab we modified in this chapter.
  2. Select the PlaceMarker in the Hierarchy window, and then from the menu, select Component | Physics | Box Collider.
  3. Double-click on the PlaceMarker to focus the object in the Scene window. You will see a green box at the base of the object; this is the collider.
  4. In the Inspector window, change the Box Collider - Center and Size to the values shown in the following list:
    • Box Collider: Center: X = 0, Y = 2, Z = 0
    • Box Collider: Size: X = 1, Y = 1, Z = .2
  5. Now, change the objects layer to Monster. When prompted, don't worry about changing the children, as the collider is only on the top object. We likely should create a new layer for our collision detection, but this will work for now.
  6. Check the following screenshot to make sure that the object properties in the Inspector window match and the Scene window looks similar to the following:

    Connecting the pieces

    Box Collider settings, as shown in Scene window

  7. Click on the Apply button beside the Prefab options in the Inspector window to save the changes. Then, delete the PlaceMarker from the Hierarchy window.
  8. Press Play to run the game in the editor. Make sure that the GPS service is running in the simulation mode. Click on any of the PlaceMarker signs in the scene, and you should be taken to the Places scene.
  9. Also, of course, as always, ensure that you build and deploy your game to your mobile device for testing, making sure that everything transitions and looks as expected.

    Note

    If you encounter problems running the game, ensure that you consult Chapter 10 , Troubleshooting, for help.

That was relatively simple to set up, so let's take a quick look at the code that wires up the collision. Open the GameManager script in the editor of your choice, and scroll down to the HandleHitGameObject method. At the top of the method is our original code that handles when the player clicks on a monster and is then taken to the Catch scene. Just below that, we handle when our PlaceMarker gets hit (selected):

if (go.GetComponent<PlacesController>() != null) 
{ 
       print("Places hit, need to open places scene "); 
       //check if the scene has already been run 
       if (PlacesScene == null) 
       { 
           SceneManager.LoadSceneAsync(PlacesSceneName, LoadSceneMode.Additive); 
       } 
       else 
       { 
            //the scene has run before, reactivate it                     
            var psc = PlacesScene.RootGameObject.GetComponent<PlacesSceneController>(); 
            if (psc != null) 
            { 
                var pc = go.GetComponent<PlacesController>(); 
                psc.ResetScene(pc.placeId, pc.location); 
            } 
            PlacesScene.RootGameObject.SetActive(true); 
        }                 
        MapScene.RootGameObject.SetActive(false); 
 } 

This block of code is very similar to what we used when the monster object (MonsterController) was hit or selected. If you follow the comments, things are quite self-explanatory. As you can see, following this same pattern we could easily add other objects you may want to interact with quickly.

You may also notice that it doesn't matter how far the places are away from the player, they can still interact with them. This is intentional at this point, but it could be something very easy to fix by just setting the length of the ray when testing for collisions. If you scroll up in the code to the RegisterHitGameObject method, you will see the ray is set to infinity:

public bool RegisterHitGameObject(PointerEventData data) 
{ 
    int mask = BuildLayerMask(); 
    Ray ray = Camera.main.ScreenPointToRay(data.position);             
    RaycastHit hitInfo; 
    if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, mask)) 
     { 
        print("Object hit " + hitInfo.collider.gameObject.name); 
        var go = hitInfo.collider.gameObject; 
        HandleHitGameObject(go); 
 
        return true; 
     } 
     return false; 
} 

You could change this value to a hardcoded value, such as 100 or perhaps revisit our discussion on GPS accuracy and set this value based on the current accuracy of the device. In either case, this is another game mechanic that can be easily set, but can have wide variations to how your game will be played.

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

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