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:
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.
UI_Input
object and select the TurnAndLookTouchpad
.Assets/FoodyGo/Scripts/TouchInput
folder, drag the CustomTouchPad
script and drop it on the TurnAndLookTouchpad
object.
Touch Pad and Custom Touch Pad components in the Inspector window
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.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;
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.
GameManager
class again.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; }
Touch cast as a ray into a scene to determine object's hit
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 collisionsout RaycastHit
: This returns information about the collisionDistance
: This is the maximum range in which the search should be doneMask
: 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.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.3.144.17.45