Implementing touch with Input System's Enhanced Touch API
As mentioned above, the player can zoom the camera by making a pinching motion with their fingers. Moving the fingers closer together zooms out, while moving them further apart zooms in. You’ll add this motion next. Add a new class variable to InputManager
above the Awake
method:
private float lastMultiTouchDistance;
Then add a ZoomCamera
method below the MoveCamera
method:
private void ZoomCamera(Touch firstTouch, Touch secondTouch)
{
//1
if (firstTouch.phase == TouchPhase.Began ||
secondTouch.phase == TouchPhase.Began)
{
lastMultiTouchDistance = Vector2.Distance(firstTouch.screenPosition,
secondTouch.screenPosition);
}
//2
if (firstTouch.phase != TouchPhase.Moved ||
secondTouch.phase != TouchPhase.Moved)
{
return;
}
//3
float newMultiTouchDistance = Vector2.Distance(firstTouch.screenPosition,
secondTouch.screenPosition);
//4
CameraController.Instance?.Zoom(newMultiTouchDistance <
lastMultiTouchDistance);
//5
lastMultiTouchDistance = newMultiTouchDistance;
}
On the surface, zooming may seem complicated. But fear not, the logic is simple! The ZoomCamera
determines if the player pinches their fingers closer together or further apart. Here’s how it works:
lastMultiTouchDistance
and newMultiTouchDistance
store the distance between the two fingers.lastMultiTouchDistance
amount for the next time the method runs.Now it’s time to hook up a call to ZoomCamera
. Add an else if
statement to Update
. Replace the contents of the Update
method with the following:
if (Touch.activeFingers.Count == 1)
{
MoveCamera(Touch.activeTouches[0]);
}
else if (Touch.activeFingers.Count == 2)
{
ZoomCamera(Touch.activeTouches[0],
Touch.activeTouches[1]);
}
Touch.activeFingers.Count == 2
means two fingers are on the screen, and you can assume they’re attempting to zoom. That’s it! Save your changes, run your game and pinch your fingers to see the camera in action.
Now that you’ve added the touch input camera controls, it’s time to add some new features!