How to use Touch with NEW Input System - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'll be going over touch input so currently here i'm on my desktop and if i click here the character will move to that position and this simulates a touch screen so we'll also be building it and this is how it looks when you build it to your phone and we'll be using the new input system for this so we can easily simulate this on the desktop alright let's get started so the first thing that you want to do is in unity hub under your version click this three buttons here and add the modules and make sure if you're building to android you have this selected and make sure you have sdk ndk and jdk installed or if you're building to iphone and you're on mac be sure to do ios build support all right and now let's download the input system so go to window package manager go to packages up here and go to unity registry and scroll down until you find the input system or you can search it up here and then you can just click install and now it will install the new input system and when the pop-up comes up just click yes to restart the editor so right here just click yes alright and now we have the new input system installed so let's go ahead and make an input action so go to scripts or whatever folder right click and create a new input action all the way down here and let's call it touch controls and i have numerous videos on the input system if you're interested in learning more alright so in our input action let's add a new action map and let's call this touch so this is just a set of controls and then our actions are our controls and so here we can press f2 to rename it and we can call it touch input and so i'm going to set the action type to pass through and so you can see your password just means that it doesn't have a disambiguation process so it doesn't have the concept of a specific control driving the action so you can connect any control and use them at any time and it will just work with that control instead of having a main controller alright and then i'm going to set the control type to any so i can show you all the options so then under no binding under path we can go to touch screen right here and you can see we have so many options here so the main ones is delta so that's the change in position between the previous frame and this frame and that's good for getting the direction of the finger we also have the position of the finger we also have if you pressed on the screen we also have the pressure of the touch and then we have a primary touch along with a bunch of other touches so we have touch 0 touch 1 touch 2 all the way until touch 9 so it supports 10 touches at the same time so 10 fingers so let's go ahead and look at one so you can see in one touch we have similar parameters we have the delta we have an indirect touch which just means that it's not the finger touching the screen but maybe it's one of those pens that are touching the screen we have a touch base which highlights what phase the touch is on whether it's started moved or finished this is the position of the current touch this is whether there is a touch contact so is the finger touching the screen right now this is also the radius of the touch so when you touch down your fingers kind of in a circular shape so it gives you the radius of that and then you have the start position so this is when the finger started touching the screen we have the start time which is what time did the finger start touching the screen this returns whether it was a tap this is kind of like a button we also have the tap count how many times it's tapped and we have the id and you can do this for any finger you can also just select the whole touch itself so you can select primary touch to get all of the information regarding that finger alright so i'm just going to select the primary touch for the touch input here and i'm going to select another one so our goal is to press on the screen and have a character move towards that point so we want to measure when we press down and we also want to measure the position of where we pressed and we can set the action type to pass through and button will work here for the action types since this is kind of like a button that we're pressing on but it's a screen instead and then for our path just go to our touch screens and for our primary touch you can see that it limits the actions for the control type that we selected in our case button so right here you can see that we have touch contact which means that our finger is contacting with the screen that we're touching the screen so let's select that you can also add an interaction so let's add an interaction and you can add in hold multitap press slow tap or tap so this means that the action gets executed or performed whenever these conditions are met so for example we put press then we have a trigger behavior here which is press only so this is triggered this action is triggered when we press down we can also trigger it only when it releases or we can trigger it when it presses and when it releases you can also change the press point which is kind of like a threshold of how much the player should press down before it actually performs that input so right now it's a 0.5 which is good it returns one of its pressing down which it triggers the behavior and it returns zero if it's not pressing down and it does not trigger the behavior and you can actually change this in the input settings here and you can create a new settings asset and you can change those values here for example the tap radius default tap time the dead zone minimum which dead zone is more for controllers all right and then let's add in another one and let's put touch position and for the action type let's select pass through and let's select vector 2 and then go to your binding path touch screen and for our primary touch let's just select position here all right so now that we have our actions we can save our control scheme and then go to your control scheme and press generate c-sharp class and click apply and so now we have a script that we can call from other scripts and i'm just going to right click and create a new c-sharp script and call this input manager so it manages our input and you can see this is the generated script from our touch controls all right and then you can delete these two using statements and i'm just actually going to delete all the functions here so i'm going to show you how to use the input action directly and i'm also going to show you how to use the touch api which you don't have to make an input action for it's just an api that you can call through script and depending on what you like best you might choose one or the other alright so first we need a reference to our new input so private touch controls touch controls let's make an awake function and let me just zoom in here a little bit alright and then we can say touch controls equals new touch controls then we have to have an on enable function so touch controls dot enable we need to enable our input and and on disable we need to disable our input all right and let's make a start function now and here we want to subscribe to the event when we press down on the screen so we can do that with touch controls dot our action map which is touch dot our action which in this case is touch press then we can say dot and here instead of performed let's actually put started so we started pressing down and then we want a reference to our context so plus equals context equals and then we have this greater than sign and then let's just call a function that we're going to make start touch so this is just the syntax for subscribing to an event and also being able to pass in the information from that event through to our function which in our function we can just input our context all right and so we're gonna want to import the input system so at the top go to using unity engine dot input system and then here you can say private void start touch and for our parameter let's just call our input action dot callback context and we can just call that context and let's do the same but with end touch so just copy that and instead of started put cancelled and end touch instead of start touch and let's just copy this function here and just change the name to end touch all right so this context will return the information for our touch so let's just debug.log some statements here and so you can see we put context dot we have some parameters here so we can read the value as you normally could so you can put dot read value and then since this is a button we can just read in a type of float and that will get either one or zero in this case we already know it's one since we're pressing down and that's why we call this function you can also get the phase that it's on you can get the start time and the time which is when the event was executed here time but in this case we can just read in our position so i'm just going to add here touch controls dot touch dot touch position dot read value and let's read in our vector 2 touch position and this will return the touch position in screen coordinates all right so let's actually test this in the game so right click and create an empty game object and let's call this manager and let's add in a component called our input manager right and then one thing we want to do is go to window analysis and then input debugger and then in the input debugger go to options and put simulate touch input from mouse or pen so this will allow you to test whether your touch works in the editor and this only works for one finger because you only have one mouse so if you wanna test multiple fingers you're gonna have to build it to your phone alright so when i press down you can see that the touch has started and the touch ended so let's press down touch started i release it over here touch ended and you see it's a different position all right so what i like to do is actually fire events from the input manager so that other scripts can subscribe to it and get those events so let's just make some events here so we can say public delegate void and delegate just means we're letting another function handle the functionality or another script handle the functionality and let's just call this one start touch and we're going to want to pass in our vector2 position and the time that it started of course you can customize what parameters you want to pass into other script and then we actually have to make a public event of that start touch so this is what we'll be able to call in this script and we can just call this on start touch and i'll show you how to use this in the script alright and let's just copy that but instead of start touch let's put n touch in here on and touch all right so we're going to be calling this event which is of type start touch and we're going to pass in those parameters to delegate the functionality to another script and i just realized i named this function the same as this so i'm just going to call this start touch event instead and touch event all right and then in your start touch you can just say if [Music] on start touch does not equal null so this just checks that if any scripts are currently listening towards this input because if no scripts are listening then why would we want to send out an event to no one right then you can call the actual function on start touch and you can pass in the position along with the touch time so let's just cast this to float and we can say context dot start time all right and same with the other one just copy that in but instead of on start touch we want to do on end touch same with here and then instead of dot start time we can just put dot time which is the current time and you can actually get the difference in time by subtracting this time from the start time if you wanted to measure the time between you started touching and you ended touching all right and then let's make another script to subscribe to these events so let's call this one test touch and i usually like to make a singleton of our managers so i can just reference them in other scripts very easily or you can just pass in the reference directly so i actually have a video where i made a singleton class and so i'm just going to be using that singleton class for this video so here i have a simple singleton class and then for the input manager i can just say singleton and then input manager and i want to make sure that the input manager runs before any other script so we can actually do a default execution order so this is the order at which the scripts are executed and we can put it at a negative number so let's just put this at negative one and you can actually go to edit and then project settings script execution order and you can change it there manually you can see that the negative numbers are at the top which means they'll be executed first and any new script will be added below this so if we put it at negative one we can ensure that it'll run before any other script that we make all right then in our test touch let's just remove these two using statements and let's get a reference to our input manager input manager input manager i'm just going to make that private then in our awake function we can just get a reference input manager equals input manager.instance so this is our singleton instance now we have to have an on enable script so we can subscribe to the event that we shoot out so input manager dot on start touch let's just call move same with on disable we can say input manager dot on and touch minus equals move so here we're subscribing to the event here where unsubscribing when the script is disabled let's just have a public void move function here and we want to take in the parameters of vector 2 screen position and our float time so the first thing we want to do is convert our screen position to world coordinates so we can say vector 3 screen coordinates because we want to convert a vector 3. now we can say new vector 3 let's put our screen position x our screen position dot y and this is really important but for the z axis you want to put in the distance the camera has from the plane that you're trying to touch so we're going to set it up so that we're going to be using the near clip plane from the camera so we can just get a reference to the camera here private camera camera main and then in our awake we can say camera main equals camera.main so we don't have to keep finding the camera every time and then here we can just say camera main dot near clip plane if you don't put this value incorrectly you're gonna see that it's gonna move to another position when you click on the screen so make sure you have that correct and so let's convert this to world coordinates so vector three world coordinates and we can say camera main dot screen to world point and let's just pass in that screen coordinates that we made and then i'm just gonna set the world coordinates dot z to zero so the character stays on the plane and then we can say transform.position equals world coordinates all right so i'm just gonna add in a cube here i'm gonna add in our test touch to the cube as a component and you can see this is the 2d screen i made a red material right here so that it can be seen easier and then for our main camera we want to adjust the clip plane so currently this is a 2d game right so our position is z at 0 for our cube and then for our main camera let's zoom out here let's make sure that our clip plane is at the correct position so you see that right here we're at negative 10 so then put your clipping plane at 10 to offset it and you'll see that it clips right where the box is at and you can kind of adjust the values so that it doesn't completely clip the box this works better when you actually have a 2d sprite rather than a 3d cube so 9.99 seems pretty close in this case if you have a sprite it doesn't mess up like that you can see that now when i click around the screen the cube will follow my mouse position awesome and so let me show you some other touch features here so as you may remember we added this touch input here with this pass-through value and touch control type and i found it really difficult to actually get that control type from there so i recommend getting it directly through script especially if you're going to be polling those actions frequently because unity actually recommends using the enhanced touch api for polling so i'm going to show you how to quickly use that api instead of the input actions if you're interested in using that api for certain things and you want to use the input action for other things so up above we want to import that name space so we can say using unity engine dot input system and then dot enhance touch and so if you want to enable the touch simulation through code instead of going through the input debugger you can do touch simulation dot enable here so this will enable you to test your input in the editor just like we activated it previously and we also need a touch simulation dot disable and so to mimic what we did previously we can do unity engine dot input system dot enhance touch dot touch dot on finger down so on the finger down we can subscribe to an event so let's just call this finger down and we're not going to be taking any parameters and you can actually do this in the enable function and you can do the same for disable except put minus and then equal so we can unsubscribe from it and so let's do a private void finger down and so this actually takes a finger finger and i'm just going to call that finger and so from your finger you can get several attributes so finger dot we have the current touch we have the index is it active the last touch screen screen position and touch history and so i'll link the documentation down below but here are some of the properties that i mentioned before and they have the descriptions of what they do here but if you wanted to fire the event here for example we can erase these parameters and we can pass in finger dot screen position here and since this function is called when we press down we can just pass in time.time which is the current time all right and then some other cool things you can do are get the list of all active fingers so unityengine.inputsystem.enhancetouch.touch.com now we can do active fingers and we can also do active touches so active fingers returns an array of fingers and active touches returns an array of touches and i'll just put a debug.log statement around that another thing you can do is track the phase of your touches so you can say for each touch i'm just going to copy it for each touch in active touches then we can do some comparisons here so we can say if our current phase touch dot phase equals touch face dot begin then it will print true you can see here that it's throwing an error because it's defined both in the input system and in unity engine so we have to specify which one we want to use by adding in the name space in front of it unity engine.input system dot touch phase dot begin and so here you'll see that we have several phases we have began moved ended cancelled or stationary so if you want to do some cool stuff with your movement you can check what phase it's at and depending on the phase you can fire certain events so they are both very similar it's up to you which one you want to use i would recommend using the enhanced touch api for polling which is what unity recommends and the input system is good because you can easily change the controls and have it regenerate the script with you the pros about the input system here is that it's very easy to change the controls so for example touch press if we wanted to switch it to something else we can just easily put release only press only instead of having to do that in script so i wanted to provide an introduction on how to use touch with the new input system if you want to build to your phone then we can just go to build settings file build settings and switch to your platform so in my case i'm using android so i just select android and press switch and you have to make sure that your phone has debugging enabled and it depends on every phone but you have to go into your phone settings and enable developer mode so that you can connect your phone to the computer and the computer can send information to your phone all right and now once that's switched over go to your player settings and under player you have to scroll all the way down to publishing settings and if you don't do this it'll probably throw an error you have to go to keystore manager and you have to create a new key so create a new key i'm just going to put anywhere and save it right there and so you can just put in a password has to be six characters long and you can create a new key after you put in an alias so i'm just gonna put sam and you have to put in your passwords again and just put yes so this is to provide authentication for your app and so you'd probably want to put a better password if you're actually publishing this to the app store and more information because the keystore identifies you as the developer of the app and if you lose that key then you might not be able to update the app anymore because it would be unauthorized usage all right and then you can just build and run i like to create a new file here called builds and i'm just gonna name it touch input test make sure you have your phone in debugging mode and that it's unlocked and you actually want to make sure that you add in your open scene here so make sure you add your scene and then you can build and run and you can also in your project settings under resolution and presentation choose the default orientation and you can choose the orientations that you want the player to rotate the phone in alright and now when the game is built on your phone you can see that now we can click to move and the box will move depending where we touch the screen so yeah i hope you enjoyed thank you so much for watching this video and so i want to thank my patrons thank you so much for all of your support we actually reached our first goal on patreon which will be a q a which is awesome and so i've actually made a google form for the q a and i'll put it down in the description if you're interested you can ask me any questions um try to keep it not very personal and the form will close on the 27th and i want to thank my new patron in the dedicated tier ironclad vr thank you so much for your support i really appreciate it it helps me so much to make these videos and i'm looking forward to do the q a and if you haven't already be sure to join our discord channel where you can ask for help or you can post memes or just chat so i hope you enjoyed and see you next time [Music] [Music] you
Info
Channel: samyam
Views: 49,114
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: ERAN5KBy2Gs
Channel Id: undefined
Length: 21min 55sec (1315 seconds)
Published: Wed Dec 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.