How To Create A Day And Night Cycle - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another mention 4 tutorial today i'm going to be creating a day and night cycle in this we're going to be creating a cycle so it goes from day to night and you can change the speed or the day length and then in the future i'm going to be advanced upon this to also make it so you have the time written down and you have days so it keeps track and i'm also going to be adding on saving the time the day all of that good stuff but again that latter part is not today's video so let me hit play and i'll show you what we're going to make today so you can see we get in it's currently day the sun is gonna move around rotate and as it goes down the lighting will change we get this nice sunset and it's gonna get a bit darker and then eventually when we wait long enough the sky will turn to stars as well so it's proper night time and then it's very dark as well here as we wait even longer the sun will rise again come back up the lighting will change as it's going to get lighter again and this will work perfect for us again it's very easy to customize different things so if you want it to get lighter sooner you want it to get like darker quicker anything along those lines and of course we're gonna make it so this can be different speeds as well so the day isn't this long because obviously in a game you want it to be a lot longer this was just so i can show you very easily so now earlier i tried many many different ways of doing this and i did a lot of different maths until i realized wasn't really that necessary so i've completely changed what i was doing so i spent quite a while earlier doing this to then just redo it and come up with a much better way but without further ado let me do this code and i'll show you how i've done it so the first thing we want to do is want to select our light source so for me that's here and you'll see it's just a nice little sun with an arrow coming off it here it's not the sun but it's an icon of a sun what we're going to do is change it from stationary to mobile so the mobility is mobile so that way we can move it and rotate it using blueprints because as you can see here we can rotate it like so while we're rotating it i also want to make sure it is pointing directly down so the y is going to be a minus 90. the reason i'm doing that is just so it starts at midday and it goes around like that as it just makes it easy for us to do so i'm going to be doing that and also you can change anything else on here you want for example the intensity but everything else i'm leaving the default but you want to make sure that you have it as movable so this directional light we can change the direction of because then that's obviously going to change where the sun is so again as an example if we rotate it you can see the light is changing as the sun rotates again that's going to be good what we're going to do next is open up our level blueprint as that's where we're going to do this code so we go to blueprints open level blueprint very very simple then in here i'm going to set up actually moving the sun so what i'm going to do is i'm going to right click add a custom event and i'm going to name this one time cycle you can name it day night cycle or move sun anything along those lines and what i'm going to do is get a reference to our light source as that's what we want to move so i'm just going to duplicate the top there to minimize it a bit select our light source and then in the world outline up here i'm going to drag and drop it into level blueprint like so with this i'm going to drag out this reference i'm going to add actor local rotation because we want to rotate it connecting that into the time cycle there and what i'm going to do is i'm going to right click delta rotation and split the structure pin because we only want to rotate it on the y because again if you see this when we rotate it we're rotating the y there as that's the one we want to change to have it rotating perfectly around the world go back in here we only want to mess about with that x and z will leave a zero and to figure out how much you want to rotate it by what i'm going to do is i'm going to right click and get world delta seconds like so so that's basically getting the frame delta so the time in between each frame so yeah there returns the frame delta time in seconds and then we come out the return value and get a float multiplied by a float and we want to multiply this by a new float variable of speed so we can change how long this will take so we can right click that promote the variable naming this speed and then we can compile and change the default value just to test this out i'm going to have it as 30. that is quite fast i think that's why i was using the start so obviously you'd want this to be a lot shorter but again just to show you for the purpose of this and for testing this is a good value retire value of this is obviously going to go into the y of the add actor local rotation there again we can increase or decrease the speed to change the speed of the day night cycle as now this is going to actually work so this is going to change the lighting however it's not going to actually move the sun so to do that we just need to refresh the material so a way to do that via blueprints is to again minimize this select our sky sphere so we just select the sky drag our sky sphere blueprint in here like so drag this under here out of this we're going to not refresh material that's just something you tick in the details we're going to instead update sun direction connecting that into there like so that's now this is going to work perfectly for us to actually have a day night cycle however we need to make this into a loop so you can use event tick however this is slightly more efficient so what i'm going to do is hold down d left click to get a delay connecting that to the update some direction with a duration of 0.01 out of completed i'm going to go in call function time cycle just so it loops like this and then off of event begin play we need to call this to start with so we can hold down p left click to get event begin play out of this call function time cycle and now this will give us a basic dynamic day night cycle so we're going to compile hit play and we can see this working you can see the sun is moving around giving us the sunset sunrise night sky all of his good stuff and it is actually moving as well perfectly like so however you can see the lighting isn't really changing that much so the lighting is changing sorry but the brightness isn't so it's just as bright during the night as it is during the day which obviously we don't really want because during the night it gets quite dark so to solve that it's very simple and i'm going to delete the delay and time cycle there so we just have this code again so what we're going to do is hold down b and left click to get a branch connecting that into there like so because we want to check to see whether or not it is sunset or sunrise because we don't want to just be changing it throughout the whole day i want to change it when the brightness should change i.e if it's day or night so we're going to come out the light source over here again and get actor rotation right click and return value split strike spin because again we only want the y or the pitch out of the y what we're going to do is we're going to get a float is less than a float and also a float is greater than a float because we want to see if it's between two values because using equal equal is good however this is working a bit too fast for that to be able to keep up with it so it doesn't recognize it and fire off and we're going to connect both of these up with an and billion so they both have to be true so it has to be below this value and above this value so it's in between them and the end is going to go into the condition of that branch there what i'm going to do for sunset so it gets darker is i'm going to have it less than 75 and greater than 65 so anywhere between these two values is when it's going to get darker now let's also set up getting lighter so i'm going to hold down b left click to get another branch connecting that into the false of the first branch and we can just duplicate the less than greater than and and as you want to use them again so select them ctrl c ctrl v and goes into condition and these values go into the y of their once again now these ones i'm going to set it to be less than minus 0 and greater than minus 15. so let me show you where this is going to be in the world so if i zoom out the sunset so it's going to get darker around this point because that is roughly also where the sky starts to change into fully night and then around here is where it's going to change to be bright again and that's where the sun starts rising and the sky starts to change once again so through my testing i found these to be the best places and best values to have it working perfectly for us to look the best but again you can of course change them to get it perfect for you as well so just have a look at what values you want the rotation to be at for when you want the lighting to change again this one being tonight this one being today obviously by that i mean going tonight and going today so now let's go back up here and what we're going to do is hold down s and left click to get a sequence connecting that into true of the first branch here then after that we're gonna hold down o and left click to get it do once connecting that into then zero out of the completed of this what i'm gonna do is add a timeline i'm gonna name this one day night cycle timeline and then i'm going to double click that to open up i'll leave the length as 5 and i'll add a float track naming this one cycle track like so then on here i'm going to right click add a key to curfloat the time of zero value also of zero right click add key to calculate the time of five or the maximum length of your timeline and a value of one i'm gonna click these to change it right click on one of the keyframes change it to auto right click on the other one change to auto so now we have a nice blend here like so so what this is going to do is it's going to take five seconds to have a nice smooth transition from fully night time to fully daytime or for the day time to fully nighttime but obviously we will also have a nice smooth transition of the sun moving which will help with that but this is the main brightness and we're going to compile and close that and what i'm also going to do is select the sequence and do once ctrl c ctrl v to duplicate them down here with this one going to the true of the bottom branch and completed going into the reverse of the timeline and i will just move these a little bit to make them look a bit nicer and more organized working perfectly like so and then the finished of this timeline we want to hold down s and left click to get another sequence with then zero going into the reset of the first two once and then one going to the reset to the second do once i'm just going to click the white execution lines or double click them sorry to get some root nodes just to keep this looking nice and organized and so what this means is that it's not going to spam this so it's only going to do this once and then when it's finished doing it it will allow us to do it again but obviously by that time it won't need to do it again because the sun will be in a different position from these values so this is what's going to actually change us from day and night brightness wise but also we need to actually set up those values changing that's also very simple to do so what we need to do is minimize this once again like so and we need to select our skylight which for me is down here or the light source sorry drag and drop that into the level blueprint and then we also need to get the skylight which you can find in the wild outline as well once again dragging and dropping that into level blueprint because we need to change the brightness of our light source and also the skylight so what we're going to do is drag out these and just get set intensity so set intensity light component there and do the same for skylight set intensity out of the light components there like so connect the set intensities together like that and then also connect the first one into the update of the timeline so you should have something which looks a little bit like this and if i move them down as well it should look like this so what's going to happen is every time the timeline updates so it goes between the different values it's going to modify the intensity of our light source and skylight which again is changing the brightness the new intensity we want to use this cycle track here but we don't want to just put it straight in because that's going to go from 0 to 1. what we're going to do instead is right click and get a larp and you can see a lot under float i'm going to get two of these so i'm just going to duplicate that like so the return value is going to go into the new intensity of these two there like that and the alpha is going to be the cycle track so a lot goes between the value of a and b smoothly using the alpha as this timeline track to smoothly go between them and then we just need to set up our values of a and b so a is going to be the daytime brightness and b is going to be the nighttime brightness so to check that we can very simply just once again minimize this select our light source and you can see the intensity at the moment is 2.75 so our intensity for day is 2.75 and for night what i set it as earlier was 0.1 you can set it to whatever you like but i think that's a good value and then we'll do the same for skylight so if we select the skylight you see its intensity at the moment if we search for it here its intensity is one so the intensity scale is one so what i'm gonna do is have that as the daytime brightness so change a to one there like so and b i had a zero point zero one earlier compile and save and that's it done very simply like so what's going to happen is it's simply it's going to move the sun and also at different times of the day it's going to change the brightness depending on that so if it's night time it's going to get darker if it's daytime it's going to get brighter and obviously the timeline is going to do it over a nice smooth transition like so as well and again to make it faster or slower just change this speed variable here so the maths i was doing earlier was i was trying to figure out how to change the speed variable but have it so the timeline also represents that so i was trying to have it so the timeline is always equal to the day length and i did manage to get it working after a few different calculations however i didn't really like the effect it gave i'll tell you what the calculation was what i figured out to be was a hundred divided by the speed times by five equals the day length so let me show you that here so if i get the speed we can then also get a divide so float divided by a float the bottom value being the speed top value being 100 out of that we can get a multiply so float multiplied by a float times it by five and then this value here the return value is our day length in seconds and again did a few different calculations and that's how i got it so the day length in second is the speed divided by 100 times by five and so i believe as well if i'm correct that would mean that to have the day length in seconds as a variable you input what you do is let me create that so let me get day length in seconds changing this to a float and compile that i believe if we get that you can then input this in seconds and if we come out of this what i believe now correct me if i'm wrong but we should be able to multiply this so get a float multiplied by float i want to multiply it by 100 and then multiply that by five again i'm not a math wizard i'm not a math genius so if i'm wrong someone can correct me in the comments obviously but again we're not actually using this so it doesn't need to be too correct i'm just showing you so you have the workings out of what i did just in case anybody's interested you might not be at all but if you are there you go and also you may want to test this out as well so if you want to have a look into writing in the day length in seconds instead this will be a good start for you so this is equal to the day length in seconds and this i believe would then be equal to the speed again i'm going to delete that as we're not actually using that compile hit play and we can test this out so now the sun isn't moving so let's have a look at why we did that and that's a very simple thing actually what i forgot to do was i forgot to loop this so as you saw at the beginning we had a loop here but i deleted that because we need to add it here as well but i forgot so that's why we have these sequences here so what i'm going to do is i'll then one of this sequence i'm going to get a delay the duration is going to be 0.01 and now if they're completed we're going to call function time cycle not that one that's the timeline sorry call function time cycle so it's the exact same thing we had at the beginning except it's here now instead and i'm going to select that control c control v down here connecting that into then one of this sequence instead and then the false of this branch is also going to go in there again i'm going to double click that to get some room nodes so once again now we've created this as a loop so it's always going to be firing off which is again why you could use event tick but custom events are just slightly more efficient so that's why i'm doing it this way instead so now if we hit play we can test this out and you can see the sun is moving and it's moving quite quickly so i don't even see it at that time and this brightness is now also changing you should see the sun should come up there there it is and it flies all the way past with the brightness going up as it goes down the sun sets it gets to night time it's going to get darker and then as it comes back up it's going to be nice and bright again and again if i was to just change the speed here to be something a bit lower so let's put it as 10 we can have a look at this and also do the outro so i think that'll be for this video is we've done everything we want to do we've set it up so we've got a day night cycle in which we can easily change the speed of the cycle and the sun is going to move around the world as well we're going to have the sunset sunrise skies as well and when it gets dark enough we're going to have the nice dark night sky with the stars as well and we can also change the brightness as you saw here so when it's night time it's gonna be darker than when it's daytime and again it's coming up now the sun will start to rise and the brightness will start getting brighter as you saw there so this works perfectly for us again in the future we're going to be adding on to this so we can keep track of the day and time so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one [Music]
Info
Channel: Matt Aspland
Views: 43,651
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, day, night, cycle, track, time, daytime, nighttime, brightness, sun, moon, bed, sleep, bright, day night cycle, weather, speed, change, sunset, sunrise, set, rise, wake, clouds, light, source, sky, directional, refresh, move, in, sphere, ue4 day night cycle, day/night
Id: Zf7Sc1vW6ks
Channel Id: undefined
Length: 17min 42sec (1062 seconds)
Published: Thu Apr 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.