Smooth Motion in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hold up a sec... no EDM music playing way too loud? No 3-minute explanation about why it's been so long since your last video.... You didn't spend 28 minutes explaining the easy obvious stuff and then skip over the things anyone actually wants to know...

Is this even a Youtube tutorial at all?! What is happening?!

👍︎︎ 15 👤︎︎ u/UmaroXP 📅︎︎ May 26 2017 🗫︎ replies

Thank you for this. This has happened for a while with me and I just assumed it was because my computer couldn't handle all the work unity was throwing at it. Hopefully I'll see some improvements

👍︎︎ 9 👤︎︎ u/bcoffey12 📅︎︎ May 26 2017 🗫︎ replies

Is it bad that I basically always use regular Update()? I just never found any good reason to use the others when you've got Time.DeltaTime.

👍︎︎ 7 👤︎︎ u/Dec_bot 📅︎︎ May 26 2017 🗫︎ replies

Am I misunderstanding this?

I thought the commonly accepted technique was:

  • Physics and contiguous input for physics in Fixed Update
  • Camera logic and camera input in LateUpdate
  • Game logic and all other input in Update

Assuming your character controllers are all using Rigidbodies then is this article still applicable? If all controllers are not using Rigidbodies then what are some of the other gameplay bugs that could affect your game?

👍︎︎ 3 👤︎︎ u/Saevax 📅︎︎ May 26 2017 🗫︎ replies

Quote: If you feel like your fixed time step is too laggy, it's because the default Fixed Time Step is 50 FPS. Change the fixed time step by going Edit -> Project Settings -> Time.

https://www.reddit.com/r/oculus/comments/2odahu/unity_developers_change_your_fixed_timestep_to/

👍︎︎ 5 👤︎︎ u/loolo78 📅︎︎ May 26 2017 🗫︎ replies
👍︎︎ 1 👤︎︎ u/kyl3r123 📅︎︎ May 26 2017 🗫︎ replies

Unity comes with an Interpolation option on rigidbodies. Is there some reason to use the scripts in this video over that?

👍︎︎ 1 👤︎︎ u/Alressun 📅︎︎ May 26 2017 🗫︎ replies

Repost?

👍︎︎ 1 👤︎︎ u/_Typhon 📅︎︎ May 26 2017 🗫︎ replies
Captions
hi this is dog from kinematics ooh and today we're going to cover motion smoothing in unity if you've ever worked in almost any game engine you've probably experienced stuttering motion there is no single solution that works for every situation but there are certainly suboptimal ways of dealing with it this video aims to explain time steps and unity where you'll typically experience that are in motion and will present a solution that helps resolve it we're going to start with an example that shows how stuttering occurs and what it looks like notice the annoying stuttering going on here this occurs because some elements of the scene are moved and update while others are moved in fix update we'll go over the unity update cycle in more detail later on now let's look at the same scene where we apply the techniques I'll show you how to use notice the dramatic improvement in smoothness to understand this issue we need to understand how the update loop works in unity the parts will focus on most is the update and the sixth update loop unity implements what's known as a semi fixed time step which means the main game loop can run at any frame rate using a variable time step called Delta time and unity that manages an internal loop which uses six time steps this is some advantages primarily being able to run visual updates as fast as harbor permits while locking the core game simulation to a constant rate there are disadvantages though including being prone to the after mention stutter the following is a simplified representation of what unities update loop is probably structured like the update method is likely familiar to you already it's called an mana behaviour every frame just after your inputs are processed by unity and before the screen is rendered when one frame is complete unity will immediately start on the next one therefore attaining the highest possible frame rate since hardware and the complexities of each frame varies the frame rate is never constant because of this update can be called any number of times per second sixth update is called an mana behavior - each time the physics simulation is progressed unity treats these calls if they were a six time apart even though in actual time multiple simulation steps are not calculated at in even Interpol this is done because game physics in particular accelerated motion is most accurate and stable given even Delta time that fixed time step is known as six Delta time within unity by default it has a value of zero point zero two meaning there are always 50 physics steps and six updates calls for every second of game time in this way one can think of sixth update as frame rate independent as it's called the same number of times per second even if the rendering framerate is very low or high it is necessary to stress that fixed update and the physics loop are synchronous and does not occur on a separate thread this is an ideal representation of what 50 fixed updates and 60 update frames per second looks like however this is not perfectly achievable since frame rate varies so a more realistic they will exaggerated timeline is below notice how some frames are close together with no physics Tufts between them which typically occurs when there is low rendering complexity on the other hand other updates have long pauses between them with numerous physics steps computed between each frame this often happens when loading assets this means that even if you're trying to match the frame rate with the number of fixed steps they're not guaranteed to be aligned using our knowledge of unity time steps we can now understand the case presented in this scene there's a seer and camera both orbiting a pivot the seers transform is set in the update loop while the camera transform is set in the sixth update loop on the left and the update loop on the right the left side is obvious guttering while the right side remains news because update is called at a different rate and fixed update the sphere often moves all the camera mains still this causes a sphere to move inconsistently relative to the camera creating the stutter let's slow things down a bit so this behavior can be better observed now we can see the root cause of the stutter is moving some objects in upstate and others and sixth update the simplest fix is to move all transforms to either update or fix update however this is where things get tricky the common ends are found among unity developer is to put most game and movement object and update and use fix update only for the occasional physics task well this has its merits including simplicity of use it's problematic for many reasons perhaps most importantly is that your game play is now framerate dependent in unity it introduces problems and you need accelerated motion on an object such as a character controllers gravity for those objects six updates should be used but since other objects are moved in update you'll get stuttered therefore a common and sometimes necessary alternative is to put all state and gameplay logic in a fixed time step like sixth update and strictly handle visuals and input logic and update this is all without its own challenges however first off you may want your physics steps to occur at a different rate than the game logic ticks depending on your game this can be resolved by implementing your own fixed time step loop independent a fixed update the tick that would ever rate you wish this is fairly simple to do and can give you a lot of control allowing for fine-tuned optimization however one large issue is that fixed update is typically called less than the client framerate so moving objects don't update their position as frequently as the screen is rendered this makes the game somewhat choppy though consistent there are various ways to resolve this centering around using interpolation and extrapolation to fill the frames between fixed updates smoothing the motion out interpolation is moving an object smoothly from one game state to the following state it is convenient and that it can be applied to most objects and works easily but does introduce a fixed Delta x worth of latency this latency is generally accepted and plenty of games even twitch shooters allow for this delay to gain smoothness extrapolation however predicts where an object will be the next six step avoids latency but is inherently more difficult to get working seamlessly and comes with a performance cost this is another comparison to demonstrate interpolation on the left side both the camera and the sphere have their transform set in fixed update the right side is the same but with interpolation moving the transform smoothly between fixed eight steps notice how both objects remain aligned in either case but on the right side the more freaking transfer updates reduce stutter this example setup works based on three scripts done under this directory in the example project let's take a look at the interpolation controller first this script stores the timestamps of the two most recent fixed steps and by comparing them against the times during updates generates a global interpolation factor the script must be attached to a single game object in the scene the next script interpolated transform store the transform for an object after the two most recent six steps and interpolate the object between them using the global interpolation factor it also ensures that the object is placed back where it was the last sixth step before the current six step executes instead of where it was interpolate it to last this means that any scripts moving the transforms are working from the correct state if you teleport an object and want to prevent interpolation call the forget previous state method after moving the object this grip should be attached to any object move during sixth update lastly interpolated transform updater is used to call a couple methods an interpolated transform both before and after other scripts fixed updates this script must be placed on objects that also have interpolated transforms attached for these scripts to work properly you'll need to open the script execution order window by clicking editor project settings then script execution order once they're set the script order as follows also any objects with an interpolated transform attached but only be moved and fixed update as any transformations made an update will be overridden by the interpolation additionally you'll need to make sure that you buffer any inputs when necessary well there are numerous ways you can do this a good solution is to build input buffering into your own input controller in conclusion put all your game logic and either update or fix update do not mix and match time steps unless you're willing to bite the bullet and accept some stutter additionally it is strongly worth considering putting all game state in six updates using update exclusively for user input visual effects and interpolation between game states while this requires a change on how you structure your game it's a proven design structure with many advantages [Music]
Info
Channel: KinematicSoup
Views: 43,857
Rating: undefined out of 5
Keywords: unity3d, gamedev, indiedev, kinematicsoup
Id: qtV8JEIq0Ng
Channel Id: undefined
Length: 9min 28sec (568 seconds)
Published: Thu May 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.