Unity - Multiple Touches on Mobile Device [Input.GetTouch]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'm going to show you how to create a script to detect and store multiple touches on the screen in unity I went ahead and created a quick circle prefab when a finger touches the screen we will spawn the circle onto the screen and have it move with the finger then when that finger is lifted off the screen that circle will then be removed from the scene the first step is to create a new script to store our custom list let's call this touch location since this script won't attach to a game object we can go ahead and remove monobehaviour and the start and update functions in this list we will want to store two values the finger ID which we use for an index and let's just label this touch ID and secondly a game object for our circle then in order to save those values we must pass them along by writing our public touch location and in parentheses we will create references for our new values let's just call them new touch ID and new circle lastly let's assign these new values to our list by writing touch ID equals new touch ID and circle equals new circle then back in unity let's attach a new script to our camera called multiple touch first let's create a reference for our circle prefab by writing public game object circle then in unity let's drag our circle prefab into the inspector next let's create a new list reference for the list type we just created by writing public list touch location then let's set the reference name two touches and unless set it equal to new list touch location we now have a reference for our list of touches in my previous videos we've used input dot get mouse button to detect either a mouse click or a single touch on the screen since we are trying to detect multiple touches we will use input dot get touch instead which will only work for touch devices the first thing we want to do is cycle through our touches we will do this by creating an integer called I and to zero then creating a while loop and in parentheses putting I is less than input touch count now input dot touch count is always equal to the amount of touches detected on the screen then we will want to reference each individual touch by using a shorthand by writing touch t equals input get touch and then we pass I in the parentheses now our lowercase T will store all the touch information for that touch including finger ID touch phase and touch position there are three different touch phases we want to focus on in this video touch phase that began touch phase that moved and touch phase that ended touch phased up again its reference on the first frame that a finger is pressed onto the screen touch phase dot ended like its counterpart its reference on a single frame the moment a finger is removed from the screen and when a finger changes its position from the last frame that is when touch phase that moved is set to true so to set up these three conditionals we start by checking a see if a touch is started so let's write if T dot phase equals touch phase that began then let's write to our console by writing debug that log touch began next let's write else if T dot phase equals touch phase that ended then let's also write to our console with debug that log touch ended and then lastly we do the same for touch phase that moved and before we test it make sure you increment our I integer so that our while loop doesn't crash our unity project since we are working with touch only we can only test this project on a touch device we could export this project to a device but a simpler and much more efficient way would be to plug our phone into our computer and then use the Unity remote app I'll include a link to the app in the description once plugged in and the app is open we just then need to connect our phone to the project by going to edit project settings editor and then selecting our device in the drop down then when we press play we can make touches on our device which will be detected in our console now that our touches are being detected let's spawn a circle where each touch begins to do this let's create a quick function that will return a game object called create circle and let's pass a touch called T as a reference then let's create a new game object called C and set it equal to instantiate and then parentheses let's link it to our circle prefab reference called circle and then let's set it as a game object this line of code creates a clone of our circle prefab onto the scene then let's modify the name of this new circle and let's set it equal to touch and at the end of the name let's put the finger ID of the touch next let's set the new circles transformed opposition equal to the touches position and then let's return this new game object although before we continue T deposition is located in screen space and not world space this will prevent our circle from appearing in the same spot that our finger is touching to fix this we need to convert the touch position to world space I cover this topic in two different videos one for prospective camera and another for an orthographic camera which is what I'm using right now to save time I'll just copy and paste the code used from that video into a function called get touch position and then let's wrap our T deposition inside a get touch position to convert the value to world space now that we have a function to spawn our circle prefab let's add our circle to our touches list we can do this by writing touches add new touch location then we pass our current finger ID and then let's create a new circle with our current touch as a parameter this will add our new touch to our list and then spawn a new circle in the position we just touched notice that our circles remain on the screen even after our touch is released so let's go ahead and remove the item from the list and delete the circle whenever it touch ends first we need to look up our touch by the touch ID we will do this by creating a new touch location called this touch and setting it equal to touches that fine and then in parentheses we use a lambda expression to find our touch by finger ID so let's write touch location and then touch location dot touch ID and then T that finger ID this basically searches our list for a touch ID value that equals our current touches finger ID now that we grab this current touch we can reference the circle that is associated with that touch and delete it so let's write destroy and then in parentheses put this touch circle and lastly let's remove this touch from our list by writing touches that remove at and in parentheses put touches dot index of this touch now in unity when we touch the screen a circle is created and then when we remove that finger from the screen the circle that was created from that finger is then deleted now the last thing we need to do is set up our circle to follow the movement with our fingers so under touch phase dot move let's find our touch again within our list I'm just going to copy and paste the same line from above now that we've located the circle associated with this current touch let's set it to the position of the current touch we can do that by writing this touch that circle dot transform that position equals t dot position but again let's convert T position to world space using our function get touch position and there you have it you now can detect and store multiple inputs on your device in a future video I will show you how to apply the script to a game where you can move a player around the screen while shooting lasers at enemies so go ahead and make sure to subscribe to this channel so you don't miss that next video [Music] [Music]
Info
Channel: Press Start
Views: 37,896
Rating: undefined out of 5
Keywords: input.gettouch, touchphase.began, unity multiple touches, unity multi touch, detecting multiple touches in unity, custom lists in unity, object following touch in unity, unity touch input tutorial, unity mobile touch tutorial, unity touch drag, unity touch move, unity multi touch drag, unity joystick, unity remote app, testing touches in unity, brackeys, unity tutorials, touchphase.moved, touchphase.ended, unity touchphase, input.gettouch unity, unity multi touch buttons
Id: 98dQBWUyy9M
Channel Id: undefined
Length: 9min 11sec (551 seconds)
Published: Fri Nov 16 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.