TOUCH CONTROLS in Unity!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
probably one of the requests that we get the most is to do tutorials on mobile games and at the top of that list is touch controls so we thought it was finally time here's how to control your game using touch input in unity but before we get started this video is sponsored by unity Pro so of you indie game developers out there unity Pro is the perfect match for freelancers or teams of two people or more when going through you get access to unity experts like three expert training sessions per month where you can get help and guidance on key dev topics along with that the pro membership gives you a free game server along with each seed priority access to a success advisor and maybe most important unity Pro comes with unity teams advanced which allows your team to save share and sync unity projects with each other for me unity teams just makes everything so much easier so if you're a freelancer or a small team who wants to take your game development to the next level I'd recommend you to check out unity Pro simply click the link in description to get started also special thanks to art Arman Michael Neely Andrew Kononenko true VR systems Alexandra Blair SEMA Eyal Cheeta 3d and infinity PPR for their support on patreon now let's look at some touch controls so the first thing that we need to do when working with mobile in general is switching our build target to do this we go file build settings and here we can see a list of all the different platforms available I'm working with iOS so I'm gonna press that and then hit switch platform if you don't have the module installed already there will be a button to download it and then install it and once it's done we are ready to pair up unity with our phone because it's just really nice to be able to test these things on the phone itself luckily unity has a cool app for this called unity remote so to set this up let's go ahead and grab our phone now I'm in the App Store here and I'm gonna go ahead and search for unity remote 5 I'm then gonna download the app here press get and wait for it to install I'm gonna press install here and when it's done I'm gonna go ahead and press open to open up the app on my phone and it will then prompt us to connect this device with a USB cable to our computer I've got it and prepared a USB cable here that we can use so I'm gonna go and plug that in and we can hear that it's now connected to my computer then step is to go into unity impressed edit project settings editor and here we want to go to the right on the device and make sure that your device is selected now this process might differ depending on the device that you're using in my case for example I'm using Windows on my computer and so I had to make sure that iTunes was installed I really recommend you check the link in the description for a more detailed guide on setting this up and now we're actually ready to test our game on the device so if I go ahead and press play inside of unity we should see that the game is now being broadcast to our phone of course our game is currently not very exciting it's just a blank white screen but I've gone ahead and prepared a small test for this so in my scene here you can see that I have a canvas and if we enable this we can see that under this canvas there's a simple UI button and as soon as I enable this in the editor it shows up on my phone and the reason why I've put a button here is just to show you that UI elements will work by default with touch you don't have to do any extra code so if you go ahead and press this button you can see that it will pop up with the text element saying that it works and that's because inside of unity I've simply added an event to our button that enables this text element so if your game is only made up of UI well then you actually don't need to do anything at all however we can of course also program other stuff in our game to work with touch here for example I have this crap sprite and currently it has nothing more than a sprite renderer and some animation now what if I wanted to be able to move this crab around by simply touching different places on the screen well doing this is actually super easy let's go ahead and add a new component and the component that we want to create is something like move by touch let's take new script and hit create an ad let's double click it to open it up in visual studio now whenever we're working with input we want to use the update method and we're actually not going to be using start at all so let's just go ahead and remove it inside of our update method we can then check for touch import and the way that we do this is by using an if statement and we want to check if input dot touch count and this is a variable that will change depending on the number of touches currently on the screen because as I'm sure you're aware the thing about a touchscreen is that you can have multiple inputs at once after all you do have five fingers so what we want to check is if touch count is greater than zero in other words are there any touches at all and if there is well then we can go ahead and get information about each touch we do this by using input get touch and here we simply give it the index of the touch so if you want to get the first touch we write zero second one is one third one is two and so on I'm just gonna get the first touch here and we can actually store the information about this touch in a variable so let's go ahead and create a touch variable let's just call it touch with an eye on capital T and set it equal to whatever input we get and now we can use this touch variable to get information about the touch so if we write touched position we get the current position of the touch on the screen in pixel coordinates we can also use touch dot face in order to get information about the current state of the touch did it just begin did it just end did it move was it stationary or did it get canceled by the device so for example we wanted to just move our object to the current point of the touch we would of course use touch dot position however touch that position is in screen coordinates its measured in pixels and the position of our object is in world coordinates its measured in units so we have to convert from screen to world space and this is done really easily by referencing our camera so we can go camera dot main to select our main camera we then do dot screen to world point and is going to take in a screen position in our case touch position and convert it to a place in our world we then store this position in an ordinary vector three so three coordinates XY and Z I will call this something light touch position and now we can simply set transform dub position so the current position of our object equal to our touch position also this is going to set our touch position on the z to the same position as our camera and we don't want that we just want this Z to remain at zero so I'm just gonna set touch position to Z equal to zero and that should fix it so if we now save this and head into unity and hit play and I'm gonna pick up my phone here and now every time I press somewhere on the screen the crap moves to this point awesome but of course if we're able to register multiple touches wouldn't it be cool if we could show all of our touches on screen and this is actually not that much harder to do inside of unity here instead of just getting the first touch we can get all of the touches and then simply loop through them and do something for each touch so let's just go ahead and remove our code here and instead will write input dot touches and this is just an array which means a list of all of our touches and just like with any array we can create a loop that goes over all the touches so I'm gonna go ahead and create a for loop here this for loop is going to create a variable called I and set it equal to zero and it's going to keep going until I reaches input touch count and every time we go through the loop I is going to be increased by one in other words we're going to loop over each touch in our array and we can of course get the position of each touch the same way that we did before by using camera dot main that screen to wall point and the touch position is now input dot touches and then we are going to get the eighth element so whatever I is at this point in our loop that is the touch that we want to get so at first we're going to get the first touch then the second then the third of which one we are going to get the position now we've converted the screen position of each touch into what position and again we can store this in a variable just a regular victor three let's call it touch position again and just like before we could go ahead and create a crab for each one and move them around I'm just going to use a handy method called debug draw line and this is going to draw a line from that just say vector three that's zero so the center of our world to our touch position and I'm going to make the color of this line red so that we can easily see it and now if we head into unity and hit play and we need to make sure in our game view here that we enable gizmos at the top to actually draw the lines and then going to pick up my phone here and now whenever I press somewhere on the screen we can see that a red line gets drawn to my finger and I can use as many fingers as I would like to all of the lines are going to get drawn pretty cool of course if we look at a real game example we might need something like a joystick to move around our character see I'm just using the same scene as we used in the shooting video that of course be a link to everything in the description and currently this character is moved around just using the keys on the keyboard let's instead create a visual joystick that can be moved around by touch and of course you can create joysticks from scratch using C sharp but I think that's overly complicated for what we need to do here instead I really recommend using this free drastic pack from the asset store at least as a base this comes with three types of joysticks and some placeholder sprites to use in your game I'm just gonna go ahead and hit import here and hit import again and we should now see this virtual joystick pack folder in our project it comes with a few example scenes and a guide and most importantly it comes with some really solid scripts and prefabs to use as a base so I'm just gonna go back to game view here and I'm gonna go ahead and create a UI canvas of course we use the canvas for all UI elements and a joystick is definitely a UI element and then gonna take a fixed joystick here and drag it under the canvas and we see everything is pretty much already set up feel free to modify any of these sprites and make them look how you want and also feel free to open up the script here and change it if it doesn't do exactly what you need and once we have this joystick in our scene we can start using it in our player movement so I'm gonna select my player here I'm gonna open up the player movement script I can see if we scroll down here to our update method we're currently moving horizontally by simply getting some input on the horizontal axis and this works fine for the keyboard and for a controller but we of course want to change this code so that it uses our joystick instead and doing this is super simple we simply need to go to the top here and create a reference to a joystick so create a public joystick and we'll just call it joystick we then go down to update method and replace this input line here with joystick dot horizontal and just like with input.getaxis this is going to give us a float value that goes between negative 1 and 1 so if you press the joystick all the way to the it's gonna be one all the way to the left it's gonna be minus one right in the center it's going to be zero and anywhere in between and in fact if we just want to try this out right now we could save this go into unity and I'm just gonna change the aspect ratio here to iPhone five wide just to make sure our pixel art still looks okay I'll then take the fixed joystick and drag it into our joystick slot I'll also quickly disable the gizmos again and now when we hit play and of course pick up our phone again we can already move our character using the joystick now I think for this game I don't actually want to be able to move a tiny bit to the sides I want to either move at full speed or nothing because I think it looks really silly when he's running quickly in his animation but almost not moving at all so in order to go ahead and change this in our script we can of course just play around with our movement variable so inside of visual studio instead of just setting horizontal move equal to joystick toward horizontal and then multiplying with our speed let's go ahead and create a few if statements here so in the case that joystick dot horizontal is greater than or equal to some value and I'm just gonna set point to so this means that we have to move the joystick at least point two in order to move our character you can play around with this it's almost like the sensitivity of the joystick when in this case we want to move right so we can go ahead and set horizontal move equal to our run speed now in the case that this isn't met so L say if we want to check if joystick dot horizontal is less than or equal to negative point two in which case we want to go ahead and move left so here we'll set horizontal move equal to negative run speed of course to move in the opposite direction and if none of these requirements are met so if it's not greater than 0.2 or less than negative point two well then we just don't want to move at all so we'll set horizontal move equal to zero whoops forgotten else there and now we should actually see this working just fine so if we save that go into unity and play again we can see that if I only move the joystick a tiny tiny amount nothing is happening however if I get past our threshold we can see that our characters snaps right to full speed and I think when playing with this it actually feels really nice of course adjust your movement speed adjust you're sensitive in all this stuff to suit your game but I think this feels really nice for mine great now we also want to be able to jump and Crouch in this game and we could of course create buttons for this but I think it would be smoother if we just used the up-and-down part of the joystick to do this so to implement this we'll go in to visual studio and we'll go under our horizontal movement and create a new variable is going to be a float let's call it vertical move and we're going to set it equal to joystick vertical then when we currently check if we want to jump here we're checking if we get the button down jump we can instead just check if vertical move is greater than or equal to some value again this is the sensitivity of our jump I want this to be much less sensitive than our movement because we don't want to be jumping on accident all the time so I'm going to set it to something like point five and now every time we move our joystick further than half way up we're going to trigger a jump and we can do the exact same thing for crouching instead of getting some button input we can check if vertical move is less than or equal to negative 0.5 in which case we want to crouch and if it's not well then we want to stop crouching so we're simply going to put else here and get rid of this if statement entirely and now if we save this code go into unity and play and now by moving up out jump stick we should be able to jump and by moving it down we should be able to crouch I can see how this allows us to fluently move around our level jumping and crouching in any way that we would like and from here on it's really up to you to implement more controls to configure your game I would recommend trying to implement a shooting button yourself you can of course either do this by using a UI button or by registering touch on a certain part of the screen everything can be done using the same methods shown in this video so from here on it's just about having fun with it that's pretty much it for this video if you liked it make sure to subscribe and bring that notification bell so you don't miss the next one also make sure to check out your new pro just click the link in description to get started and in case you haven't seen it we just did a video for the practice game jam and the game that I made for it it's something I'm super proud of he made so many awesome games for the gems so I definitely recommend you check it out if you haven't already on that thanks for watching and I will see you in the next video thanks out the awesome patreon supporters who donated in August and a special thanks to X and a player see Mariah cheetah 3d Infinity PPR 2 VR systems Sybok mommy turkey is Kirk personify thanks to long-lived assets Clinton Vince kuha Ronan Tynan Sullivan for presea Chima for the park James P Bruins cat now Kiyosaki Gregory pierce their retreat cure Swedish ski James Rogers propfan Robert Boone Anthony patent and Erasmus you guys Rock
Info
Channel: Brackeys
Views: 1,063,122
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, touch, touch controls, controls, iphone, phone, android, mobile, joystick, input, IOS
Id: bp2PiFC9sSs
Channel Id: undefined
Length: 16min 9sec (969 seconds)
Published: Sun Sep 30 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.