This works well enough for what we're doing right now, but I'm assuming that you'll want to know how to use the mobile device's own way of moving, so we will go ahead and learn how to replicate the same functionality using touch instead.
Unity's Input engine has a property called Input.touches, which is an array of Touch objects. The Touch struct contains information on the touch that occurred, having information such as the amount of pressure on the touch and how many times you tapped the screen. It also contains the position property--like Input.mousePosition--that will tell you what position the tap occurred at, in pixels.
To adjust our preceding code to support touch instead of mouse inputs, our code could look something like the following:
//Check if Input has registered more than zero touches
if (Input.touchCount > 0)
{
//Store the first touch detected.
Touch myTouch = Input.touches[0];
// Converts to a 0 to 1 scale
var worldPos = Camera.main.ScreenToViewportPoint(myTouch.position);
float xMove = 0;
// If we press the right side of the screen
if (worldPos.x < 0.5f)
{
xMove = -1;
}
else
{
// Otherwise we're on the left
xMove = 1;
}
// replace horizontalSpeed with our own value
horiztonalSpeed = xMove * dodgeSpeed;
}
Now, you may note that this code looks very similar to what we've written in the preceding section. With that in mind, instead of copying and pasting the appropriate code twice and making changes like a number of starting programmers would do, we can instead take the similarities and make a function. For the differences, we can use parameters to change the value instead; consider the following parameters:
- Keeping that in mind, let's add the following function to the PlayerBehaviour class:
/// <summary>
/// Will figure out where to move the player horizontally
/// </summary>
/// <param name="pixelPos">The position the player has
/// touched/clicked on</param>
/// <returns>The direction to move in the x axis</returns>
float CalculateMovement(Vector3 pixelPos)
{
// Converts to a 0 to 1 scale
var worldPos = Camera.main.ScreenToViewportPoint(pixelPos);
float xMove = 0;
// If we press the right side of the screen
if (worldPos.x < 0.5f)
{
xMove = -1;
}
else
{
// Otherwise we're on the left
xMove = 1;
}
// replace horizontalSpeed with our own value
return xMove * dodgeSpeed;
}
Now, instead of using Input.mousePosition or the touch position, we instead use a parameter for the function. Also, unlike previous functions we've written, this one will actually use a return value; in this case, it will give us a floating point value. We will use this value in the Update to set horiztonalSpeed to a new value when this function is called. Now, we can call it appropriately.
- Now, update the Update function, as follows:
/// <summary>
/// Update is called once per frame
/// </summary>
void Update ()
{
// Movement in the x axis
float horizontalSpeed = 0;
//Check if we are running either in the Unity editor or in a
//standalone build.
#if UNITY_STANDALONE || UNITY_WEBPLAYER
// Check if we're moving to the side
horizontalSpeed = Input.GetAxis("Horizontal") *
dodgeSpeed;
// If the mouse is held down (or the screen is tapped
// on Mobile)
if (Input.GetMouseButton(0))
{
horizontalSpeed = CalculateMovement(Input.mousePosition);
}
//Check if we are running on a mobile device
#elif UNITY_IOS || UNITY_ANDROID
//Check if Input has registered more than zero touches
if (Input.touchCount > 0)
{
//Store the first touch detected.
Touch myTouch = Input.touches[0];
horizontalSpeed = CalculateMovement(myTouch.position);
}
#endif
// Apply our auto-moving and movement forces
rb.AddForce(horizontalSpeed, 0, rollSpeed);
}
In the preceding example, I am using a #if directive based on the platform selected. Unity will automatically create a #define depending on what has been selected as the platform we are deploying for. What this #if does, along with #elif and #endif, is allow us to include or exclude code from our project based on these symbols.
In Visual Studio, you may note that if you're building for iOS or Android, the code within the UNITY_STANDALONE section is grayed out, meaning that it won't be called currently. However, if we change our platform, the appropriate code will become used, depending on the platform that we would like to create for.
This will allow us to specify code for different versions of our project, which is vital when dealing with multiplatform development.
- Save the script and dive back into Unity. Upon exporting your game to your Android device, you should note that the controls now work correctly using our newly created touch code.