How to use TOUCH with the Input System in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'll be giving you an introduction into unity's input system or touch and this works exactly as you see here on mobile as well and so what you see here is that we are simulating touch in the unity editor and then once we're ready to build we can directly build to our phone and it will now work with our finger instead of a mouse and thank you to Unity for sponsoring this video and so for this we'll be using the new but not so new input system and to do so we can go into the window package manager then we can go to Unity registry at the top and we have to download the input system package which you can scroll down until you locate input system which currently it's at 1.3.0 at the time of this video and click install or you can use the search bar up here to search input system which filters out the packages and make sure here to click yes to restart the editor and this will make the input system apply the new changes and so why do you even want to use the new input system well I do have a whole video just on that topic but there's two main reasons that I use it one of them is because it encourages cross-platform compatibility of controls so you can define an action and it can now work on two different devices let's say your phone and the computer and maybe even the PlayStation 5 so you don't have to write code for each one of these consoles you just have to write the code one time and the second one is because it is event driven meaning that it shoots off events in response to certain actions that have been performed and so now you don't need to do everything in the update method anymore which is really neat because I hated having the update method be cluttered with tons of if else statements so I'll show you how to do that now to start let's make a scripts folder so right click and create a folder and I'm just going to name it underscore script so it can be the first folder and you'll want to right click and create an input action which is the last option input actions and so this is going to be basically your directory of actions and so I'm going to call this touch action and a action is just a certain control so let's say you want the player to jump that will be one action running will be another action and within the action you'll have controls that trigger that action so the first thing we need is an action map so let's create an action map and this is just a set of actions so let's just call this touch and it's neat because you can actually disable and enable different maps for different circumstances so you might want a set of controls on one scene but not on another scene and so here are the actions and each action will have a control that it is bound to so let's say we want our player to move to a certain location well you can select this action and press F2 and rename this to move or add one here instead and name that move which I'm just going to right click and delete this one so now you have a move action which will be able to query in our code and see whenever the player has pressed the control that is bound to our move action so for an action we have a type and it can have multiple types you have value button or pass through button is simply if it is well a button and you press it once and it returns one value similar to a key on the keyboard the mouse button even pressing down on the screen can be considered a button and we have value which allows you to select a control type and the control type is what this action will return so right now we have it selected as a button so it will return a type of float axis will also return a type of float however axis is on a scale button will only return 0 1 or negative 1 well this will return a float value depending on the control that you're using we can also return a vector 2 for something such as a joystick or even the wasd keys or you can just keep it General and select any and have the control itself derive what type it will return so as an example if we select here no binding The Binding is the actual control itself we can select the path which you'll see there's numerous options here but we'll want to go to the touch screen option and you'll see now here are all of the different controls that we can assign to our action we can assign the Delta which you see has an X and A Y so the Delta returns a vector two we can assign the position press so this is a Boolean and tells you if a button has been pressed you can return the pressure of the press and now there's something called primary touch the primary touch is the finger that's driving this car current action so if you only have one finger on the screen that will be the primary touch and if you scroll down you can actually see now you have touch zero Touch One Touch two touch three all the way to touch nine so it supports up to 10 fingers on the screen which makes sense and so you can have actions for all sorts of fingers in our case we only want to do one finger and we'll see that for each finger you can get the Delta indirect touch which means if this was either a finger or some sort of pen that touched the device the phase that the touch is on so touches have phases whether it's started whether it's moved and whether it has been finished the current position and this is returned in screen coordinates whether the touch has contact with the screen the pressure the radius of the touch the start position so when you started pressing on the screen not where you currently are but when you started pressing on it when you started pressing on the screen if you've tapped on the screen how many times you've tapped on it and the actual ID of this touch so every touch has an ID associated with it so you can keep track of the fingers and whatnot and so you can look at these more in depth in the documentation which I will link down below you'll see here all of the controls the types and the descriptions of these controls but for now what we want to keep track of is the position of our touch so in this case let's go back to the action and select our control type to vector2 and if you're wondering the difference between the value and the pass-through there's not much difference up front and I have a whole video explaining this but basically passthrough does not perform any processing on the input though if you have multiple fingers on the screen it will send all of that input into your action whereas value you have a driving finger or a driving control that determines the output of an action so in this case with our move we only really want one finger we to drive the move action so once we've selected that and with Vector 2 we can go to no binding and now go to touch screen and actually select right here position and two other quick things we have interactions which basically determine when an action is being performed so if you want the section to be performed once you've held down a certain amount of time you can put hold you can do a press which is just pressing down a slow tap so tapping is just quickly tapping on the screen and then there's also tap and then processors add some processing to the value that's outputted from this action and I have like a million videos on all of this explaining really in depth what each of these mean which are in the description and I'm just going to rename this with F2 to touch position and another action that I want to have is touch press so once we press on the screen the path will be here touch screen press single touch we don't want multi-touch here we only want this to be triggered once we press with one finger and you can also search up here for an action or if you have the control you can press listen and if I press the space bar for example the space will show up here you can also add multiple bindings which are the controls themselves to an action and you can also add modifiers which I do have another video on okay so now we have our two main actions and I just want to make note that as a control type you can also return a whole touch control type which is very interesting that lets you see all of the details related to a specific finger but I will put that back to Vector two so once you're done make sure to save the asset you can also click autosave here so it saves it automatically and we can exit out of that now to actually use this in our script we're going to want to right click on the hierarchy and create an empty game object and let's call this the touch manager and now in the inspector we can add a player input component which is included in the input system package and now we can just drag and drop our input action assets into the actions parameter here or you can select this little circle and search for it the player input component is basically a wrapper around the input system that gives you a set of helper functions and just makes it easier to use the input system without having to write so much code up front and you'll see here we have a default map so the map that's default and we only have one so it's touch there's different ways how to use the player input component I do have another whole video on this I know I am repeating that quite a bit but if you do want to learn more in depth I do recommend looking at that specific video which is how to use unity's input system and these are different ways on how to read the events being triggered from the input action asset in this case I recommend creating a script and doing it yourself just because it gives you more control so let's call this the touch manager script and so the first thing we want to do is erase all all the nonsense that we don't need and then we want to import the using Unity engine dot input system and now we want a reference to our player input component so we want a private player input let's call it player input then in an awake function we can get a reference to that so player input equals get component player input right and now basically we want to get a reference to our actions in the player input now you can do that each time however I like to make variables for them so private input action and we can call this the touch position action and we also have a private input action and we can call this the touch press action and so then in the awake function we can do touch press action equals layer input dot actions which is our list of actions and you'll see that now we can have some parameters here such as action Maps which return the maps associated with the action you can add an action map we can use find action if you'd like we can find our our action whatever you named it and it is Cap sensitive so make sure you spell it exactly correct so we called it touch press touch press adversely you can also do this if you like to go more wild and old school which is how I like to do it and then we can copy that and now do that for the touch position action and now do touch position and so now we have a reference to our actions if we want to be notified on when these actions have been pressed either we can in the update function check on each frame if a value is being pressed and read the value or we can use events and I'll show you how to do both of them in the case of events we can have an on enable function and an undisable function because we want to make sure to unsubscribe from events when the script is being disabled so we don't keep getting notified of things when we can't do anything about it and so let's say 4r touch press action we want to be notified when it's performed so there's three callbacks there's the started callback which is when the action has been started there's the performed callback when the action has been officially performed in the case of a button that just means it's been pressed down and so the started and performed callback will be called at the same time however if you have an interaction on this action the performed callback may be called at a later time there is also the canceled callback so whenever the action has been finished or canceled AKA I lift my finger up from the button that will be called so we can do performs and then subscribe to this event which is plus equals so basically we want to listen to this event we have our ears open and whenever our ears have picked up a sound from this event we want to call a function called touch press which we don't have defined so we can do private void touch pressed and you'll see it's not working because we need to take a type of input action.com back context context and this is just information regarding our action that has been performed and we can actually query this to read the value and find out more info and we can just copy this and put it on disable and replace the plus with a minus so that means we want to unsubscribe from this event and stop listening with our ears or unity's ears or the scripts I don't really know where I'm going with the ears part all right and so now this function will be called when the action is performed and so you can use this touch press action directly we can do context and get more information regarding the action in this case usually what you'd want to do is read the value of this action so there's several functions for that you can do read value which is what I usually do so for example you can read the value and pass in the type so I know it will return a type of float and so now I can equal that to a type of float value and and we can read debug.log this value however if you don't know the type up front they've actually added since my previous video Read value as button which will return a Boolean basically telling you if this value is pressed and then we can do read value as object which doesn't require you having to know the type at runtime however it does allocate Heap memory so I just recommend passing in the type directly if you know up front what the type is so let's test this out in the editor but before we actually test this we need to enable touch simulation in the editor to simulate touch so you can do that by going to window analysis input debugger and then under options select simulate touch input from mouse or pen and you'll see that now we have a simulated touch screen under the devices and you can actually double click that and see all of the values that are being performed at each frame so you'll see that my position is moving because my mouse is moving and I do have a video on the input the bugger as well wow and so before we Press Play Let's also add in the script we just made which is called touch manager we can just add that to the touch manager game object so it reads the player input component attached to it and you'll see that once I press on the screen one is being printed out which means that it's correctly getting my touch alrighty that's great and all but let's actually have some functionality here so that once we press on the screen our character will move to the location now you don't necessarily have to use the touch pressed at the start you can just do this touch press position action dot performed plus equals and your function however I found this works best if you first see if the screen has been pressed before querying the position so in that case here where we pressed on the screen we can now do touch position action Dot read value and we can read it which is a vector 2. we can do Vector 2 position and so this does return a screen position value basically it's something like this where the 0 0 is the top left and the bottom right is the width and the height of the pixel similar to 1920x1080 but that doesn't correspond to the world position so we want to convert whatever we're tapping from screen to World coordinates very simple with that we can do camera main so get our main camera and you might want to store this object camera dot main if you use it frequently and then we can use the function screen to World point and we can pass in this screen position that we've read which I will just delete this line actually and now we can just equal that to Vector 3 position and this might actually make the Zed kind of funky so I like to set the Z position to what it was before which in this case we don't have a reference to our player if we scroll up we can just get a serialized field private game object player or we can just get the transform of the player as well and we can do player.transform.position.z to make sure the Z value the depth isn't changing of the player then we can just set the player.transform.position equals to the new position awesome and so for the main character I got it from this free asset pack on itch.io link in description pretty cool go support them for now I'll just right click create a 2d object Sprite Circle and this will be our player then in the touch manager we can drag in the circle as our player and we can press play and so once we press you'll see that now the circle will go towards our Mouse awesome for a quick overview on how to use these values on the update method well it's very simple let's say for example with the position you can do touch position action Dot and similar as before you can read the value on every frame and it will return a vector 2 in this case or if you want to tell if this action has been pressed on the frame you can do was press this Frame there's also was release this Frame so this is similar to the canceled callback and was performed this Frame so if the performed callback was called on this Frame then this function will be called so if you wanted to you can do like if the touch press action was performed this Frame then we can now do the same thing that we did here and just copy it there very simple however I like this way better because it's much neater and now there's not this in the update method which this update function will be called on every frame and so if you want to build this to Android you'll have to have the Android packages installed so in the unity Hub you can go to install find your version or here for example you can go to the selection that you want and put install with unity Hub and it will open then backing into the Hub you can select the gear icon next to the version atom modules and select the versions you want to install and this case I already have Android installed you just install that pretty easy or if you want iOS you select iOS click continue and Go with downloading that and then you'd want to click on your platform and click switch platform and I do have a video on how to build to Android on how to make your phone setup work with this because it's quite an involved process and a little out of scope for this video and so once you have switched the platform make sure to select development build so you can actually build this without using keys which the keys are used for app stores so that they can verify that the application is yours and that it's not stolen and then once your phone is connected you can select build and run I usually create a new directory here called builds and then I just save the build there then I just call it something and save the build and now it'll start to build it to your phone and make sure that your scene is added in the scenes in build up here and for some reason if it doesn't work got right I had to go to run device and select my actual device and then click patch and run don't ask me why and so now you should see something like this where you can tap the screen and the circle will move around now two quick things I want to mention first of all if you want the game to mimic the phone resolution you could go up here to free aspect and select a 16x9 portrait aspect and now it will mimic a phone resolution you can make sure everything is aligned perfectly and secondly there is another way to control touch called the enhanced touch so you see there's the touch screen which is what we're using and then there's the enhanced touch which provides enhanced touch support and basically with the enhanced touch you can get information by finger and by touch so if you want more in-depth information on your touches the enhanced touch might be a way to go for a quick introduction on how to enable it first we need to import the unity engine input system dot enhanced touch touch and I like to equal it to using enhanced touch because if you have the input system package it has trouble differentiating some of the objects on the enhanced touch to the input system and you have to write out this each time so if you do it like this it's much easier for example we can just do enhance touch Dot and then call one of its functions or objects so when the script has been enabled we need to enable the touch simulation we can use this on the editor and we also have to enable enhanced touch support on disable we disable the touch simulation and the enhanced touch support and then it's quite simple it's the same concept you can do enhancetouch.touch Dot and you can do on finger down and you can have a function which takes in a type of finger here and with that finger now you have access to more information here so the index last touch the screen and the screen position and the touch history so you'll see that there are some extra options here so we have current touch and then with the current touch now we have even more information here so we have the Delta whether it's in progress whether it's a tap the phase that it's on so there's different phases similar to the other input system touch class green position start time tap count if it's valid or not if it's ended and so with this you can do a lot of processing with the fingers and the touches I'm not going to go in depth on this right now but the documentation is down below and all you need to know is that we have these things that we can subscribe to you can get the active fingers the fingers the events and if you want to use this in the update method you can in a similar way you have access to the fingers being pressed you can do something like four each we can do enhance touch dot touch you can do touch in the enhancetouch.touch DOT active touches now with each touch you'll have access to all of these data points and you can do touch.phase for example and you can do some sort of comparison so so if the phase is equal to the enhanced touch dot touch phase is equal to the touch phase dot begin canceled move or stationary then perhaps you can do something whatever your heart and your game desires and so you'll see here we actually have to use the input system.touchphase dot began because Unity engine has something called touch phase which is quite confusing but that's okay so I hope this video helped you out and if it did please like And subscribe and comment down below and thank you to Unity for sponsoring this video I'd also like to thank my patrons for the support they make these kind of videos possible if you're interested the link is in the description I offer source code Early Access and exclusive content and if you haven't already make sure to join our Discord where you can chat post memes or ask for help I'll see you next time foreign [Music]
Info
Channel: samyam
Views: 55,984
Rating: undefined out of 5
Keywords: unity game touch controls, unity touch controls tutorial, unity touch input tutorial, unity mobile touch tutorial, touch input unity, new input system touch, unity touch input system, touch new input system, mobile touch controls unity, touch input mobile unity, touch controls unity, input system touch, mobile input system, unity new input system touch controls, new input system touch controls, touch controls in unity, touch controls, enhanced touch, enhanced touch api
Id: 4MOOitENQVg
Channel Id: undefined
Length: 24min 10sec (1450 seconds)
Published: Mon Nov 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.