Unreal Engine 5 | Lasers Tutorial Part #1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we're going to be making the following laser system in UE 5.1 we'll be making the following actors which is laser spawn laser target laser Cube and Laser reflects the laser Cube will be in the next video along with laser effect because it's more complex and would have taken too long too in the first video we're going to start off by making the main assets we will be needing throughout the tutorial first so we're going to start with creating a blueprint interface we're going to call it BPI underscore lasers next we're going to create a parent actor called laser parent and we're going to use it to create the following children a underscore laser underscore laser spawn and a underscore laser targets we're going to start with the parent class so open that one up first we're going to get a static mesh component which we'll be using in all the children to give them a visual next we're going to make a few variables the first variable will be a Boolean that we will call Active which serves to check if an actor is creating a laser or being hit by one next a linear color called laser color which we'll use to change the laser color later on make sure it's exposed on the spawn a variable of type a underscore laser spawn which we created earlier called spawn reference make sure it's exposed on the spawn as well this variable will allow us to indirectly track the origins of each laser and finally a variable to track all the lasers attached to a specific actor which we will call lasers attached and it will be an array of type a underscore laser we can close the parent actor as we won't need to make any changes to it for the rest of this video next open the laser spawn and we'll start by giving it a static mesh for the tutorial we will be using the basic cone but feel free to use any static mesh you want it has no importance and we're just going to move it slightly a bit up in the viewport so that it can be more easily attached to walls now add an arrow component make sure it is rotated and in a position where it's not overlapping the static mesh and is pointing the direction you want the laser to go the variables unique to the spawn we'll start with child lasers which is an array of a laser type next is a string called ID which will be crucial to the proper removal of lasers later and to finish create a variable called spawn laser and it is no spawn laser is never used so you don't need to create it now we're starting to get into the blueprints we can start by removing aventic and event begin overlap as we'll only be using event begin play from begin play we are going to drag off and set spawn reference to self as we're currently signing up the swan actor we're going to perform the same logic for the ID as it is in the spawn we want a unique ID so we will be using get display name and finally we're going to create our laser using spawn actor from class we search for a underscore laser and select it we split the transform now with our Arrow components we are going to get one location and plug it into the location and for the rotation we will simply get our actor rotation we search for get laser color and get spawn reference and attach them to our spawn actor to complete I'll begin play we add our new laser to child laser array now we're going to work on the key Act of the whole system that being the laser so we're going to open it and set it up just like we did previously we're going to give it a static mesh in this case though I suggest using the exact same one as me that being the cylinder in the level prototyping directory reason for this is because the pivot point is at the bottom of the geometry meaning it's easy to use and only needs to be scaled we're going to create an empty function and place it after the begin play it will later serve to change the color of the laser but we're first going to focus on making the laser work so we create a function called set endpoint we'll be calling this function in the event tick this function will contain most of the logic for the system we start it with box Trace by channel to determine the length of our laser for the start location we will be using get actor location and get actor up Vector to make sure that we don't Collide at zero we multiply the up Vector to start the trace in front of the actor we set a half size of 0.5 on all axes and for the end location we're going to multiply the up Vector by local floats variable we will call L range we are using a variable because we will need it later we then add the resulting multiplication to our start location and don't forget to set the default value of L range to something around 3000. from the box Trace we will save the out hits as a local variable called L hit info next we will scale our laser based on the hit result so we break our hit info and get distance we add one turn Shield laser slightly longer than the hits that it really hits the target then we divide by 100 to convert the distance to a scale we grab our static mesh component and set world scale 3D we want to split the vector set X and Y to 0.05 which will make it a thin cylinder and for Z which will be the laser length we need to put a select the reason for the sect is because if the Box Trace hits nothing it will return zero so we need it to select our L range in that case so we use the blocking hit that we get from L hit info for the select and we set distance to A and L range to B after having divided by 100. now we can compile and place our spawn actor inside the level to see if it is working as you can see it works as in tenant the laser appears and goes to the next wall but there are a few issues first the Play-Doh collides with the laser which we don't particularly want also a laser being a laser shouldn't emit a shadow to fix these we open our laser again and select our static mesh scroll down to Collision and change the preset to custom and we select overlap in the Collision response we can also set visibility to ignore as lasers will not be interacting with each other next we want to disable the Shadows so we do a search for shadows in the details panel and set cast Shadow to false let's run the game again and now we can walk through the laser no problems and we don't see any Shadows but it doesn't look much like a laser so let's fix that create a new material we'll call M underscore laser open it once it's loaded we hold for and press left click to create a full Vector right click on it and convert to parameter we'll call that parameter color and we'll attach it to our base color next hold s and press left click to create a scalar parameter for the intensity set its default value to 100 and finally hold M and press left click to create a multiply node attach the color and intensity to the multiply and attach it to the emissive color apply and save the changes and we have a complete laser material now back in our laser blueprints selectostatic mesh and apply our newly created material we forgot to set our default value to the color so I open the material again and set it to red next we can change our color function with blank earlier and put it to use get the laser color variable get the static mesh and search for create Dynamic instance select our M underscore laser material for the source material in the node drag from the return type and search for set Vector parameter value once you have that node type in color inside the parameter name and attach the laser color to the value we can now Run the game and the laser is not the correct color something went wrong so we forgot to change the variable on the spawn which determines the laser color if we change it to red and run it again it works as intended let's try with another color to ensure that it's working and even if we put it as blue we get a blue laser so everything is functioned correctly currently we have a laser that extends and is blocked by objects the next thing we want to do is add a Target to do so before going into our laser target we're going to use the blueprint interface we created at the beginning so we open the blueprint interface and we are going to rename the function that we see to Laser Bounce this function will have three inputs the first is a reference to the laser hit or more accurately the laser that is hitting the Target make sure to use the type a laser next is a Boolean called Hit response which will check whether we are hitting an object or not and finally we want to get the hit information of type hit results and we also need to add one output of type a laser which we'll call new laser it is the new layers that we will create once we hit an actor such as the cube or a reflective surface we can now close the blueprint interface because we won't be using it anymore as we just finished setting up the blueprint interface we are now going to make use of it within a laser inside of the set end point function from the hit info we want to save another local variable called L hit actor we want to check if it can cool our blueprint interface so we'll drag from it and search does implement interface patch the return value of this function to a branch and on true we'll be able to call our function Laser Bounce by right clicking and searching for it attach lhit actor to the Target and set hit response to true self to the laser hits and lhit info to hit information with this now in place we can place our a laser target in the level and put the final touches in the blueprint of laser target inside laser target where we start as usual by setting up the static mesh now we want to set up the blueprint interface to be usable inside this acting so to do that we open the class settings and go to implemented interfaces and click add and search for our BPI lasers once you have done that a new category should appear below functions called interfaces and we will double click on Laser Bounce to open it once inside we're simply going to add a print CFL function is indeed being called let's just align our spawn and Target and run the project and it is indeed printing correctly we should also check that it stops when we block the laser with an obstacle and it does indeed Stop Printing once a cube is placed in front of the laser the checklist without using a prints let's create a function called puzzle solved which will change the color of the static mesh to the color of the laser that's hitting it the function will need a single parameter of type linear color we need to do the same as what we did for laser color so we start by creating a dynamic material instance but instead of using the material we created we'll use something called M underscore solid as the source material drag from the return value and type set Vector parameter value and use base color as the parameter name but unlike with laser we want to use a select node with active to determine whether we use the parameter or simply remain Gray we now return to the Laser Bounce function and we set active based on our hit response parameter and replace the print string by puzzle solved we get our laser color from the parameter laser hits now let's run another test to see if everything is functioned as intended as we can see the target is blue like the laser let's see what happens when we block the laser now it doesn't turn back to Gray the reason for this is in the laser we never call Laser Bounce with it responds to false so let's fix that by opening up a laser and set endpoints to solve this issue we're going to need to create a new variable make sure it's not a local one call this variable last hit actor and of type actor first we want to check whether local hit actor and last hit actor different if they are we want to check whether the last hit actor could call our blueprint interface we want to make a copy of what follows here as we're going to be using it twice once with hitresponse false and once with hit response true on the branch equal true we want to change our L hit actor with last hit actor and we want to change hit response to false if they are equal we want to use what we copy to enable the hit actor if it can call our blueprint interface to finish this function off we need to send our last set actor to our local hit actor so to do that we're going to add a sequence node right after the set world scale 3D we use a validated get to ensure that we are not setting a null reference also we need to remember to make sure that we don't forget to change local hit actor to the last 10 actor on the if they're different Branch otherwise will not be disabling our previous activity disabling the new one so now we can run the game one last time and push the cube in front of the laser and our Target does indeed get deactivated everything is functioning as intended so that does it for the basics of the laser system in the next video we'll be adding the reflection and a cube thank you for watching and making it all the way to the end as this is my first time making a tutorial please let me know if you enjoyed it or if you didn't let me know how I can improve for the next one the second part will be the last and it will be coming out in the next two days
Info
Channel: Patsanoob
Views: 4,558
Rating: undefined out of 5
Keywords: Unreal Engine 5, Puzzle, Lasers, Tutorial, part 1, blueprint
Id: 74TRpvuaKfs
Channel Id: undefined
Length: 22min 2sec (1322 seconds)
Published: Wed May 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.