Unity 2020 Mobile Game Development
上QQ阅读APP看书,第一时间看更新

Using mouse input

Before we dive into mobile-only solutions, I do want to point out that it is possible to write inputs that work on both mobile and PC, namely using mouse controls. Mobile devices support using mouse clicks as taps on the screen, with the position of the tap/click being the location where the finger has been pressed. This form of input provides just the position where the touch happened and that it happened; it doesn't give you all of the features that the mobile-only options do. We will be discussing all of the features you get using mobile-specific input later on in this chapter, but I think it's important to note how to have click events on the desktop as well. I personally use the desktop often for ease of testing on both the PC and on my device, so I don't have to deploy to a mobile device to test every single change made in the project.

The following steps show how to use the desktop-based mouse click events for movement of the player:

  1. Inside Unity, open up your PlayerBehaviour script and add the following highlighted code to the Update function:
/// <summary>
/// FixedUpdate is called at a fixed framerate and is a prime place to put
/// Anything based on time.
/// </summary>
private void FixedUpdate()
{
// Check if we're moving to the side
var horizontalSpeed = Input.GetAxis("Horizontal") * dodgeSpeed;

// If the mouse is held down (or the screen is pressed
// on Mobile)
if (Input.GetMouseButton(0))
{
// Converts to a 0 to 1 scale
var worldPos =
Camera.main.ScreenToViewportPoint(Input.mousePosition);
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
horizontalSpeed = xMove * dodgeSpeed;
}

rb.AddForce(horizontalSpeed, 0, rollSpeed);
}

We have added a number of things to the preceding script. First, we check whether the mouse button had been held down or not through the use of the Input.GetMouseButton function. The function will return true if the mouse is held down, and false if it is not. The function takes in a parameter, which is for what mouse button we'd like to check, providing 0 for the left button, 1 for the right, and 2 for the middle button. For mobile, however, only 0 will be picked up as a click.

For more info on the Input.GetMouseButton function, check out  https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html.

To start off, we can get the position that the mouse is at using the Input.mousePosition property. However, this value is given to us in screen space. What is screen space? Well, let's first talk about how we traditionally deal with positions in Unity making use of world space.

When dealing with positions in Unity through the Inspector window, we have the point (0,0,0) in the middle of our world, which we call the origin, and then we refer to everything else based on an offset from there. We typically refer to this method of positioning as World Space. Assuming that we have our camera pointing toward the origin, World Space looks like this:

The lines are the x, y, and z axes of our world. If I were to move an object to the right or left, it would move along the x axis positively or negatively, respectively. When in school, you may have learned about using graphs and points, and world space works very much like that.

It's not important for the current conversation, but you should note that children of parented objects use a different system in the Inspector, which is that they are given positions relative to their parents instead. This system is called local space.

When using mouse input, Unity gives us this information in another space, Screen Space. In this space, the position is based on where the camera is and isn't involved with the actual game world. This space is also just in 2D, so there's only an x and y position with z always being stuck at 0:

In this case, the bottom left of the screen would be (0,0) and the top right would be (Screen.width, Screen.height). Screen.width and Screen.height are values in Unity that will give us the screen size of the screen window in pixels.

We could use these values as provided and then compare what side of the screen the player pressed, but, in my case, I think it'd be better to convert the position into an easier space to work with. One such space is the Viewport space, which goes from (0,0) to (1,1):

Note that some of Unity's functions will use  Vector3 instead of  Vector2 in order to work with 3D spaces as well.

Instead of searching whether our x position is less than half of the screen width, I can instead just check whether it's less than 0.5, which is what we are doing here. If the value is less than 0.5, it's on the left side of the screen so we return -1; otherwise, it's on the right side, so we give 1.

Once we know that, we can then set the horizontal speed variable to move to the left or right based on our movement.

  1. Save the script and dive back into Unity and you will see the following:

As you can see in the preceding screenshot, we can now use either the mouse (Input.mousePosition) or our keyboard (Input.GetAxis) to move our player.

This form of input 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.