What is Touch?

Touch is a struct. To put it differently, it is a group of different variables that hold information about what the input can do. It is possible to monitor multiple Touch structs at once if the user is trying to input more than one, although we are only looking for one.

We do not need all the information Unity's touch gives us. We only need the location on the screen where the touch started, how long the touch lasted in seconds, where the touch ended on the screen, and the current state of the touch. From this, we will be able to detect the type of input and the direction of the input, if the player tapped, or if the player swiped in a direction.

Our own Touch

The first step to coding our input class is to create our own struct that will hold the values we need. The benefit to creating our own Touch is that we can then use it for only what we need. It also gives us the means to use the mouse input to accomplish the same input logic as the device, allowing editor testing that will match the device.

At the top of the PlayerInput class, enter the following code:

public struct SimpleTouch
{
  public Vector2 StartTouchLocation;
  public Vector2 CurrentTouchLocation;
  public DateTime StartTime;
  public TouchPhase Phase;
}

As stated previously, we need to know the location where the touch started, ended, how long it lasted, and the current state (or phase) of the touch.

  • Vector2: This is a struct that holds X and Y float variables and is generally used to store a 2D location.
  • DateTime: This class is used to manage the date and time in your code. We will use this so that we know at what time the touch input started. Then, when you subtract the time the touch ended, we will get the length of the input in time.
  • TouchPhase: This enum is used to categorize the state of the input. We will use this to compare the touch input from Unity with our SimpleTouch.

Global variables

We will also need to include a few settings that we can use to manage how we detect a swipe. Under our SimpleTouch struct, enter the following code:

public float SwipeTime;
public float SwipeDistance;

These two values, which we can set from within the editor because they are public, allow you to fine-tune what we call a Swipe.

We will also need two variables to manage the touch input. The first one will be the one Unity gives us, and the second variable will be the SimpleTouch struct we made. To add these, enter the following code under the two previous variables:

private SimpleTouch ActiveTouch;
private Touch DeviceTouch;
  • ActiveTouch: This specifies the simple information that we need to detect a swipe
  • DeviceTouch: This denotes the raw touch information Unity gives us when the player touches the device screen

    Tip

    These variables are set to private to prevent both the editor from showing them and prevent any other class from accessing them. To put it differently, we don't want any other source to manipulate these values, so we label them as private.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset