Space Short Monthly: “New” Input System in Unity Part II

Using both keyboard and touch for movement in the new input system in Unity — Touch

Danielle H
4 min readJul 28, 2023

Notice: I will not publish new stories during August. See you again in September!

In the previous article, we covered using arrow keys to move a player using Unity’s new Input System. In this article, we will add touchscreen movement (dragging and tapping) and switching between the modes.

Again, I highly recommend the following tutorials which helped me get started: gameDevBeginner and samyam.

Adding touch input to the map

In the previous article, we arrived at the following Move Action map:

Now we add the Touch movement (dragging) to the map. We have one unbound entry (if you don’t, simply add it using the + in the Move row). Click on it. In the Path value, choose Position(Touchscreen) (you can search for it). Also, mark that it’s part of the Touch scheme:

Excellent!

Adding touch input to the Player Input

Go back to your Player, and look at the Player Input module. Now we need to decide which is the default scheme? I chose Keyboard. And also, mark Auto-Switch: This will switch schemes automatically when detects the device used for your game.

Adding touch to your player

Now we need to modify the Update function in our player. If the current Scheme is Keyboard, then we want to use the movement form before. If the current Scheme is Touch, we want to move the player to the point that was touched.

On the other hand, we don’t want our player to be able to teleport, so we don’t just set the player’s position to the touch position. Instead, we use MoveTowards, with the same speed as the keyboard movement. This will ensure smooth movement that is limited in speed and without teleportation :) Also, this will allow the user to tap and not only to drag.

void Update()
{
//1 - get current scheme
String scheme = GetComponent<PlayerInput>().currentControlScheme;
if (scheme == "Keyboard")
{
//2 - Code from previous article
transform.Translate((Vector3.right * movement[0] + Vector3.forward * movement[1]) * Time.deltaTime * speed);
}
else //3 - Touch scheme
{
//get current position in world coordinates
Vector3 position = Camera.main.ScreenToWorldPoint(movement);
//My movement is 2D in X and Z, so Y is constant
position.y = transform.position.y;
//If you just set the position, the player will teleport - therfore use moveTowards
transform.position = Vector3.MoveTowards(transform.position, position, Time.deltaTime*speed);
}
}

Let’s go over the code above:

  1. Get the current scheme from the player’s PlayerInput component. This is a String, in our case either “Touch” or “Keyboard”.
  2. If the current scheme is Keyboard, then use the code from the previous article.
  3. Else:
    - get the position of the touch in our world coordinates (not camera coordinates)
    - If using 2D movement like me, set the additional axis so that it doesn’t cause you any problems
    - set the player’s position to move towards the touch position.
  4. Done!

Testing

All this is well and good, but nobody wants to test touch input by actually using a touchscreen. You want to test it on your computer, right? How to do that?

So the completely unintuitive way to simulate touch events with a mouse is to go to Window → Analysis → Input Debugger:

There, in Options, mark Simulate Touch Input From Mouse or Pen

And voilà! You can now use mouse clicks to simulate a touch and run your game :)

Space Short [10 levels, two bosses, no distractions. Can you beat it?] beta is out here, with mobile support on the web :) Would love to hear your feedback!

--

--

Danielle H

I started programming in LabVIEW and Matlab, and quickly expanded to include Android, Swift, Flutter, Web(PHP, HTML, Javascript), Arduino and Processing.