Souls-Like Melee Combat Tutorial - Target Lock On System [#5] [UE5]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another part of my Souls like combat system tutorial Series in this episode I'm going to show you how to create complete Target lock on system so firstly let's create an input action right click input input action and let's call it Target lock on open it and add one trigger this one will be tap and now go to the input mapping context add one mapping the target lock on and select whatever you want for me I'm going to use Q now let's create an actor component that will handle all of the target lock on system so create an actor component and name it Target lock on component open it and create three variables right here first one will be lock on radius it will be float then we will have lock on class this will be ector but it will be class reference and both of these will be editable and expose and spawn and now let's create one more variable called Target locked on and this will be just a simple actor now we need to get player character reference so get player character and cast it to the third person character and because the player character is third person character we can just convert it to Pure cast and now just promote it to a variable called player reference now we can actually start to create Target lock on system itself so right click create a custom event this one will be called trigger Target lock on and also let's set it up in the third person character so right click get your input action then we need to add the actor component itself so add Target lock on component get it and Trigger Target lock on now when you select the target lock on component you can see on the right side that there is lock on radius and lock on class I'm going to set the radius to 1,650 lock on class for me is going to be of course the master enemy now first thing we need to do is create a sphere around the player and check every Target that is near the player so for that let's create a function this function will be called trace for targets it will be pure and it will have one output of the type ector it will be an array and it will be called targets so firstly let's create the sphere around us so multisphere trace for objects and the start and end will be the players location so get player reference and get AC location of course the radius will be lock on radius and normally you would just make an array in the object types and you would set it to pawn but there is a big problem with this and I'm going to show you what I mean let's just first check if we actually hit something and then the out hits I'm just going to get length you don't need to do this I'm just going to show you how it actually bucks if I'm correct it also does in anal Engine 4 not only five now I'm just going to untake the pure for now and get this so you can actually see that when I hit Q it says 23 on the left side but there is literally just one enemy here and it doesn't make sense and it's just because the mesh is actually created from multiple parts and each and every part of the mesh is actually in the trace for that to actually change so we won't have insanely costly racing we need to go to the project settings type channel right here and create an object Channel just by clicking on this set the default response to ignore and I named it lock on Pawn and when you've done this go to the master enemy and add an Collision right here it doesn't matter what Collision but I'm just going to use capsule collision and I'm going to call it lock on collision and just this lock on collision when you scroll down at the right side set the Collision preset presets to custom set everything to ignore and the object type will be lock on Pawn and when you do this actually and go back to the Target lock on component set this to Lock onon And now when we hit play and click Q it just gives us one output I really don't know why this is still a thing but I guess it's just how it is programmed and this is the workaround around it so go back to the Target lock on component I'm just going to delete this and set it to Pure also delete this and let's actually continue so when we actually hit something let's for each Loop the hits break the hit result from ARA element and check if the hit actor is child of the Loon class so first we need to get class and then we need to type is child off of course the parent class is lock on class and the return value will go to the branch now if this is true we need to set the target to an array so create a local variable and call it local targets it will be an actor and of course an array now just get it and add to it the hi actor and when the for Loop is completed we just output the local targets now with this we get all of the targets that are around us but we need to get just the one closest to the center of the screen let's just imagine that there is something in the middle of the screen and that will check which Target is closest to the center and that is the one that we actually want to select so for that let's create another function this one will be called check for closest Target it will have one input of the type ector array and it will be called targets and one output actor single Target to lock on also we need to create two local variables right here first one will be local compare float of course it will be float and another one that will be called local Target to lock on and that will be an actor now let's for each Loop the input targets and now firstly we need to check if the target is actually not behind some kind of obstacle or wall so for that let's line Trace by channel the start will be the world location from the player camera so get player reference get follow camera and get World location and the end will be get actor location from the array element for each Loop now don't forget the trace channel is visibility so if your enemy doesn't have the visibility right here to block it will not work so set it to block now let's check if we hit something and if we did we need to check how close the target is to the center so for that create a function called check how close Target is to Center this one will be pure and it will have one input of the type actor called Target and one output of the type float called return value now to actually check something how close it is to the center of the screen you do it with this kind of math so from the target let's get actor location also get the player reference and get actor location from him find luad rotation and the return return value get rotation X Vector now again from the player reference we also need to get the follow camera because we are checking it from the camera not from the player and get forward Vector of the camera and now we just need to dotproduct these two numbers and the closer the number or the written value is to one the closer it is to the center of the screen so now get back to the check for closest Target get the function that we created the target will be array element from for each Loop and with this we can just compare float the input will be the return value from the function and output will be local compare float which is zero and if input is bigger than local compare float first thing we need to do is set the local compare float to the return value from here and then set the local Target to Loon to the array element from for each Loop and when the for each Loop is completed we actually get the Target that is closest to the center of the screen so let's just output the local Target to lock on now when we go to the event graph and grab the check for closest targets oh I actually forgot it should be pure now drag this to that and firstly check if this is actually valid because if it's not then it means that nothing targetable is around the player but if it is let's just set the target lock on to Target to lock on and now we can actually set the target lock on system because we have the targets to lock on so you can do it with event check or set timer by event it doesn't matter I'm going to use set timer by event the time can be Delta seconds it will be looping and the event let's just create custom event and this one will be Target lock on and if we don't want to lock on anymore let's just create a custom event called stop Target lock on and it will just clear and invalidate the timer handle itself now for the Target lock on first thing we need to check is if the target is actually still in the lock on radius so get the target lock on and get player reference get their actor location and check the distance and we are checking if this is greater than lock on radius and if it is we want to stop the target lock on itself now if it isn't we need to update the camera itself so it will always face the target we are locked on so first get the player character and get controller from this set control rotation and let's split this struct pin here because we will change some numbers here so firstly get control rotation and break Rotator and the x value will be the same so let's just drag it here but Y and Z will be kind of different so for that let's actually create another function this one will be called get lock on camera rotation and here we are actually going to set the math to rotate the camera itself so for that let's get the player reference get follow camera and get its World location then find look at rotation and this return value will be the return value that will be outputed in the return Noe so let's create a return Noe and just drag this here to create a pin now we have the start but for the Target we actually need to get the target locked on and get actor location now split the struct pin and also make Vector the X and Y will be the same but the Z number will be different so for that let's get again the player reference and get the target locked on again get their actor locations distance and this will be divided by two and then the return value Z will be subtracted by The Divided number right here let's just plug everything in so what this actually does is the camera will be always spanned in a way that player can see his character and also the enemy character at all times because when you don't see the enemy character the combat system itself is kind of iffy let's just face it and this number right right here sets how much the camera will be panned which means how much this will be happening I found out the value two is good but you can change it however you want I'm just saying it so you actually know that it is here and is is changeable now go back to the event graph get the get lock on camera rotation and let's just reer 2 the current will be get control rotation and Target will be the get lock on camera rotation now the Delta time can be 0.01 inter speed can be one and the return value will be split and the Y will go to the control rotation and Z also and now when we play and hit Q it is really slow so let's set the inter speed to for example 10 yes now it lcks on properly and you can see that the enemy is always visible on the screen as I said you can change the number right here and I'm going to show you what it actually does when I divide it only by two you can see the camera is Spann in different way and when it's going to be for example 10 you see that the player blocks the enemy so as I said two is the best for me but you can change it however you want so now we can actually lock on to Dynamic and when we copy the enemy multiple times right here I'm going to show you that when I look on him it locks right there when I look on this character it locks on that character and when I lock look at the middle character it locks on the middle character but we still cannot switch between the characters themselves so let's change that in the souls like games when you have Target lock on already and you move your mouse to the left it will Target another character on the left and the same goes for the right I actually don't know how to do that if you know let me know in the comments and I'm going to pin your comments so everybody else can have that knowledge but I'm going to use Mouse wheel up and mouse whe down for now so right click Mouse wheel down and mouse will up but we still cannot assign anything to them so go back to the Target lock on component let's actually create two custom events here first one will be switch Target on left and the other one will be switch Target on right so firstly what we want to check is we if we are actually locked on so get the target locked on and check if it is valid and if it's not we don't want to do anything but if it is we actually want to switch the character so let's create a function right here let's call it get Target on left it will have one output of the type ector called left Target and this one will be really similar to what we did before so get trace for targets for each Loop them and from that we want to line trace for objects again the start will be the Camera World location so get player character get follow camera and get World location the end will be again from array element get actor location and the object types let's just make an array and it will be Lo compon now firstly let's check if we actually hit something and if we did let's break the hit result and actually firstly let's check if the target is not the target we are currently locked on so get the target locked on and also type not equal and it shouldn't be equal to the array element now we can get branch and also end Boolean and the other one will be the check that will actually check if the character is on the left side of the screen so for that let's get the player reference and let's just copy the follow camera right here get right Vector we need to do product this and we will do product it with the normal from the hit result and then we just need to as in degrees and check if it is greater than zero and that's all you need to do to check if the character is actually on the left side of the screen now the last thing let's just create again the local variables called Target compare float Target to lock on and again check how close the target is to the center the target is of course the array element and we need to compare the float with this as an input value and local Target compare float as compare width and if it's again greater set the local Target compare float to the return value from here and again local Target to lock on will be the array element from for each Loop and when the for each Loop is completed we just want to Output the local Target to lock on now the same thing will be for the right side so let's just right click on the function and duplicate it and let's call it get Target on right and the only difference in this is that this right here is not going to check if it's greater than zero but it will check if it's less than zero and with this you get the targets on the right side of the screen now go back to the event graph and get the Target on left and also on right and set it to the Target locked on now when we go back to the third person character we can just copy the target lock on component and switch Target on left and also switch Target on right now when we play and actually lock on someone and use Mouse wheel it will go to the left and then it will go to the right and we actually got one error here so let's check it actually I know what it is it is when we get Target on left but there is no Target on left it still sets the target lock on so let's just check if this is valid and with this there shouldn't be any bucks so let's just copy the enemy multiple times and now when we play and actually hit q and use Mouse wheel you can see that everything is working as it should you might experience some sometimes a bug but this is the best way how I was able to make the lock on system with switching so yeah now we can actually set the movement of the character to correspond how it it would be with the souls like games so for that we need to actually create a blend space so right click animation and create blend space right here let's select your mannequin and I'm going to call it BS Player lock on and there are multiple ways how to set up the 3D blend space I'm not the best at it but I'm going to try my best to show you how I am doing it so firstly the horizontal axis will be the movement Direction and it will go from minus 180 to 180 let's set the grid divisions to eight vertical axis will be movement speed and for me it will go from 0 to 400 also check the snap to grid and now let's start so firstly we need an idle I'm going to also set it on the furnace right here then we need walk animations oh and also set the grid divisions here also to eight for me so I can actually go and set this to 150 movement speed now the right side will be the right side of the animations and left side will be the left side of the animations so right here let's just get the V front 45 to the right then we want to strafe to the right then walk back right for 5 and also at the end we want to walk back the same thing goes for the left side so front left left back left and back because this is 100° so this means it is walking backwards this is literally just degrees and I just set it up to eight divisions so it goes to 45 each one now up here will be the Running Animation so let's just type run and firstly here will be run front then at the corners will be run back run right will be at the Middle Run left at the middle right here and then front right back right front left and back left and now when you hold control you can actually see how it will look but for it to actually be used we need to go to the animation blueprint set up the direction right here and you calculate Direction pretty easily just drag from the character reference get actor rotation and from velocity calculate Direction the base rotation will be actor rotation and this will be promoted to a variable called Direction now for us to actually check which one of the blend spaces we should use we need a variable of the type Boolean in the Target lock on component that I forgot about this one will be called lock on and let's set it to True before we start the timer by event and also set it to false at the stop Target lock on now when we go back to the animation blueprint and get Target lock on component we can actually get lock on variable and check if this is true for that let's just create another variable right here called the same and hook it right here now we can go to The Locomotion and drag from the idle movement at State this state will be called lockon movement and it will go to that state only when lockon is true and also it will go back only if loon is not now in the lock on Movement we need to set the blend space type the BS Player lock on and get player blend space the movement direction will of course be the direction speed will be speed now when we hit play you can see that the player is still not moving correctly and that's because his Orient rotation movement is set up differently oh there is an error right here so let's actually check it I think it's too fast so let's just add one delay here duration will be literally zero so yes it's just too fast go it's just fastly updating so that's why it doesn't work so let's just add one delay here and you should be okay now the movement to change that we need to go to the Target lock on component and change the movement system itself so get the player reference get character movement and drag from this set Orient rotation to movement this one will be false and also we need to use controller desired rotation and also when we stop the target lock on we need to do this but change it the use controller desired rotation will be false and Orient rotation to movement will be true and with this you can actually move just like in a Souls game oh and we can still move camera which we shouldn't be able to so I do apologize for that I forgot to set ignore the look input so for that let's get player controller and set ignore look input and this will be true also set it before the timer event itself and copy the player controller right here at the stop Target lock on and reset ignore look input so we actually will be able to use Mouse when we stop so now we cannot even use Mouse and when we go away it actually turned off the lockon and I also forget to stop the lock on itself I do apologize again for that so that's pretty easy so let's just get the target lock on component and get lock on check if that is true if it isn't we want to trigger the Target lockon and if it is we want to stop the target lock on now we can actually stop the target lock on anytime we want so thanks for watching I hope this tutorial helped you somehow and and have a wonderful day
Info
Channel: Darklore Creations
Views: 3,796
Rating: undefined out of 5
Keywords:
Id: fNAqYUAT18M
Channel Id: undefined
Length: 26min 14sec (1574 seconds)
Published: Thu Feb 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.