Moving via touch
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.
Note
For more info on the Touch struct, check out https://docs.unity3d.com/ScriptReference/Touch.html.
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...