Moving in Unity3D w/ FixedUpdate vs Update - Unity Physics and Movement For beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up I'm Jason and when I first started out with unity I really struggled with the difference between update and fixed update I got confused I made a lot of mistakes and I ran into a lot of bugs so today I'm gonna show you how to avoid those bugs what some of those bugs were that I ran into and how I use updated and fixed update now to make everything nice and smooth and just work cleanly so here I've got a little example with a ball that bounces around I can see I can jump and control it that's what we'll be using as our example but before we dive into the code for all this stuff we're gonna need to cover what update is and what fixed update is and what the differences are real quick so the best explanation I found or at least the simplest and most to the point one was on this stack overflow post which is also referencing a forum post and it just says that update is run once per frame which we all know because if you add a new unity script you see the update is called once per frame but when you add a fixed update you don't get a notification like that or any message right so you don't get this next little bit of information thrown in your face all the time which is that fixed update can run zero one or several times per frame depending on how many physics frames there are per second and the thing in the settings and how fast or slow your game is running so if your game is running really really really fast you may have 10 updates for every fixed update call it's running really slow you may have three fixed updates for every update call it all depends on your frame rate and your settings so the reason for this is that our fixed update is there to control physics stuff it's there for or it's there to keep in sync with our physics system so it's where we're supposed to do physics stuff but what happens is people run into an issue where they want to read input and then they want to push some physics data so they end up doing them all in either update they read it all and update and set the physics data or they read it in fixed update and sets the physics data and both of those have pitfalls so let's take a look at what those pitfalls are I'm gonna just pull up some commented code as an example so here's our ideal situation they were reading a jump in input and we're applying the force and input totally fine or for reading it and fixed update and we're applying it in fixed update everything would be fine if we were in this state where we have an update and a fixed update back to back every single time but this is not real world that's not gonna happen it's never gonna happen don't rely on it happening because it won't and you're gonna run into problems let's look at what some of those problems could be so say we are just reading the input and fixed up because well okay we know that we have to do physics stuff in fixed up B so we're not gonna do it an update you know better than that right so we'll just read the stuff in fixed update what could go wrong here's what could go wrong so we got an update and a fixed update yeah whatever order those nothing happened but then here on line 66 in this update the player has pushed the jump button whatever that is it's the button on their controller or their mouse button or something so get button down returns true so if you called the input get button down pass in fire one for that left click or left control then that would return true and some would get button because get button returns true as long as the button is down but let's go to the next frame we read the next update get button down returns false because get button down returns true only for the frame where it's red or that one time so now it's no longer true and when we go into our fixed update and we try to read our get button down there it's false and they don't jump and they go what the hell our jump isn't working right okay well then we'll just move it all to update nothing nothing bad then right well that'll fix it shouldn't be a problem but we do the same thing we go down here and we look at our moving in our update say we read the input in an update and then we move with the input then we read the input in another update and we move with the input we've already moved two times in one physics frame and realistically we could end up moving five or ten times we can easily miss collisions and cause a lot of other problems with our physics system so we don't want to do either of these but there is an easy so and that's to just read the data in our update and handle the data in our fixed update so let's take a look at what that actually would look like well first let's go back over to the editor and see what this feels like one more time so I come over here I run around and go left and right I can go up and down and if I left click I can jump so that that's what our controls are that's how everything works and before actually before we go to the code I just wanted to again thank everybody who does share the videos and subscribes and hits like button if you don't mind doing that now please just go hit share hit like subscribe any of that stuff it really does help gets the word out and then more people watch more people learn and everybody's codes better so you're really just helping yourself by making your future co-workers better at code by sharing it alright ok let's jump into the code I'll shut up now also a special thanks to everybody on patreon I always just want to say thank you to those guys really awesome I really do appreciate it now the code so we have a single script in this project and it's just called a mover and it requires a rigidbody because we're gonna move a move or thing around we're gonna apply some force to a rigidbody we have two fields here a move speed and a jump multiplier you can see that writer is smart enough to know that I've actually set the value to 210 is not the value that I'm using 200 is the value I love writer it's freaking awesome feature ok then we've got a rigidbody here that we're cashing in our awake so our awake just gets that rigidbody component we have a vector for our movement force and a boolean for jump effect let's add those to their own alliance these are the important parts this is what we're actually working with in our update which is called once per frame we read the horizontal and the vertical values from our axis so that's our WASD or our joystick or whatever we're using to move around and then we set the movement force to those values so set the movement to those so that next time I move in my fixed update I'll be able to read this I do the same with our jump so I read to see if fire 1 was pressed and I check to see if I'm grounded here we're just faking it we're just saying yes we're always grounded so I can jump all I'm in the air it's fine it doesn't matter and it's just because the way that you would implement a grounding system would depend a lot on the type of game that you want you know you may want to be able to bounce off the walls you may want to be right on the ground maybe water based stuff who knows so I didn't want to implement that and confuse things we just check to see if you're on the ground and we just lie and say that you are but if you press fire one or jump or left click we sent the jump pool to true that's it so that's all of our updates notice that our update is really just reading input so what I should do here and what I should have done before this is hit select all of that hit control shift R and hit extract method and let's call this read input and make that a method that's what I'm doing I'm reading the input and I'm filling in my movement force in my jump value then in our fixed update which is called every X milliseconds which is adjustable in our project settings let's take a quick peek at that so to get to that you just go to the project settings which is under edit and then project settings I have mine docked right here and then go to time and here you can see the fixed time stamp or time step not time stamp time step which is the amount of time between each physics update so here it's at point zero two so that's what a 50th of a second but or you could write it as like 20 milliseconds like that until so that's what it is and that's where we would adjust it if we wanted to so that's how often it's called and we have our move method and our jump method called so our move gets called adds force in the movement force amount or that yeah the movement for some vector which is that the direction that we want to go in the amount that we want to go in that direction and multiply that by a move speed and then for our jump this is important we do it a little bit different we check to see if jump is true if it is we add some force and then we want to set jump back to false because we want to reset that our movement force gets reset all the time in our update when we're reading it or jump does not we don't want to clear out our jump so we want to make sure that we reset that after we've processed the jump so just to recap one more time when we're doing stuff with movement or or moving objects around I guess you want to generally read your inputs in your update and then move things around and fix that date you should be able to get a nice smooth movement and if you don't check grounding you can jump higher in the air and kind of float around like a weird ball also um just real quickly I want to mention that grabbing the assets for this project was awesome I actually just used the built-in project view down here let me just show it because I thought this was really cool I used the project view and went searching for like fall found things that I wanted by clicking on asset store and just in a dug around grabbed a pack and imported and it was awesome really worked out great for just getting everything in here really quickly for prototype and so if you haven't tried that feature out I highly recommend it I'm in a twenty nineteen point three beta right now and it works great it's really cool anyway thanks again for watching again if you like this kind of stuff please share it first then like and subscribe after and then go put up posters about it and stuff but anyway thanks again really appreciate everybody and if you have special requests or other things you want covered just drop them in the comments below try to catch them all I miss a lot of them because there's so many but I'm gonna try to get as many of them as I can alright thanks again but you
Info
Channel: Jason Weimann
Views: 55,520
Rating: undefined out of 5
Keywords: fixedupdate vs update unity, unity fixedupdate, unity update, fixedupdate, unity3d, unity, beginner, tutorial, c#, programming, update, game development, unity 3d, unity tutorial, gamedev, addforce, addforceatposition, transform.translate, input.getbutton, getaxis, move, physics, jump, easy, unity3d college, movement, coding, tutorials, controller, unity 3d tutorial, unity rigid body, charactercontroller, rigidbody, character controller, game dev, unity physics, input, unity basics, how to make a game
Id: MfIsp28TYAQ
Channel Id: undefined
Length: 10min 31sec (631 seconds)
Published: Sun Sep 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.