How To Make An Object Follow A Spline Path - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another origin 4 tutorial in today's video we're going over something quite simple but quite useful and that is making an object follow a spline path as you can see here so you might want something to follow a very specific path and to do that you can use a spline to draw out and lay that path and have something follow it as you can see here now as it reaches this point you'll notice it kind of snaps back a little bit that's because that's the starting end of my spline and i've just placed it there to loop it all i probably need to do is just be a bit more fiddly with where i place it so it goes back a bit and you probably also won't have it looping it like this it might be going through different places so you probably won't notice on your game but if you do want it like that all you do is just be a bit more careful with how you're placing it i just did a bit quickly for the purpose of the video so this is what we're gonna be going over and creating today again i'll show you that little thing there all i do is just need to move it a bit well the actual point like this so this is what we're going up today you can draw out this spline and have an object of any kind move along it at any speed that you like so without further ado let me delete this code and i'll show you how i've done it so the first thing we want to do is we want to actually create the spline in which we can draw our path for this cube or your object to be following along so to do that we're going to right click go to blueprint class and create an actor and i'm going to name this spline path bp as that makes sense for me but you can name this one if you want so it could maybe be patrol path or road if this is for a car really anything that you want so i'm then gonna press enter and open that up straight away that's a bit small so let's make that bigger and all we need to add in here is a component for this being a spline that is all we need to do now obviously if you want this to be a road you can actually add a road static mesh on here and you can have it so when you increase the size of the spline the road will be placed manually as well and that's also very useful for if you want it to curve you just curve the spline it will also curve the road so making a big road network and system splines is a great way to do that and if you want me to create a video on that then let me know in the comments down below and i can do that but at the moment we're not going to customize the looks of it so this is going to be fine for me we do not need to do anything else in here so i'm going to close that like so then we want to create the object which is actually going to be following this path so i'm going to right click go to blueprint class create an actor and i'm just going to name mine cube bp as i don't have anything else to actually follow it uh a cube is just good for me it's nice and simple just to test this out so i'm also going to open it up straight away like so then add in a cube that makes sense again use whatever you want so it could be a car if you're using the road and once it's in here we do want to change this a bit so you'll notice it's just going to be in the dead center as it normally would we want to actually raise it up a bit so the bottom of the object is going to be kind of in the middle here so essentially this grid the floor of the actor we want the base of our object to be on the floor so if we compile save and minimize this when we drag it into level you'll notice it's now placed completely on the floor like so whereas it would be like this normally if we kept it in the middle but now it's like this so that's just good so it does then actually float along the floor not halfway through so obviously do that for your object for how it looks best for you then we'll open up our blueprint again and go over to the event graph here i'm going to delete event act begin overlap and event tick however we are going to use event begin play what we're going to do is get actor of class just the singular actor not all get active class with this being our spline path bp which we just created and we're going to right click this promote it to a variable and name this spline ref for spline reference like so so now we have easy access to the spline that we're going to place down inside of our level we'll compile and save that just so we do have the updated variable now if we scroll down under here right click and add a custom event this is actually going to be moving the object so i'm going to name this simply move object as that makes the most sense for me i'll move object move object now this is where we're going to do the majority of the code now if you've watched my videos on doing most things where i'm moving things for example opening a door you'll notice that i commonly use timelines now timelines are a great way to just really smoothly move things from point a to point b and that's essentially what we're doing we're going from point a the start of the path to point b the end of the path so what i also want to do is make it so we can really determine the length of this timeline without having to change it every single time so i've made a video on this separately but i'm going to go over here as well we're going to create a variable and name this time to complete and if you really want to get particular you can write in brackets in seconds just so you remember and i'm going to change this from a boolean to a float compile and save and what we can do actually is also make this instance editable so we can change it for every single cube or every single object for you you don't need to do that but i've just thought of it now while making this we're going to drag this in so we're going to get time to complete in seconds then we're going to right click get a float divided by a float with the time to complete goes into the bottom value and the top value will be one so it's one divided by our time to complete if we compile this we can actually set a default value for this so i'm going to set it to five seconds so it will take five seconds to complete so we have one divided by five which is obviously 0.2 and that is going to go into the play rate of the timeline we're about to create in a minute so the timeline will play at a rate of 0.2 which will then take 5 seconds this will make more sense in a second so let's add that timeline we're going to right click and add timeline down at the bottom here and i'm going to name this one move object t for timeline and before doing anything else with this what i'm going to do is in the components list in the bottom left here under variables i'm going to drag in my move object t for the timeline there get that and out of this i'm going to set the play rate which is what i just mentioned a second ago this is going to go into the move object here and the new rates will go into the divide there so again one divided by the time to complete is the play rate for our timeline so the timeline then plays however long we want it to here so again you can now change this for every single instance of the cube but it just makes it a lot easier from having to create multiple variations of this blueprint and changing the actual timeline here so set play rate is going to go into the play of the move object t like so will the play of our timeline and we're going to double click our timeline to open this up set the length to one and down that is very important if you don't set it to one this here will not work it needs to be one second so we can actually change the time to complete and we're also going to add a float track in here i'm just going to name this one move track like so we're going to add a key to curfload so right click add key with a time of 0 value of 0 so it's all the way at the beginning then right click add key with a time of one and a value of one so it's all the way at the end i'm going to press these buttons to zoom to fit so we just have a nice straight line here now what you can do is you can right click on these keyframes and change them to be auto just so you have a little bit of a nicer curve like this and if you want it to be even nicer you can add another key and just really move this to where you want and kind of just auto again just change how this looks so you can really mess about with the tile here to get it different so the speed is going to change a little bit so again you can just move these but you want to just really get it to be completely different speed so the speed here is just going to constantly change it'll be a bit random so obviously you might not want to do that but that's just showing that you can change how this looks so you can get a nice smooth curve or just a straight or change it to make it go up and down if you wanted i'm just going to keep it like this so nice and simple what we also want to do is tick loop and you can take auto play but because we have this here we want this to fire up first so i'm not going to so i'm just going to take a loop we'll compile save and i'm going to close the timeline there like so so now we have our timeline which is going to be the value of where we're going on the spline so we want to use this value to actually find the location of where that spline is so that's actually quite simple what we're going to do is get our spline reference so it gets blind graph here out of this we're going to get the spline scroll all the way down to the bottom and we have it here this is just the spline we added in so if you change the name of it search for the name you changed it to so get then i'm just gonna move it down and out of this we're going to get spline length so you can then change this to be however long you want and it will always work dynamically for the correct length of the spline so we've got it like this now this is going to go into a verb so we've got the return value and get a lerp float and this is going to be b not a so what love does is again i've mentioned in a few of my other videos is it basically smoothie goes from point a here to point b here via this alpha so it's kind of like a timeline ignore this timer up here this is something different kind of like a numbers timeline you've got the start you've got the beginning and the alpha it's kind of the key or the point on where you are in the timeline and this timeline up here is going to be that alpha so that is the actual point of where you are so i hope that makes sense i do have a video going over in more detail what alert actually is if that's something you think you'd find useful so a is just going to remain a zero so that's going to be the beginning of the spline so zero and b the end of the spline is gonna be the total length so it could be a hundred or whatever it actually is for you and if you want to go backwards all you do is just swap dna around so a is the end b is zero so obviously do that or you could just do multiply the track by float multiply by minus one to inverse it and put that in the alpha however i just want this to be a normal track going forwards so then this return value here is the location that we currently are on the spline so it's the distance from the beginning of the spline that we currently are so we want to use this to set the location so we're going to come out of update of the timeline and set the actor transform will do and let's do set actor transform at the bottom there now we want to right click this new transform and split the structure pin and the location and rotation is what we want to mess with you can do scale as well if you wanted i'm not going to mess about with it but i did the transform just in case you do want to do that so now how do we get this float into a vector to actually find out the proper location because you've got to remember this retaliate at the moment is distance but we want location luckily there's a very useful node already made for us so we can get our spline ref drag out this get spline just like we did earlier so you could copy and paste that actually then what i'm going to do is drag out spline and get location if i could spell that correctly let get location at distance along spline like i say it's already nice and easily done for us so we can input the distance which is the return value here the coordinate space being world as these aren't in the same blueprint so we want it to be the world location and this return value go into the new transform location and that is now going to actually set the proper location of where the distance is on the spline so again it's going to be moving along the spline getting that distance and updating the actual location of our cube to the location of the spline so i'm going to move this out a little bit to give us a bit more room and now luckily we can do something very very similar for the rotation so i'm going to drag out our spline again and get rotation at distance lung spline pretty much the same thing but just for the rotation instead of location i'm going to move that up a bit move these across and again the distance is the return value there like so and actually i'll move these down that doesn't look as good so this is what we've now got and this is all we need to do so i'll go through again when we tell it to move the object it's going to set the current play rate so it's going to complete it in this amount of seconds which we've set which will then start the timeline which is looping so this will constantly do it throughout the entire game and that's essentially going to be going along this spline path updating the location and rotation of our object along the spline as it goes along so we can compile save minimize this and now let's actually put in our path so i'll delete the key for the second and put in our spine path so i'm going to start it here and now the way to use splines is you need to try and select this end of the spline here so you can just click it like so then if you hold down left alt and drag with left mouse button you're going to create a new spline point i'm going to draw it out like this and so you can move it about here then you can drag another one out then another one then what i'm going to do is drag another one here rotate it and move it so we're going to have a nice bend and a nice curve like so then i'm going to move out again and do the same thing so i'm going to move it down here and rotate it just so it's got a nice curve to it instead of it being an instant snapping point and then bring some more down and i'm really going to make this basically a very simple loop so i think something like that is going to be good for me it's a very very simple loop as you can see if i select it again it's just going to be going around like this you'll probably want to make this look a lot nicer for you and have it doing something proper like again a road system i'm going to keep using that example because it's a good one this wouldn't work perfectly again this here really doesn't look that good but i'm just doing something nice and quick and simple so now all we need to do is drag in our cube just place it near the beginning like so and you can change the time to complete here again but we've already set a default value to five now if we were to simulate and go full screen alt p you'll see nothing has actually happened so i already know what was wrong we actually forgot to call the function so let's open up our kbp again back to event we can play we need to actually call the move object custom event we created something so simple but i somehow forget it all the time so let's compile save and try this again so let's go full screen alt p and there we go you can now see it's moving along now that's actually going quite quick but that's just because it's taking five seconds to go along the whole thing and you'll notice at the beginning and end it's slower than it is at the rest that is because i had it curving near the beginning and end so it's kind of speeding up and speeding down as it starts and ends which again would look good for a car so it's not just instantly going 50 mile an hour it's speeding up and slowing down which i think looks a lot better and you'll notice the very beginning as well really because of how i've got it going you can't notice that it's snapping so i think this looks great so i think that'll be it for this video as we've done everything we want to do what we've done is we've set up so we have an object now moving along a spine path which we have set up and again if you want me to advance upon this even further i can infer the videos if you want so let me know in the comments down below and that can include actually adding a road mesh to the spline changing the cube into a car making it so we can have multiple instances of this in the level so we have multiple different road networks all working so again let me know in the comments down below if that's something that you want to see but again in today's video what we've got is just on a simple object following a simple spine path 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] you
Info
Channel: Matt Aspland
Views: 103,281
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, spline, path, object, ue4 object follow spline, follow, ai, npc, car, road, ue4 spline, splines, how to use, use, make, create, follows, along, move, spline path, dynamic, automatically, automatic, moving, moving an object, along a, mesh, meshes, blueprint, blueprints, actor, roads, network, system
Id: -V6D5WtemMI
Channel Id: undefined
Length: 16min 11sec (971 seconds)
Published: Sun Feb 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.