How to make Slow Motion in Unity - Bullet Time Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hey Brackeys, great video and cheers for the tip. Just a quick question: In the TimeManager Update function, you're gradually incrementing the timeScale back up to 1, but nothing is done about the fixedDeltaTime. Would this not leave you with a higher fixedDeltaTime than before, much higher than the original 50 physics steps / second?

๐Ÿ‘๏ธŽ︎ 2 ๐Ÿ‘ค๏ธŽ︎ u/Novwyi ๐Ÿ“…๏ธŽ︎ Jan 16 2017 ๐Ÿ—ซ︎ replies

This guy is amazing, watched all his C# videos but felt kinda betrayed when he started his unity series using Java

๐Ÿ‘๏ธŽ︎ 2 ๐Ÿ‘ค๏ธŽ︎ u/Chody__ ๐Ÿ“…๏ธŽ︎ Jan 16 2017 ๐Ÿ—ซ︎ replies
Captions
[Music] hello I want to look into this video creating a bullet time effect in unity yes what we'll be making today throughout this video I'll explain about how time works in unity and how it can manipulate it while still getting really smooth results I also just want to mention that I'm on a fixed video schedule so you can expect a new video both witnessed days and Sundays let's just go ahead and get started here's the project we're going to be working with today it's fairly simple but I will of course have a download link for you in the description if you want to have it on your own system if you go ahead and in play we can see what it currently looks like so we can move around and we are able to shoot these explosions that will create a force that acts on these rigid body objects which are currently just red cubes however we currently have no slowing down so let's go ahead and create a bullet time effect so to get started let's create an empty game object so right click in the hierarchy go create empty and let's reset the transform on that to make sure it's centered in our scene we can now rename this to something like time controller or time manager and we can go ahead and drag it to the top of our hierarchy and add a new component and this is where we want to add our actual script so let's just go ahead and call it the same thing the time manager lets it create an ADD and let's double click this to open it up in visual studio let's clean this up a little bit let's delete our two functions and let's also delete the two using tags up here because we're not going to be using anything under system collections now we can create two variables that will allow us to configure how our slow motion will occur the first one is going to be a public float and this one is going to be the slowdown factor the slowdown factor is going to control how much we will slow down time when we do up all the time effect the lower the number the more slow we're going to be moving so I'm going to default this to something really low say 0.05 we also need a variable to define for how long this slowdown will occur so in that case we'll create another public float called the slowdown length and we can go ahead and default this to something like 2 meaning that we will be slowed down for 2 seconds tolls now we need to create a method that will change some time settings around in unity in order to make the game engine slow down we'll go ahead and write Boyd and the name of the function is going to be something like bullet time or slow down or slow motion I'm just going to go ahead and call it do slow motion and inside this function we can set time time scale so what is time scale time scale is basically a value that determines the scale at which time is passing if time scale is one time we'll be passing at a normal pace we say that it's running in real time if time scale is 0.5 time we'll be passing two times slower than real time and so we can adjust time scale to a value between 0 and 1 the lower the value the slower the game will run so let's go ahead and set our time that time scale equal to our slowdown factor because the slowdown factor is equal to 0.05 we can very easily calculate just how many times slower we are going to be moving at and we do that by saying 1 divided by 0.05 which is equal to 20 that means that we will be moving 20 times slower than normal this function should actually now slow down time but we also need to call the function from somewhere let's go ahead and find the place in our project where we can do that online player I created in a script called explosion gun and this is responsible for shooting out an explosion wherever the user is pointing let's go ahead and double click on that to open it up in visual studio and we can have a look at what's in here so I have a few references one to the explosion prefab that we want to spawn and one to the camera in the update loop I basically just check if the user presses the fire button and if he does we should then I do a bit of break as trigger II to shoot out away from our cameras current position and then just straight forward and if we hit something I go ahead and create an explosion as the impact point and this is basically where we also have the opportunity to slow down time but in order to do that we need a reference to our time manager let's go to the top here and create yet another public variable and it is going to be a public time manager and we'll just go ahead and call it time manager with the non capital T so right after we instantiate the explosion we can now say time manager dot do slow motion but the problem here is that our slow motion method is currently private so we cannot access from within this script to change that we seem to go into a time manager and insert a public in front of the board so now this method is public and so we can access it from within this class so we call the do slow motion and hit save so if we now head into unity we need to select our player and drag in the time manager into the time manager variable there we go we can now hit play and we should see this working but something weird is going to happen so if I hit our shoot here we can see that time is slowed down quite drastically but it almost seems that the game is lacking however our mouse movement and the particles here are smooth so what is causing this well the reason why is that whenever we adjust our time time scale we also need to adjust time ducts fixed Delta time fixed all the time is one of those things in unity that just sounds really scary but actually it's quite simple we know that the update function runs each time the computer draws a frame this normally happens many times each second and allows us to make changes to an object over time but how many times the update function runs each second depends on how much load the computer is under and how fast the components in the computer can perform this is normally not an issue but when it comes to doing physics calculations everything becomes much easier and more reliable when we know beforehand how many times a second of function will be called there for unity uses a separate function called fixed update to calculate its physics fixed update is of course called a fixed amount of times per second so the bug that we just saw in unity occurred because fixed update got slowed down so much that we could visibly see it updating luckily we can change the time between each fixed update through script that is done by modifying the fixed Delta time variable we normally want a fixed update to run around 50 times per second which means that fixed Delta time should be 1 over 50 which is equal to 0.02 and then we multiply with the time timescale so that when we slow down time making timescales smaller we will also decrease the time between each fixed update so to make sure that we adjust the fixed Delta time inside of our time manager we simply go time dot fixed Delta x equals x time scale multiplied with some small numbers say point zero two which is the standard make sure you do this after we adjust the time time scale so now when we save this and head back into unity we should get a very smooth slow-motion effect so if we go over here and hit youยดd we can see just how nice that looks in comparison so much much better however we are currently just staying slowed down time doesn't go back to its normal state so in order to slowly return back to our normal time scale we need to use the update method let's go here and write void update and update is of course called each time the computer draw the frame and unless your game is ridiculously hard to run that means many many times a second so what we'll do is each frame add a tiny number to time that timescale in order to slowly return it back to one so to do that we write time timescale plus equals and in here we write one divided by this slowdown length so right now a slowdown length is Q which means that this will say 1/2 which means 1/2 and so each second will add 1/2 back to the time scale and so in 2 seconds it should return to normal however that's not what we are currently doing what we're currently doing is actually adding 1/2 onto the time scale each frame which might be drawn say 60 times a second or 120 times a second so what we need to do is multiply this whole thing with time.deltatime so to do that we wrap this in a few parenthesis here and then we say multiply with time dot go to time and time that delta time is the amount of time passed since we drew the last frame and that means if we are drawing a lot of frames a second this value is going to be small and if we're only drawing a few frames a second this value is going to be big and that means that it is going to even out this value so that over 2 seconds we are going to be adding 1 to the time scale and we'll do that in very small increments if that's kind of hard to wrap your head around I totally get it no one really understands time that does the first time but really the more you get to use it the more you understand however this is currently going to propose a problem and that is that time that Delta time is actually also affected when we change the time scale and so this whole equation becomes really weird so what we do instead is use time unscaled go to time and this is the exact same thing as delta time the amount of time elapsed since we last refrain but unscaled delta time is not affected when we change the time scale so right now we're altering the time scale in order to have it returned to one but time scale can actually be greater than one if we want to speed up our game we can set time that time scale equal to something larger say if we wanted to run two times faster we'll set it to two and so right now unless we are slowed down this is just going to keep increasing and so our game will run faster and faster if we hit same on this right now and then hit play we should see that our game just slowly speeds up and indeed it does we're able to run quicker and quicker until of course we shoot something and then we are reset back to 0.05 so we're going here and say time time scale equals mass dot clamp and the value we'll want to clamp is time time scale and we want to clamp it between zero and one and this means that will either be totally slowed down or running at a normal speed and so we won't be able to increase the time scale between or further than to the value one and so when we add save now we should see if we have play here that time will not increase above the one and if we go ahead and shoot something time will slow down and then slowly go back up to one and so now we have that old school bullet time effect what i really suggest to do is combine this with some changes in audio a very normal thing to do is adjust the pitch of your game so that when you're slowed down you will decrease the pitch and then increase it as you slowly increase the time scale but that's really just a lot of things that you can do to play around with this and I suggest you just have a lot of fun finally I just want to say if you're making a shooter and want to grab some weapons you can go to dev assets calm and select the modern weapons pack here there are some really really nice high quality weapons here for you to download it's pay what you want so you can totally decide the price and yeah we really hope that you are going to enjoy them that's pretty much it for this video if you're making a shooter I really suggest you check out my latest tutorial on creating a sniper scope effect in unity I think it turned out pretty well if you want to see more subscribe to the channel you can expect a new video every Wednesday and Sunday thanks for watching and I will see you in the next video thanks to all the awesome patreon supporters who donated in December and a special thanks to Sultan al-sharif Derek James Kirk place to verify James Calhoun's Robert Bonham and Jason Lotito if you want to become a patron yourself you can do so at patreon.com slash Trekkie
Info
Channel: Brackeys
Views: 269,699
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, models, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, tip, game, development, develop, games, programming, coding, C#, slowmotion, slow, motion, bullet, time, superhot, deltatime, timescale, smooth, fixeddeltatime
Id: 0VGosgaoTsw
Channel Id: undefined
Length: 11min 52sec (712 seconds)
Published: Sun Jan 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.