Unreal How to Make a Mesh follow a SPLINE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a while ago I made this tutorial about having actors of following along a spline in your own real engine games as a result of that I've gotten a lot of questions on how you can make a mesh follow along a spline to make something like real tracks so simply changing around this spline will add new parts to this track which is exactly what we'll be doing today so if you want to follow along let's get started and there is a link Down Below in the description to the finished project file with the entire assets available for you to look through in the description down below to my patreon so first and foremost you're going to need a static mesh just a 3D model and there's a couple of things that you need to keep in mind there the most important one being the direction that your mesh is facing for this tutorial I'm going to assume that the mesh that you have is going to be orientated in the X Direction meaning that the x-axis is going through your mesh like this you can have it in the y or the z-axis as well and I'll mention the slight differences that that will make in the actual blueprint when we get to that but for now let's make our blueprint class and we're going to be making act up and we'll call that something like BP mesh over spline opening that up we're going to start off by adding a variable which we'll call something like uh mesh will start that to be in public and we'll make that of type static mesh we'll need to scroll down a little bit until we see in object types here static mesh and we'll need a object reference to that then in our components of course we're going to need a spline because that's the entire point of this entire tutorial is we're going to put things along this spline and from there we can go into the construction scripts where we'll actually do all of the blueprint systems that we need so first things first we need this mesh which we can just drag and holder control to get a reference and we need the exact dimensions of it this way our system will automatically work perfectly with every single mesh that you could put into this so what we'll do is dragging off this we get bounding box and if we split that structure pin we get a minimum value and a maximum value and we want to make sure that neither of these values have negative numbers in them so what we get is the absolute value for them so we do that for both and then we simply add these together with a simple addition node if we split the structure pin here on the results we now have the exact X Y and Z dimensions of what our mesh is like at least simplified as a box and here's the first step where it's important to keep in mind that the mesh that I'm using is orientated in the X direction if your mesh for whatever reason is is orientated in the y or the Z Direction you're going to use the corresponding values there but we're going to use the X so we're going to right click that and promote to variable we'll call that something like X length and now we have that available as a variable for using later on next up we're going to go into a for Loop not a for each Loop just a normal for Loop the first index will keep at zero the last index however we are going to need to calculate using our X length and our spline so we'll drag in a reference to our spline and from that we'll get our spline length which we will then divide by the length of our mesh and then we want to truncate that to make sure it's just an integer because we can't have a float going into the index for this Loop this way this Loop will run the exact same amount of times that our mesh will fit into the length of our supply in the loop we're going to add a spline mesh components and now it starts making sense why we need to Loop through what we're going to do here the amount of times that our mesh fits into our spline because we're going to add a new mesh for every single time it can fit in the return value here will be that mesh component so first things first I'm going to set the Collision enabled and we're going to set that to just Collision enable this way the mesh that you're adding actually will have Collision which is usually relatively important and then next up we're going to set the forward axis to being the one that matches the direction of your mesh so again for me in my case that is the x-axis if your mesh is orientated in the y or the Z of course you're going to want to change this around but now we only have a component which doesn't have an actual mesh in it yet so that's the next thing we're going to do is we're going to set the static mesh that this component is going to hold and we already have a variable that holds whatever mesh that we're going to be using so we can simply just drag that in and connect that up and one last time we drag of the add spline mesh component just like all these other three and this one we're going to set the start and end location and for this we're going to need to do a little bit of a simple math so stick with me it's not that complicated going back to the for Loop we want to use our index which is the amount of times we've looped through this so the first one We're looping through this index value is zero then when we Loop through it a second time this index value will be one when we Loop through the third time this index value will be two and so on and so forth and we're going to multiply that value by our X length this will give us the length along the spline for this starting position and starting rotation of our mesh so we can drag in a new reference to our spline and and we can get location at distance along the spline and we'll keep that at a local and put in the answer from this multiplication into that we'll also get the tangents at the distance along spline which will use the same distance for and those will go into the starting position and the starting tangent respectively and then we also need some for the ending position and the ending tangent which is simply the start of where the next mesh will start is also going to be the end of where this mesh will end so we simply just drag off this index and we add one and then we can copy this entire thing over and that's all there is to it now we have the end position and the end tangent except we're not quite done yet because the tangents will grow out of control and they will make things look buggy and messy and weird so so what we're going to do instead is coming off the tangents we'll get a clamp Vector size and that will go into the tangent instead and there's a vector size will be clamped a minimum of zero that's fine the maximum will be the X length meaning that the tangent can never be longer than the actual mesh is because that will then cause weird overlapping stretching artifacts and we don't want that so that's why we're clumping this and of course if we're clamping the starting tangent we also want to be clamping the ending tangent and that is everything there is to it once you compile this and you go back to your whatever level you can just drag this in and now we have a mesh variable here we can just drag in the mesh that we want and you can see at first it might look a little squashed but now when we start moving these points around we now have a pretty decent looking rail along our mesh well I don't have the materials set up for this right now but of course I can just do that real quick give me a moment real quick made two materials to match this and now we have a pretty good looking reel which we can very easily change its shape and if we play the game and we try to jump onto it you can see it even has proper collision and everything works the way you would immediately expect this kind of thing to work so all together that's relatively easy if you need a little bit more help looking around at how this works there is a link Down Below in description to the patreon where you can download the project files with the mesh that I'm using here included as well it definitely helps out the channel if you support me through patreon so that is very much appreciated if you want to do that next time we'll be back with some more interesting Unreal Engine stuff and a very big thank you to all of my patrons you can see them on screen right now if you want to help out supporting the channel there's a link Down Below in the description to the patreon page
Info
Channel: The Game Dev Cave
Views: 27,112
Rating: undefined out of 5
Keywords: spline mesh, mesh on spline, unreal engine spline, unreal spline, unreal engine, unreal mesh on spline, unreal tutorial, the gamedev cave, unreal engine 5, ue4 tutorial
Id: l5W8KdQYI7k
Channel Id: undefined
Length: 9min 8sec (548 seconds)
Published: Wed Jul 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.