Unreal Preview Navigation Path

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone so today i'm going to show you in this top-down template a preview of the path this character will take before it moves before it start moving so it's like this so as the character moves the path is shown like this and as we move around the cursor it in updates the path in real time that will be taken by the character if we click on this point when we play the game right so let's see how to implement this today and this episode is sponsored by this generous patron thank you very much for your support right um i'm gonna select games next and top down template here name this says show path create project right so here we have the usual top down template which moves like this okay so what i want to do is when i click or when i like move this cursor i want to preview the path this character will take in order to in order to go to that point okay so in the top-down character first without having a spline mesh let me add just add a spline component let me make it longer like this okay right so this has only two points and if i go here we can't see this line in the game so let me splines not visible in the runtime so let's use let me delete this let's use a spline mesh and as the mesh okay i think i have another mesh that i um i'm gonna use this cylinder right okay it's too large ah yeah we can't just just one spline mesh is not enough we have to use this alone with the spline compart okay so this is for testing again let me add a supply just a spline component now it shouldn't be a child of spline mesh right so this spline is just for testing we have added in the runtime so in here uh let's just this is just for preview then we will promote this to a function and do it in the runtime this is just the previewing of this blind mesh part to show the path the character would take so here first we have to get number of spline points because um okay we have an option called number of spline segments okay i think we can use this one also right now using this let's do a whole loop first index should be zero last index should be the number of spline segments minus one because index starts from 0 and if there are two segments that means the index are 0 and and yeah so now here we can add spline mesh component so here as the mesh i'm gonna select the cylinder i just imported and it's nothing special just a simple cylinder mesh and this doesn't have two-sided rendering enable that's why i don't see the inside but that's okay right display mesh component and here let's set start and end so we need start position and instead tangent and position and and tangent so now we have this index so from this spline we can get [Music] position location and tangent at the spline point right now what's the point we can use this index so this is going to be the starting position location and tangent and i'm gonna use local space so for the n position and tangent i have to use the same function but one added to this because it should be the next point in the spline and and location and tangent right now let me delete this here you see this dynamically added this added cylinder but it's too large can we make it smaller yeah here let's select this node and set the scale of y and you said to 0.1 then compile now you can see it's a much smaller spline right now if i update this spline you see oh wait we can't change the scale see it mess up all right so let me keep the scale same or change this one also no we can't change the scale this has to be one by one by one right then can we make this smaller in the mesh itself and import scale yeah but if i make this point one and reimport okay now it's small and here oh no oh it's fine yeah it was just i had to recompile okay now if you can see as i add more points this path is updated so what i'm going to do it in order to show the path i'm gonna uh do this in the runtime so first let me move this spline down oh wait i can't right anyway then let's keep it in the same location also you don't need this spline mesh right now we do have a way to show this is flying path but still this is not complete oh we need to disable collisions as a we can do that here oh it's already don't have any questions and noble appearance all right now shall we collapse this part to a macro not necessary to be a function let's call it at meshes right and we don't need this in the construction script so i'll disconnect this that's only for preview purposes oh this one giving us a warning we don't really need that yeah okay okay now the next step is when the character sorry when the player click on one of these places so when we move the cursor around the word we need to show the path that this character would take right and we already have this cursor to word object which is in the game this yellow green circle that you can see right under the mouse and you can see how the location is calculated in here cursor to word location is calculated using get player controller and git hit result under cursor by channel and from that we get the location so we already have the location pointed by the mouse cursor right now let's add a function uh no not a function let's add a custom event show path right so now here now we need to know what the path actually is so for that we can use this cursor to word get word location and from the navigation system all right here we can use this find path to location synchronously and from this we can get path points right so this is array of vectors here we need to set the starting point to get that location right so before the spline meshes let's just add spline points to the spline so first get a reference we need to clear existing spline points right then from this we can get path points so here we have all the points we can run up for each loop to get all the access over the path points so here again on the spline component let's add spline point like this and may here make sure to use the word space otherwise we will get incorrect result and also the first point of the spline should always be the current location of the actor so at spline point as the input so before running this as the input make sure you use detector location as the position and also in world space right right now let's preview this oh in order to show the spline we have to use this show splines now we can see this plant but you can see it's not visible because it's under the it's under the flow so all right so what we can do here is uh but also i think we already get the position right below the character so we may not need to add this point only the path point should be enough right so here let's add let's set 30 in the same direction so it should be it will be above the ground right now you see the path is drawn as we move around the cursor if i click you can see spline is also following which is not cool so what we can do is let's uh when we call this show path or actually in the beginning we can get this spline and do a detach from component so it will not be attached to the player when it moves the points stay but here it is updating as the character moves so i guess we should not do that so how do we prevent doing it right here in the show path let's uh we have movement component that can access the speed now it follows okay we should show this only if this is equal to zero or roughly equal to zero let's set a branch here so when the character is on the move the spline should not update see only when the character stops now we get okay right now how to add the spline meshes for this and also we can do one more optimization technique for this we don't always have to update this we don't have to update this in every tick if the target location is not changed so we access the target location like this using this cursor to world so let's promote this to variable let's call it target location no pre-target location because this is going to be our previous target location right and when we call this so far event next time we can compare the current location uh the distance between the current location of the cursor and the previous location like this between this one and the previous target location if the distance is uh larger than if this is larger than let's say 50 we should redraw the path the branch here okay all right otherwise we don't have to update it let's check again show spline see now for a small variation a small change in the cursor it doesn't update that's good for performance especially when we have these blind meshes in place right the next part is how to add the mesh component um right we already have this add display and mesh part but here we actually once we add spline meshes we don't really delete them uh that's not good so let me promote this to a variable let's call it spline meshes and disconnect it and turn this into a type of array right now we don't need it here the reason to do it is so that i can automatically get this plan mesh component variable type here so right then once we add a spline mesh we can add to this array okay and then uh before yeah this function this macro will also be called called more than once so we have to run for each loop on this and destroy previous components before we add new ones like this and also need to clear this array otherwise there will be older invalid references oh okay is that all also here make sure that you have make this moveable because otherwise you get the same warning we got earlier oh we didn't use the macro here in the show path so after adding spline points we have to call add spline meshes let's connect this completed into the output it's not necessary but otherwise let's say if we have something to call after this point we can use it right now let's see i don't see anything why ah why don't we let's again test it by keeping this part and even though we detach it let's keep world right so is this macro not working what is working then what's wrong yeah maybe this was the reason now let's check in the runtime oh yeah it is working cool it is okay okay all right so [Music] if i press move oh one more thing we have to when we add the spline meshes we have to detach them also from component otherwise they will move along as the player moves right now see they don't move oh wait why it's not showing properly right i think i should use word space here oh no no no no right actually i solved the problem we have to keep this in local space and we don't need to uh detach the spline component from the from the character because spline component is not going to be visible in the runtime in the game if you were just visualizing it for debug purposes so but we can detach the spline meshes then this works properly so see it works perfectly now the only thing remaining to do is uh showing this adding a better material to this cylinder to m split let's make it translucent and defined a color like blue and add a little bit of animation 2 yeah like this and opacity let's set it to 0.5 right we can adjust this if we want and let's assign it oops material sorry that's the material and spline right now it looks like this okay and that's all and as always project files will be available for the download through patreon page link would be in the description below and if you like to support my work you can get the membership of the patreon club see you in another episode bye
Info
Channel: CodeLikeMe
Views: 21,078
Rating: undefined out of 5
Keywords:
Id: 3h1U6UWLCSQ
Channel Id: undefined
Length: 31min 4sec (1864 seconds)
Published: Sat May 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.