To calculate the type of touch from the touch input, we will follow the same logic. We will only use the touch location to calculate StartTouchLocation
and CurrentTouchLocation
.
Add the following code in the else
block of the Update
function, after Application.isEditor
and in if Input.touches.Length > 0
:
if (Input.touches.Length > 0) { DeviceTouch = Input.GetTouch(0); if (ActiveTouch.Phase == TouchPhase.Canceled) { ActiveTouch.Phase = DeviceTouch.phase; ActiveTouch.StartTime = System.DateTime.Now; ActiveTouch.StartTouchLocation = DeviceTouch.position; ActiveTouch.CurrentTouchLocation = DeviceTouch.position; } else { ActiveTouch.CurrentTouchLocation = DeviceTouch.position; } }
As you can see, this follows the same concept as what we do for when the game is running in the editor. The big difference being that we will now assign DeviceTouch
to Input.GetTouch(0)
when there is more than one touch active. From this, we can view the position of the touch on screen and update this position for however long the touch exists.
Just as with the mouse input, we need to manage the touch when the user stops clicking on the screen.
Add an else
block after if Input.touches.Length > 0
:
else { if (ActiveTouch.Phase != TouchPhase.Canceled) { CalculateTouchInput( ActiveTouch ); ActiveTouch.Phase = TouchPhase.Canceled; } }