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
:
Assets
folder in the Project window, drag the Game
scene into the Hierarchy window.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.
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.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._GameManager
object in the Hierarchy window. Set the Game Manager - Places Scene Name property to Places
in the Inspector window.
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
:
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.PlaceMarker
in the Hierarchy window, and then from the menu, select Component | Physics | Box Collider.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.0
, Y = 2
, Z = 0
1
, Y = 1
, Z = .2
Box Collider settings, as shown in Scene window
PlaceMarker
from the Hierarchy window.PlaceMarker
signs in the scene, and you should be taken to the Places scene.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.
18.118.200.136