Unreal Engine 5 - Procedural Train Simulation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone so today we are going to implement this procedural Train by procedure what I mean is we can dynamically change the number of cars attached to the tray we can go anything let me add 20 so and also we have this circular train track which is also procedurally generated including these trusses and these guys we worked on that yesterday today we are going to focus on this train and its moment so here is the demonstration so train can nicely move along this q1 track at the moment if these are just simple models but later on I'm gonna improve so that maybe player can get in get into these four frame one of these cars and travel in the train so yeah yeah it works nicely let's complete one Loop and then let's get into the tutorial okay one Loop is completed so let's see how to implement this system today so I have completed a circular track as you can see here see it's course in a complete circle like this and you see this procedurally generated the wooden ties and these trusses to support it is also generated so we worked on that part yesterday and I have modeled and imported this simple crane engine and this train car mesh into the engine so I model them in blender so it's a pretty simple model and at the moment these are just static measures because I only want to focus on how to move the Train Engine and the cars along this path so after we figure that out we can work on how to add rotted in wheels and all other details that we need okay so first let me just delete these two meshes and then here let's create a blueprint in the type of act yeah uh maybe not that let's use the type on because it says sometimes we want to control the train as a little driver so baby Ray all right so now let me open it and I think I'm gonna give this defaults Road under that let's attach this engine compile and let's drag and drop it here so first in the trend I'll create a variable track and track should be in the type of BB Raider track so Maybe [Music] a track now I'll make this public so I can set the rare track reference Theo well I can't see that track compile right [Music] okay now I can select the rail track so um now in the construction script [Music] if you do have a valid track you can access the supply component and I'll promote this to a variable track wait no um [Music] now get location at distance around spline so here I'm going to use the distance 0 and location word and also um get rotation at distance so long spy this also has to be worked then detector location sorry not just location location and rotation say that location and rotation this is the location this is the rotation and compile now see the train is placed on the beginning of the track so this is the beginning point this one is the first point in the spline so it's automatically placed on that and I have designed the scale of the mesh to fairly match with the track so it think it it's good enough the placement of fields so right now the next part is moving the tricky so for that oh here even if this is a pawn we can have like a full loading moment component or anything but I for now I don't want to use any of them I just want to move the train through the code so get the track spline and just check if it is valid then get application [Music] uh at distance a long spline this distance I [Music] promote it to a variable I'll call it track distance and of course we need the word then get rotation at distance around the supply both has to be word and its distance track distance now oh well basically we are doing the same thing as here yeah but said that location and location same as before and then we need to move the trend so get the track distance and add something so this one is the speed also into variable confide let's say one I'm not sure and then wait we need to multiply this with uh word Delta seconds to make it smooth and independent from the frame rate and set update the track distance like this so to keep it looping if track distance so if track distance exceed the length of the track spline we are going to run into a problem so we can get the link then to modulus division with the length so if we get something higher than the length we will get the remainder only here right that's basically all right everything and play I think it's small but very slow so that's correct okay so our train is moving let's see if it can look without an issue so it's closer to complete the loop yeah because without anything so the amount logic is working right okay so the next step is add in the cars um for that here I'll add a variable in the type of integer I'll make it public too none of us and also for this we need to know the length of one car so if I go to the top view press middle Mouse button and drag a line place it I can't read 455. okay let's say 500 number and also call it [Music] should be flawed and let's try 500 minimum of course let's start with five right now this part is about positioning of the tray so [Music] then next part is cost so let's have a fold starting from zero I think we should start from one and last index number of costs right here it's at the static mesh component as I said later on we might change these into a skeletal mesh because we need to have rotate and wheels and everything so yeah or maybe a separate child blueprint I'm not sure for now okay so as the mesh we can assign car and relative transform we can make we can split this and we can keep X and Y next vector we can keep X and Y 0 part 4 C multiplied with the current and put it here compile because five oh ah sorry not C we should change X sorry X compile oh okay sorry again multiply Pi minus now it's correct so the next uh challenges how to make them uh place along the along the road along the track right one more thing this okay they are movable already so this is a static mesh component so I'll add a variable cause [Music] type pieces [Music] static mesh on what object reference and it has to be an array then get class and unique even if I use that it would make a difference that this case I think actually the reason I save it to a array is because I need to manipulate the allocations later on so after updating the car after adding components let me do function because we need to call this synthetic as well I'll add a function called update uh positions uh not just position rotations as cells so update car get from now for each Loop and then distance get the track distance and also get that index multiplied by car length so what I what I'm going to do is for each car finding the appropriate location and rotation on the track so this has to be multiplied by minus one because Chi is always behind the engine then add to the track distance now if this is negative let's have a branch this is negative that means we that means for example if the engine is at the beginning of the track if we get a negative length or distance that means we should consider the last points of the display because it's a circle so what should we do here I'll add another variable another local variable car track distance should be float not an array just to a single variable let's set this so yeah as I said if this is negative we need to get the track spline this P should not be kept get spline length so you'll get a point in the end and that is the car track distance otherwise you don't really need to do anything else if it is positive we can refuse it right now after that we can again just like before from the track spline you can get location at distance along spline word space and get location at uh turns along spry is also has to be word and then finally we can access this static mesh component here otherwise this could be unclear or I need Q set word location and rotation all right I think that's oh wait we need to use car track distance compile and we need to call it update cause compile now you see all the classes are on the track nice and also we need to call this same method in the tick also okay Plus okay now let's see yeah it's okay I see we see some elasticity essay I don't know the exact word but that looks natural when we have that okay nice so we got only four costs include uh five class including the track let's see complete the circle without any shift it does all right so now of course why is it four star from one to five one two three four five that's it for then we can do one thing we can make this start from zero and add one here to have the exact number of cars as we have defined one two three four five yeah okay uh nothing do I need to clear since this is construction script I don't think we have to okay now if I make 50. see we can change to any number of cars 20. and still it works all right let's I think we can stop this episode right here just like the snack we worked on previously oh I would have reused the same code didn't think about it okay let's wait until this complete one Circle and then I'm gonna stop this episode right here yeah it can complete the server without an issue okay so thanks for watching as always project files will be available for the download in the pattern page link would be in the description below and if you like to support my work you can read the membership of the better see you in another episode goodbye [Music] oh I just noticed very bad one mistake here um there is a car uh all right so here also we have to add one make it work properly yeah correct oh right no that makes sense originally we shouldn't have it we can start from one and do it like this now we should have the correct move so that's what has happened you can test it if I make it four easy for class firefighters okay correct and the logic also works without any issue okay so sorry about that mistake and then oh also I forgot to show one more thing in the rare track I had to connect these fours into here because if I don't do that once I hit something encounter some location that does not have does not get a hit then after that point we don't see these trusses so no more tracing happens so that's also something I forgot to mention and I should have mentioned in the beginning okay thanks again see you in another episode goodbye
Info
Channel: CodeLikeMe
Views: 8,847
Rating: undefined out of 5
Keywords: unreal engine 5 train simulation, unreal engine 5 procedural train, unreal engine 5 train movement, unreal engine 5 train system, ue5 train simulation, ue5 procedural train, ue5 train movement, ue5 train system, unreal engine 5 pcg, unreal engine 5 procedural content generator, unreal engine 5 procedural content generation, unreal engine 5 procedural content generation framework, unreal engine 5 pcg graph, unreal engine 5 procedural graph, unreal engine 5 procedural railroad
Id: 5RMGKfewDxg
Channel Id: undefined
Length: 33min 3sec (1983 seconds)
Published: Tue Apr 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.