Creating the Input GameObject

Save the document and then maximize or select Unity. This will force the editor to compile the code that we have just written.

  1. On the left-hand side of the editor in the Hierarchy tab, right-click and select Create Empty. This will add a blank GameObject to the scene.
  2. Right-click on the GameObject that we just created and select Rename. Name it Input.
  3. While still having the Input GameObject selected, look to the right-hand side of the screen in Inspector and click on Add Component.
  4. When the search window pops up, enter the first half of what you called your input class. For example, when I enter player, my Player Input class will appear in the list:
    Creating the Input GameObject
  5. Click on your input class to add it to the Input GameObject. If everything goes according to plan, you will see the component added and SwipeTime and SwipeDistance both set to 0. Alternatively, you can add the Input component to GameObject by left-clicking, holding, and dragging it onto GameObject in the Hierarchy tab from the Assets | Scripts folder.
    Creating the Input GameObject
  6. For now, set Swipe Time to 0.08 and Swipe Distance to 25.

Calculating the TouchInput function

We can now create the function (also called a method) to take in the touch information so that we can check it. To start, enter the following code under the last two variables:

private void CalculateTouchInput(SimpleTouch CurrentTouch)
{
}
  • void: This a keyword used to identify the function as something that will not return a value. As this function is designed to take in data and manage it within its own scope, we don't need it to return any data back.

    Tip

    Scope is the range of code that the brackets will manage. In this case, the brackets of CalculateTouchInput will manage detecting specific types of touch input. Variables such as ActiveTouch and DeviceTouch are what is called Global Scope. This means that the entire class can read and manipulate them, whereas something such as SimpleTouch CurrentTouch are only seen and manipulated in CalculateTouchInput.

  • CalculateTouchInput: This is the name of our function. This can be anything you want, although the naming function should be done in a way that gives some clue as to what it will do. In our case, it will check the input data and calculate what to do with it.
  • (SimpleTouch CurrentTouch): This portion of the function is what is called an argument or parameter. It is a way to send data to this function so that it can be used or altered.

Next, we will use CalculateTouchInput to detect what type of touch is being sent to the function based on the data of CurrentTouch.

Add the following code in the CalculateTouchInput function:

private void CalculateTouchInput(SimpleTouch CurrentTouch)
{
  Vector2 touchDirection  = 
  (CurrentTouch.CurrentTouchLocation - 
  CurrentTouch.StartTouchLocation).normalized;

  float touchDistance = 
  (CurrentTouch.StartTouchLocation - 
  CurrentTouch.CurrentTouchLocation).magnitude;

  TimeSpan timeGap = System.DateTime.Now - CurrentTouch.StartTime;

  double touchTimeSpan = timeGap.TotalSeconds;

  string touchType = 
  ( touchDistance > SwipeDistance && touchTimeSpan > SwipeTime )
  ? "Swipe" : "Tap";

  print( touchType );
}

As mentioned before, we will need a few things to tell what type of input we are calculating.

  • touchDirection: This is the value of the current location of the touch minus the start location of the touch.
  • normalized: This converts the vector, which can have very large numbers, to a vector that has a magnitude of one—or all the values in the vector are no more than 1.0 or less than -1.0. For example, for touchDirection, we need the direction in which the player swiped. To do this, we subtract the current location from the start location of the touch. This vector value still holds the same direction between both the points; however, now the values of X and Y are normalized to 0.0 and 1.0, respectively.

    Let's look at it like this. If you were to have a vector with the Y value of 1.0 and the X value of 0.0, you know that the direction of the vector is vertical and going upwards in the direction of Y. Now, if you have another vector with the Y value of 0.8 and the X value of 0.2, you can still see that the direction is moving upwards in the direction of Y, but it is also slightly moving towards positive X. As we have the start and current location (or when this is calculated, the end location as CalculateTouchInput is being called when the user lets go of a tap), we can subtract them from each other and get the direction between them. When we normalize this value, we will still have the same direction; only the vector member values are within -1 and 1 instead of something much larger, which is what we'd have if we didn't normalize the vector.

    Tip

    This example is set to Vector2, although the same is represented in Vector3, but with the added value of z.

  • touchDistance: This is the total magnitude of the start and current touch locations. Unlike what was mentioned before in touchDirection, this magnitude represents the entire length value between the two vector points. For example, say you have a screen resolution of 400*400. If the user taps at 0,0 and drags their finger to 0,200, the magnitude of this swipe will be 200 because the total value of this swipe is 0 + 0 for X and 0 + 200 for Y.
  • timeGap: One of the factors that decide, the difference between a swipe and a tap is the length in time the users finger was on the screen. We can have this value by first storing when the tap started and then subtracting it from the current time on the device.
  • touchTimeSpan: As the TimeSpan struct is compatible with System.DateTime.now, we will be able to subtract one from the other. TimeSpan then allows you to convert this time difference directly to seconds, which is what we do to get touchTimeSpan. Once we have this, we know exactly how long the user had their finger on the screen. Coupled with touchDistance, we can decipher the type of touch.
  • touchType: We will be able to tell you what type of touch is calculated by making sure that touchTimeSpan is greater than SwipeTime and touchDistance is greater than SwipeDistance. If this comparison is true, we will assign touchType as Swipe. If it is set to false, we will set it to Tap.
  • print(touchType): This function allows you to print text to the console window in the Unity editor. Print is a handy way to display some debug information in the editor so that you can see the value of something directly.

Alternatively, you can also use Debug.Log( ) to display the values in the console log.

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

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