Updating touch input

Now that we can manage our scene transitions through GameManager, we now need to set the catalyst that triggers a scene change. For the Catch scene, that will be when the player taps on a monster they want to catch, which means we will need to isolate a touch input when it is on a monster. If you recall, our current touch controls work over the entire screen and the input only directs the camera. What we need to do is customize the touch input script to handle that monster touch. Fortunately, for brevity, those script changes were added as part of our last import. Perform the following instructions to configure the new script and review those changes:

  1. Open the Unity editor and load the Map scene.
  2. In the Hierarchy window, expand the MapScene object and select the DualTouchControls object. Rename this object UI_Input in the Inspector window; this will be a more descriptive name to the function of this object.

    Tip

    Renaming game objects, classes, scripts, or other components to match the function is a good development practice. A good name can be equal to a couple lines of documentation on what the function is, whereas a bad name will cause frustration and upgrade or maintenance nightmares.

  3. Expand the UI_Input object and select the TurnAndLookTouchpad.
  4. From the Assets/FoodyGo/Scripts/TouchInput folder, drag the CustomTouchPad script and drop it on the TurnAndLookTouchpad object.
  5. This will add the Custom Touch Pad script component just below the Touch Pad component in the Inspector window, as follows:

    Updating touch input

    Touch Pad and Custom Touch Pad components in the Inspector window

  6. Copy all the settings from the Touch Pad component into the Custom Touch Pad component. Ensure that both the settings are identical.
  7. Remove the Touch Pad component by clicking on the gear icon and selecting Remove Component from the context menu.
  8. The CustomTouchPad script is virtually identical to the TouchPad script, with only one line of difference. You may be thinking, why didn't we just modify the original script then? The reason we created a new copy of the script and then modified it is to make it our own. That way, if the Cross Platform Input asset needs to be upgraded in the future, our custom script changes won't be overwritten.
  9. Click on the gear icon on the Custom Touch Pad component and select Edit Script from the context menu. This will open the script in the editor of your choice.
  10. Scroll down to or search for the method OnPointerDown; the following is an excerpt of the method and the one line of changed code:
            public void OnPointerDown(PointerEventData data)
             {
             if (GameManager.Instance.RegisterHitGameObject(data)) return;
  11. The OnPointerDown method is called when the user first touches the screen and will be the start of a swipe. What we want to do though is not track a swipe if the touch was on an important object. This is what the new line does. The line of code calls the GameManager.Instance.RegisterHitGameObject with the touch position. If an important object is touched, true is returned and we return, not allowing the swipe action to start. If instead nothing is hit, the swipe will act as normal.

    Tip

    GameManager.Instance denotes a call to get the singleton instance of the GameManager. A singleton is a well-known pattern used to maintain a global single object instance. A singleton is perfect for our GameManager since it will be used by a number of classes to control a single game state.

  12. Now, while you are still in the code editor, open the GameManager class again.
  13. Scroll down to the RegisterHitGameObject method:
            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; 
                     } 
    
  14. The function of this method is to determine whether a particular touch input has hit an important object in the scene. It does this by essentially casting a ray from the position on the screen into the game world. You can think of a ray as a light pointer, and perhaps the following diagram will help:

    Updating touch input

    Touch cast as a ray into a scene to determine object's hit

  15. The bulk of the work is done within the Physics.Raycast method, which uses the ray cast by the touch interaction, a reference to a RaycastHit object, the distance the ray should be tested for, and finally, a layer mask to determine whether and how an object was hit. There is a lot going on here, so let's break those parameters down further:
    • Ray: This is the ray or the straight line that is cast and used to test for collisions
    • out RaycastHit: This returns information about the collision
    • Distance: This is the maximum range in which the search should be done

      Tip

      In the code, we use Mathf.Infinity for the range of the search. For the number of objects we have in the scene currently, this will work fine. In more complex scenes, a value of infinity could be expensive, as you may only want to test all the objects with in visual distance.

    • Mask: The mask is used to determine the layers that should be tested for collisions. We will get into more detail about physics collisions and layers in the next section.
  16. Go back to Unity and run the game in the editor by pressing Play. As you may notice, nothing new happens. That is because we are missing one vital piece. The Physics.RayCast is testing for collisions against objects with colliders. As of yet, our monster object does not have a collider, nor is it in any way configured to use the physics engine, but we will rectify that shortly.
..................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.45