Unreal Engine 5 Tutorial - Advanced Line Trace

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] thank you what's going on everybody James here from artificial entertainment and welcome to another Unreal Engine 5 tutorial and today we're going to be looking at some Advanced line Trace usage so let's go ahead and open up a project and dive in so I have here a third person template project now a couple of things I do want to advise before we move forward is that this is a project file in Unreal Engine 5.1 however I discovered this system in 5.03 so I know it works in both I'm just using 5.1 primarily because there are a couple of different things you can do with the new input Action System that you can't necessarily easily do in 5.0 so it's just some things I'm going to add in at the end but everything that I'm going to be showing you today is accessible in 5.0 5.1 and most likely 4.27 because it's utilizing nodes that have been around for quite a while now the second thing is this whole tutorial is going to be based off of just showing you two examples of what you can do however there is an infinite number of possibilities that you can do with the system it's entirely up to your imagination what I'm basically showing you is a more effective way to use LINE traces to be able to create complex traces off of a single execution that's the goal of today's tutorial so we're going to go into the third person Blueprints and BP third person character now one thing I always advise is that if you're setting up line traces especially for something like motion warping or anything that's going to be like a complex Trace with a few different variables you're going to save but only use them for the purposes of that Trace sometimes it's better to set them up as functions rather than trying to set them up in the graph themselves so we're going to use the function section to be able to actually create our line Trace so we're going to go functions add a function and we're going to call this line Trace all right so now the first Trace I'm going to show you guys it's what's referred to as a three-point Trace there's a trace that goes out from the target forward and if it hits something there's going to be two additional downward traces and we're also going to do an object detection Trace so something like you would use for motion warping for a melee combat attack it will give you two motion warping locations as well as a check that's going to see if there's anything in the way when you try to warp and again we're doing this all off of single execution now for single execution complex traces realistically there's two things that make this entire thing work so the first thing is we'll pull up the line trace and we're going to add that first one in which is a for loop with break so the for loop with break essentially allows you to Loop a section of code and you can choose the index value for how many times it Loops so for example if we set the first index to one and we set the last index to 10 it's going to perform nine traces however if we set this to 11 it'll perform 10 traces because we're setting our first index to 1 which I do recommend doing even though technically speaking zero is a value within the system of code um I found issues when you set the first index value to zero so if you set it to one and then just set the last index to one above however many traces you want you'll have it work just fine now from here this output index value is going to be one of the most useful things to make sure this all functions properly and we're actually going to want to right click and we're going to promote it to a local variable and this is why we wanted to use the function because this way this local variable doesn't get messed within all of our main variables for whatever you're coding within your game and we'll just take this and we'll name this line Trace index now you want to make sure to hook this up off with a loop body because we want this to update every single time the index value updates so if it's going to start at 1 then go to 2 3 4 and so on until it reaches 11. we want to make sure that this variable updates every single time that the value updates now from here off of the set here we can just pull off and we get the standard line Trace by Channel now this is really all we're going to be doing um from here it's going to be just setting different Vector values for the start and end because as I'm sure you guys know when you're trying to set up multiple traces like something to go to and from the target then another one is a downwards and another one that's back and forth usually you'll need multiple traces to do that but there's actually a way to be able to control which Vector value is going to be input utilizing something called a select Vector so we'll get to that in a minute it'll make more sense in just a moment but the first thing that we need to do is get our initial trade so this is going to be the trace that's going to go forward from the character and that's the standard line Trace so we're going to right click and get actor location right click again get actor rotation pull this back here pull off of the return value and get the forward vector and then we'll pull it off of the forward Vector here and we'll multiply it changing that bottom node to a float single precision and we'll set the value here to something like 400 just so that way it goes out far enough from our character now one thing that this system does require is you split out your uh start in your end locations so while the get actor location is going to be the start and get after location in addition to a rotation value is going to be the end we're going to duplicate that after location so this way we can keep this separated from this and then from here we're just going to pull off get an add node and hook these up together like this so this way we have our end and our start now what we can do is we're going to take these pull them down a little bit and we're going to get our line Trace index value we're just going to get a get node not a set node so my bad and off of here we can pull off and we're just going to get an equal so we're going to see if our index for our line Trace index is equal to one so this is going to be the first thing we do and we'll just take this and we'll duplicate it because we'll need two copies of it now here's where we use that select Vector I was talking about so if you pull off the get actor location and just type in select you'll see select Vector as an option here so we can click on this and you'll see we have two input values for vectors A and B and then we also have a Boolean value for pick a so this is where using this can actually be really really really awesome to create some really cool line traces so we're going to take this index and we're going to plug the Boolean value into pick a so if it's equal to one we're going to pick a and if it doesn't we're not going to choose anything because we haven't given it any values for B now this is going to be important later but just make sure don't hook anything up into the B value now we'll take the select Vector duplicate it and again we're just going to plug this output into a and this Boolean value into pick a so this way if it's equal to one and again that's what we set our first index as so this is going to be the first thing that it does now I know it looks a little weird from your standard Trace but if we go ahead and plug these into the start and the end and change our draw debug type to for duration compile and we just set up a quick keyboard keyboard event e just so we can do something to call our Trace and I'll just go ahead and bring our line Trace function off of pressed like that so again this is just setting it up a little differently I'm just showing you guys that if I hit e we now have a trace that goes out in front of the character going 400 units forward just like your normal line Trace would be so now this is where we're going to start getting clever and making it so that way we can use this single execution to perform a more complex Trace shape so the first thing we want to do though is we want to perform like a validation check to make sure that if we hit something that you know we're able to set the values now again this is mainly if you're trying to make it so that way if you hit something continue forward there is also another type which I'm going to show in the the second example where you're not going to actually use the if it hits to cancel it out but for right now for this one we are going to use we want to make sure that it hits something before we continue on so we're going to go off the return value for out hit and we're going to add a second Branch with b and left click and hook the true up into that branch so basically if we do hit something what we also want to verify is if the line Trace index is equal to one because we want to make sure that we're only looking and setting off of that first Trace because this is going to be the one that allows everything else to flow so to clean up my graph a little bit there um but now so if this is true what we're going to do is we're going to take the out hit break the hit result expand it and we're going to take the impact Point pull it up and we're going to promote it to another local variable and this one's going to be initial impact location or impact Point whatever you want to name it let's go initial impact point we'll go into here off of true just like this now because of this though we also want to make sure to go off of false we'll just duplicate it and then if we don't get a hit we're also going to okay you know just check and make sure that we are within that first index value because if we don't get hit on other things we don't want to cancel out our information so Alpha false go into the branch here and again this is just if we don't get a hit checking to see if our line Trace index is equal to one so off that first Trace if we don't get a hit off of that first Trace we're going to make sure to set initial impact point back to zero and then we're going to take this execution pin and we're going to plug it into the break because this is going to be the thing that allows us to be able to control if we do or do not continue on with our trace and this way values and information don't get left behind because we want to make sure that anytime we're setting information we also have the ability to reset set information as well so now that we have the ability to set our information if our first Trace is hit and if it's not hit we're resetting everything what we can do now is use a very simple node to be able to give us more position so I'm going to take all of this we're going to move it back and we're going to get another select Vector we'll just copy the select vector in the line Trace but this time we're going to see if our line Trace index is equal to 2. and we're going to undo the return values here and we're going to plug this into B so we want to make sure that the one before the select Vector that's hooked into the next Vector select is is hooked into B because this way if it's not equal to 2 it's going to choose P but if it is equal to 2 it's going to choose a different value and this is where you can use this to chain these different Vector selects together and have multiple different options for different things that it can choose from and these are realistically the two main things that make this entire system function select vectors and line Trace index but from here we can actually use something called a lerp node to be able to actually Now define a point between the two locations and that's why we wanted to make sure to set this if we hit something so what we're going to do is we're going to take that initial impact point and we're going to get a reference to it and then we're also going to get actor location now from here we're just going to pull off the initial impact point and grab a lerp and you'll see lerp Vector now what a lerp is is essentially just allows you to be able to define a point between two points so if I hook these two up into here and if I just scroll over you'll see linearly interpolates between a and based on Alpha 100 of a when the alpha is zero and 100 of B when the alpha is one and you can set values in between like 0.5 if you want it to be right in the center so and that's actually what we're going to do here so we're going to take this Alpha value set it to 0.5 and then we'll just take it drag it down a little bit and we'll plug this into a now we're going to take this and duplicate it because we're going to use essentially the same value but we're going to make one Mega change or one minor change that makes it make a difference I guess however you want to look at at it but we'll plug this return value into B and just copy that select Vector because again we're just basically copying the same stuff and just changing the line Trace index equals so it's going to be equal to one it's going to do these two if it's equal to two it's going to do these two but what we're going to do offer this lerp Vector is we're going to take it and we're going to subtract go to the Z value and we'll just subtract 150. now this value you might want to play around with a little bit just so that way in case there's like uh terrain angle differences that you don't want to make it so that way like if they're like completely off of a cliff Edge or something like that they're not able to get the point so play around that Z value but it's really just up to whatever you need so now what we can do is we'll just take the select vector plug both of the return values back into the start and the end since we've added in one more point and go ahead and click play and now what will happen you'll see that we get the single line Trace that goes out but nothing else happens but if I go and turn around and I hit this you'll see that now we get a trace going down directly in the center and I can be closer to it and it's still going to be directly in the center because it's it's getting a linear interpolation between the two vectors and then getting the Half Point that's what I've set it to so now what we're going to do is we're going to change this value we're going to change it to 0.2 on both of these so that's one thing you do want to make sure that both lerp vectors are the same value because this way it'll make sure it's straight up and down if one's different than the other it'll create kind of like an angle um but what we're going to do is again we're going to unhook the start and the end here and we're going to add another select Vector this time we're going to see if it's equal to three and then again we're just going to take this highlight it duplicate it move that down a little bit and then we hook it up again and as you can see there's there's literally a straight pattern to this it's just getting the select vectors A Boolean value to control which Vector is being selected and then using that nice lerp to node to be able to actually set the point on the line itself so what we're going to do now is basically the same thing but instead of setting the alert value to 0.2 we're going to set it to 0.7 and that's all we're changing we're going to copy the exact same Vector values except the only thing we're going to change is same changing that alert Vector 2.7 because again the point two it's going to bring it closer to the uh the actor and a 0.7 is going to bring it closer to the Target so we'll take this alert Vector plug this into here make sure that we have our subtraction node in here accidentally didn't copy that now you do with all of these nodes it is a good idea to try and make sure you keep your graphs clean I usually take a lot of time to try and keep this all very neat but it does take a little bit of time to properly clean up so just keep that in mind there's a lot of things going on but what this allows you to do is set up single execution line traces because if you have any frame drops or any lag spikes or anything like that and it's right in the middle of two line Trace executions and they're both needed to be able to get information for an action that you're trying to complete sometimes it can cause problems but if you can set all of your traces up off of a single trace execution and just have them do different things like this it will actually allow you to do more because now you can see we've got the same value but we're going to set this value to 0.2 on this one and 0.7 on the other one so we need to make sure to set this to point seven oh no not seven point seven all right and then we'll compile and Save we'll go ahead and just minimize this really quickly and we'll click on play so now if I go and I turn around again you know we get the line Trace that goes out doesn't do anything if it doesn't hit however if we do get a hit now we've got two points and again it's getting a value of 0.8 for that first one or point seven for that first one and a 0.2 for the other one so we've got these two locations so now for the last part here so now we've got a check to see if we're actually hitting something if we are hitting something we're going downwards and getting two additional impact points so the last thing that we need to do to be able to complete this is just add in that check to make sure that there's no obstacles within our way before we actually perform the trace so we're going to go ahead and just unhook the start and the end here and this is where we're going to change this value here that the Boolean is being selected so we'll can take this and duplicate it but we're going to take out the is equal and change it to a greater node and we're going to see if it's greater than three so you basically you want to make sure that it's set to be greater than the previous value that you set so this is three so I want to make sure it's greater than three so that means four five six all the way and so on so we'll take this plug this into pick a and then we'll take this and duplicate it so this way we can have a start and end position and we'll plug up B into both of these sections so now there's a way that we can use the lerp vectors in combination with our line Trace index to now create a multi-trace that's going to happen now you guys might have seen something like this in the motion warping tutorials on YouTube for like vaulting and things like that um but we're actually going to work this in to the last thing it's going to do at the very end of our first Trace so we can take the line Trace index so we'll grab a get node and what we're going to do is we're going to take this and we're going to multiply it but we want to make sure that the output here is of a different value so we're going to take this and multiply it by in 5 for right now now this is essentially going to be the Gap because we're going to have multiple traces happen at once and it's going to make more sense in a moment but this is essentially going to be the distance in between all the traces that are going to occur so we'll set this to a value of five but then on the execution pin here we're going to right click convert it to a vector value right click it again and we're going to split the struct pin now we also want to get an add node as well but we're going to go ahead and grab this lerp Vector first and we'll just push this out of the way because we're going to take this lerp vector and we're going to add it to this so we're going to pull off the lerp get an add node we'll plug it into the bottom pin though we don't actually want it on the top pin because we're going to right click on the top pin and split the struct pin now why it is that you have to do this and have both of them split I don't know but I know that this is the only way that it works so make sure that the split on the multiplication is done as well on the addition node because then from here you can plug in the two Z values like so and then plug this into the a for select vector and now what we'll do is we're going to take this entire section here and we already actually have the select so we don't need that and we're going to duplicate it because we're going to use essentially the same code we're just going to change the lerps value that's the only thing we're going to change so we're going to take this one plug it into the A and we're going to set this alert value to something like 0.7 and the top one here we're going to set to one because we want it to start at the actor location and then go forward out now you can set it to whatever value you want but I'm using 0.7 because what's going to happen is it's actually going to stop right at where we um set our last location for this will make sense in a moment but we'll go ahead and plug in the return value of this into the start and we'll plug this into the end now we also want to make sure to change our Trace index from 11 to 5 because otherwise it's going to create a ton of traces and we don't need that many so but now if we go and hit play and again single trace goes out if it hits nothing doesn't do anything however if we go we hit we now have two traces that are coming out but as you saw the traces are a little high and we're also you know we're not going forward because we said that lerp wrong and that's my bad so we'll go ahead and we'll fix that because we're going a value of one and then a sort of value of 0.7 we're going to set this to a value of 0.3 and we also want to make sure on the multiplication we're going to set this to negative five instead of five the reason why I started at five is because I wanted to show you guys the difference between using a positive and negative value because now if I go again we get the single line Trace turn around though now we got two traces that are going from the bottom and going down now what we can do to be able to adjust this is we can add instead of five traces we can go and do we'll do eight traces instead negative five we'll do negative ten and this is I'm just showing you guys like what values to play around with to be able to kind of get the fine-tuned adjustments that you're really going to want because again I'm just playing with single values and then hitting play and it's going up and hitting a wall and now you can see we've got multiple traces happening and it's really stopping it right about knee level so the only thing I would want to do now is maybe increase that alert value so this way it'll go more towards the initial impact point so we'll set this to maybe like 0.1 then again we'll hit play and then it kind of goes a little bit past so this is where again you just play around the value of the lerp and whatever works for you works for you but again now we have multiple different traces happening if it doesn't detect anything there we go point two worked if we don't have anything happening we can see that now we're just doing an object detection and we're getting all of our initial information but again this is all happening off of a single execution so and this video did run a little bit longer so I'm actually going to we're going to do the next Trace style um in the next video where we're going to show you how to be able to do it without having an initial impact Point as well as being able to choose like vectors uh change like left Vector right Vector for things like Corner detection and stuff like that and realistically this is the main part of everything just utilizing these select vectors with a Boolean value and using the line Trace index value to be able to set which value it's actually going to use and out here you can use whatever index value so like for example if I wanted to use motion warping and I wanted to set 2 and 3 3 as by locations I could go when we get an out hit I could just do if the inline Trace index is equal to 2 and equal to 3 so we go one branch for two one branch for three and then it goes and you know sets an a warp location you know warp Target one warp Target two so there's a lot of different things that you can do with this system but again the the key thing with this is that it's all off of one Trace you don't have multiple traces going off and if a hiccup happens you're going to end up with issues in your code this allows you to be able to have a little bit more of a complex check but more efficient data within your actual game itself so I hope you guys enjoyed and again I apologize that we're not going to get into the second one I just don't want to do an hour long video so we're just going to cut it off here today um and then the next video we're going to go over how to be able to do a different version of the same type of system so if you guys have any issues please let me know um you know I'm always active in the Discord I'm always checking the comments so if you guys have any issues with this uh please just let me know um but that's going to close it out and as always guys stay animated
Info
Channel: Artofficial Entertainment
Views: 2,979
Rating: undefined out of 5
Keywords: Unreal Engine 5, UE5, Unreal Engine, Game Dev, Line Trace, Advanced, Multitrace, step trace, Line, Tracing, Tutorial, How to, Advanced Detection
Id: PI1CcHqkrkk
Channel Id: undefined
Length: 21min 30sec (1290 seconds)
Published: Thu May 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.