How to Get Smooth Movement in Unity's Input System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i'm going to show you how to smooth values in the new input system so for example usually when you press a key on the keyboard it returns either 0 or 1 but let's say for your character you want to smoothly interpolate between your input so that you can smooth out either the movement or the animation for example if i start running you'll see that it'll start off slowly and then go into a full sprint and this is because we are interpolating manually in the code so unfortunately the new input system does not have a way to directly do this like the old one for example in the old one you can just call get axis and even if you're on your keyboard it will kind of interpolate the value for you so it'll smooth out that value however for the new input system we'll have to do that manually in code so to better show you what's going on i've downloaded this visualizer package from window package manager input system and then under the samples visualizers and currently it's using this move input action which is of value and control type of stick so right now it's bound to the left stick on a gamepad and d-pad which is bound to our keyboard wasd so if i press play and i press wasd you'll see that it goes from zero to one rather quickly and you'll see that when i'm pressing on two keys diagonally it'll stop at point seven and point seven and this is because this vector is normalized and normalized just means this has a length of one and normalized is good for directional vectors so you're going in a certain direction so it doesn't interpolate for the keyboard but if we use the gamepad now you'll see that it does track all of the movements on the joystick so with the gamepad you're gonna get these smooth value because it does return all of the movements on the joystick so to smooth this we can do this manually in code quick thing i want to mention is that for these wasd values that we're using it was normalized so if you don't want it to be normalized you can just put digital instead of digital normalized digital just means it's either zero or one whereas analog means you get the full range of floating point values which in this case for a keyboard it's gonna still be either zero or one because it's a button that we're pressing we're either pressing it or not but that's basically the difference between these three alright so in my third person scene i already have a player controller set up with a player input component and you'll see that what i'm doing here is in the update function i'm reading our move action which is a vector2 with wasd and gamepad and then i'm putting that into a vector 3 and then i'm moving the controller i'm using the player input component and make sure you're using the input system namespace so you can read the value but this isn't going to be smoothed out especially with the keyboard so before passing it into anything we can smooth it manually and there's a couple ways to do this you can either learn the value slurp the value or do a smooth damp now personally i like this move damp because it kind of gives a feeling of accelerating at the beginning and decelerating at the end when you slow down so for example here's the documentation it gradually changes the vector towards the desired goal over time and you can use this for a vector 2 vector 3 or a float for example if you want to use it with a float just use math f dot smooth damp and it's pretty similar in terms of the parameters it takes in sofia homer on twitter showed a quick example of lerp versus slurp larping is just linearly interpolating as so same speed while slurping has more of a gradual increase and decrease and this reddit post describes the difference between lerp and slurp you can see that the lerp is over time while the smooth damp is accelerating and decelerating smoothly the slurp looks different because it's mostly used for directions and not for positions so in this case we'll go with smooth dam so now we can smooth the input if we do vector two dots move dam you'll see that it takes a current and a target so we have to keep track of our input so let's just put a variable up here and we can do private vector2 current input vector copy that name and we can just put that one as the current so that's our current our target is our input that we're reading now then we need a reference to the current velocity which we need to pass it in here and all that does is populate that current velocity with the velocity of this smooth damp so for example up here we can just declare another variable private vector2 smooth input velocity and copy that and then back here you can just put ref smooth input velocity so we're passing in a reference and this will get populated within this smooth dam function we don't really need to use this but we need to pass it in and then we can pass in a smooth time and the maximum speed so smooth time is basically how fast you want this to smooth so we can just call this smooth input speed and then if you want you can pass in a maximum speed however i know that this input is getting clamped at one so i know that the maximum will be one so i can just erase that so let's declare this smooth input speed at the top i like to make this a serialized field and do a private flow smooth input speed and we can just give it a value of maybe 0.2 so now we can change the sydney inspector and play around with the values so now that we have this smooth damp function we have to actually equal it to something so we can get this current input vector and equal it to that so this is basically storing our current input for the next time that we use it and now instead of using the input here as we were doing previously we can replace it with the current input vector and it will be smoothed over time so now in the editor i have this player script attached if i press play i've put the speed to zero just to see so if we run we'll see that it does look like it's damping a little but that's because of the animation if i put it to zero you'll see that it doesn't look very slow at all and i'm printing out the values down here so you'll see that once we press a value it switches immediately to negative one or one but now if i put the value to point two for example for this move you'll see that if i press a it'll go to the value smoothly over time and it generally looks more smooth and so you can do this for your input and both for your animation so once you have smooth input and smooth animation your character will look much more natural and if it's slow you can always increase your player speed so that it reacts better so yeah i hope you enjoyed this video and that it was useful to you if you did make sure to like subscribe and hit that bell notification so that you can get notified on upcoming releases and with that i'd like to thank my patrons thank you so much for all of your support it really makes these videos possible i'd like to thank my new patrons and i apologize in advance if i pronounce some of these names incorrectly in the enthusiastics here we have young hanguin mert taylon the ghoster stefan tsujimaru rudolph k pong so thorn ari andy lance danger dan 2 ricardo mingy hong aaron k and tasnim thank you so much for the support i really appreciate it and in the dedicated tier we have a wild hippie thank you so much once again for all of your support i really appreciate it if you're interested in supporting me the link is in the description it'll help me out a lot and will encourage me to continue making these kinds of videos i offer source code early access and an exclusive discord channel and if you aren't already on our discord channel be sure to join you can chat post memes or ask for help so thanks again for watching and see you next time [Music] [Music] [Music]
Info
Channel: samyam
Views: 51,017
Rating: undefined out of 5
Keywords: new input system, input system, unity input system, unity new input system, smooth input, how to smooth input, smoothing, filtering input, smooth value in input system, smooth values, smooth value, axis smooth, axis smooth input system, getaxis in new input system, getaxis input system, smooth damp, lerp, slerp, smooth damp input system, migrate input system, smooth movement, smooth animation, smooth rotation, how to smooth input system values, smooth input system values
Id: krA-B8yItc0
Channel Id: undefined
Length: 7min 41sec (461 seconds)
Published: Mon Sep 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.