detect Swipe NEW Input system ( touch / mouse ) | how to detect swipe in unity with New Input system

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to this video um so in this video we're gonna learn how to do swipe detection so uh right here I have this Cube which I can move with arrow keys so uh here I've set up this input action that is a vector 2 value and it has the arrow keys as its bindings and here in the code I have the input action which has the vector 2 value and I subscribe to that event and I start this core routine with the vector 2 value that I get from that and now we can do the same thing with swipe so let's create a script called swipe detection so we want to be using the unity engine dot input system and here we're gonna have two input actions we want to know the position of the touch and we want to know when it is pressed and released and in the awake method we're going to enable them so enable and we're going to store the value of position in a vector 2 field called current position so this is going to be position dot read value Vector 2 and we also want a vector 2 for uh storing the starting touch position so let's call it initial position and when we start getting the touch so press that perform is when we're going to say initial position is equal to current position and when we stop getting the touch we're going to detect swipe so we're gonna need a detect swipe method and we're going to call it here and in our detect swipe we're gonna have a vector to Delta which is going to be the direction of our swipe so we want to get the direction from the initial position to the end position so we're going to subtract current position from we're going to subtract the initial position from the current position which is going to give us the direction going from this to this and then we want to check if uh the touch was long enough to be considered a swipe so we're gonna have we're gonna have a float called swipe resistance and we can set it to be 100 and this is going to be the swipes threshold so if our touch has moved longer than that then we can consider it to be a swipe so we're going to say if Delta dot X is longer than our swipe resistance then we have swiped on the x-axis but since uh Delta that X is going to be negative if we have moved our touch towards left we are going to get the absolute value of it so math function dot absolute value and if this happened then we are swiping either left or right and we're going to store that in a vector 2 Direction which at first we're going to set it to be zero and here if we detected the swipe on the x-axis we're going to say direction.x is going to be either minus one or one so we're going to do math function dot clamp the Delta dot X to be either minus one or one and we're going to do the same thing for the Y so we're just gonna change X to y and now we have the swipes Direction in this uh Direction vector 2. and for its usage we're going to fire up an event so we're gonna have a delegate uh void swipe and this is gonna have a vector 2 Direction in its parameters and now that we know how to call those functions we're gonna have uh an event called swipe performed and down here we're going to check if we actually detected a swipe here so if direction is not equal to Vector 2.0 and if swipe performed is not null then we're going to uh send this event with the direction as its uh data and to make it easier to reference this in other scripts we're going to create a static instance so instant and that is going to be instance equals this and here in my Cube movement I am um starting this core routine with arrow keys and now I can do the same thing with swipe I can do swipe detection dot instance dot swipe performed and then the rest of it is just like that we can copy and paste it except we don't want to read value Vector 2 because we're passing a vector to type strictly all right now we need an instance of this script in our scene so I'm going to attach it to my main camera and the position is going to be a value and we're going to pass a vector 2 and for the Press it's going to work like a button and the interaction is going to be press and for the bindings we can add the touch position and for the Press we can add the touch press and now we can swipe the screen and it works as expected and as you can see if I do like very small swipes that does not register as a swipe and it has to be long enough and uh also if we swipe kind of diagonally it's still going to register for the axes in which we swipe longer but if we swipe perfectly diagonally like that it's going to uh detect that diagonal swipe as well and as you can see my cube is doing weird stuff because uh it was not supposed to uh roll diagonally but as you can see it's working all right guys I hope this helped you all out if it did give it a thumbs up and uh subscribe please thank you
Info
Channel: Game Dev Geeks
Views: 8,644
Rating: undefined out of 5
Keywords: how to detect swipe in unity with New Input system, swipe detection in unity, swipe new input system, touch swipe unity new input system, how to swipe unity tutorial, swipe detection, unity new input system, touch swipe new input system, input system tutorial, touch unity tutorial
Id: Xm9_rcmv3UU
Channel Id: undefined
Length: 9min 21sec (561 seconds)
Published: Fri Dec 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.