Starting to work with Update

Next, we will set up the Update function to look after any touch input from the user. In order to do this so that it works in the editor and on the device, we will need a bit of logic to repeat itself. This is because the mouse input isn't related to the touch input.

To begin, add the following code to the Update function:

  void Update () 
  {
    if (Application.isEditor)
    {
      if (Input.GetMouseButton(0))
      {
      }
    }
    else
    {
      if (Input.touches.Length > 0)
      {
      }
    }
  }

In this comparison, we will check whether the game can run in the editor. If it is not, we run what is in else.

If isEditor is true, we then check whether MouseButton(0) is pressed. Otherwise, as most people know, left-click on it.

If isEditor is false, we can check whether touches.Length is more than zero. If it is more than zero, we know that there is a touch on the screen.

Furthermore, you can also use platform-dependent conditions to check for specific devices. For example, you could use the following code:

#if UNITY_IOS
  // Code goes here
#endif

Tip

There are many platform-specific conditions you can use in this way, which can be found at http://docs.unity3d.com/Manual/PlatformDependentCompilation.html.

Given the context of the game we are making and the level of knowledge that is required to follow along, Application.isEditor is suitable.

The in-editor input logic

Under Application.isEditor and inside Input.GetMouseButton(0), add the following code:

  if (Input.GetMouseButton(0))
  {
    if (ActiveTouch.Phase == TouchPhase.Canceled)
    {
      ActiveTouch.CurrentTouchLocation = Input.mousePosition;
      ActiveTouch.StartTouchLocation = Input.mousePosition;
      ActiveTouch.StartTime = System.DateTime.Now;
      ActiveTouch.Phase = TouchPhase.Began;
    }
    else
    {
      ActiveTouch.CurrentTouchLocation = Input.mousePosition;
    }
  }

As mentioned before, when you go through CalculateTouchInput, you will need a few values to compare them with each other to know the type of input we will calculate. This is how they get their value, which will represent a touch.

To begin this block of code, we will first compare ActiveTouch.Phase to check whether it is equal to TouchPhase.Canceled. If this is true and we know that the left mouse button is down, we know that it is a new touch. As this is a new touch, we will assign the values we need based on the location of the mouse and the time of now and set ActiveTouch.Phase to TouchPhase.Began.

Now, if the mouse button is still down, but ActiveTouch.Phase does not equal TouchPhase.Canceled, we know that the user sill has an input pressed, so we will update CurrentTouchLocation of ActiveTouch to represent where their mouse, or finger, is on the screen.

As we have all the information we need for when the user had the finger on the screen, we can now check and handle what happens if they stop the input.

Under the if (Input.GetMouseButton(0)) code block, add the else statement and the following code:

  else
  {
    if (ActiveTouch.Phase == TouchPhase.Began)
    {
      CalculateTouchInput( ActiveTouch );
      ActiveTouch.Phase = TouchPhase.Canceled;
    }
  }

To keep up, the entire Update function should look similar to the following code:

  /* Update is called once per frame */
  void Update () 
  {
    if (Application.isEditor)
    {
      if (Input.GetMouseButton(0))
      {
        if (ActiveTouch.Phase == TouchPhase.Canceled)
        {
          ActiveTouch.CurrentTouchLocation = Input.mousePosition;
          ActiveTouch.StartTouchLocation = Input.mousePosition;
          ActiveTouch.StartTime = System.DateTime.Now;
          ActiveTouch.Phase = TouchPhase.Began;
        }
        else
        {
          ActiveTouch.CurrentTouchLocation = Input.mousePosition;
        }
      }
      else
      {
        if (ActiveTouch.Phase == TouchPhase.Began)
        {
          CalculateTouchInput( ActiveTouch );
          ActiveTouch.Phase = TouchPhase.Canceled;
        }
      }
    }
    else
    {
      if (Input.touches.Length > 0)
      {
      }
    }
  }

As Update is called for every frame rendered, we want to make sure that code is being run when it is time. In order to make sure that we handled ActiveTouch when we need to, we first check to make sure that the mouse button is not down. Once we know that the mouse button is not down, we then check whether ActiveTouch.Phase is equal to TouchPhase.Began.

If this is true, we know that the player has just let go of the mouse as we assign TouchPhase.Began in the block that checks whether GetMouseButton(0) is true.

As the player has let go of the input, we can send the ActiveTouch data to CalculateTouchInput to see what type of touch it is. Once this is done, we can also make sure to assign ActiveTouch.Phase back to TouchPhase.Canceled because the touch is no longer active.

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

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