How To Throw A Rock At An Enemy To Stun Them | Pick Up Rocks - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the religion 4 tutorial in today's video i'm going to show you how to throw a rock at an enemy or any object you want so you can throw anything at an enemy ai and it will stun that enemy so they will fall on the floor be stunned they won't be able to move and then after a set amount of time they'll carry on moving again so if you're in an enemy chase you can throw this at them they'll stop chasing you and then carry on if they can still see you afterwards so i'll show you what's gonna look like so we get in we see the enemy is moving around there if i hit g i'm gonna throw a rock so i just missed them so this might take a minute to actually hit them but you can see that when i do so again it can be any button i hit them they fall to the floor like that the rocks disappeared after five seconds they can get back up like so and continue about their day what they were doing which is for me just randomly roaming around and you can set this up so the player has a limited amount of rocks so obviously they can pick some more up if they want but i've just set this to be five by default so that's going to happen and then when we run out of rocks we're not gonna be able to throw any more so i'm pressing g i'm not doing anything so again obviously you can then pick up more rocks if you want so actually i might throw that in there as well just to show you how to do that at the very end but i'll show you how we're gonna do this now so our first step is we're gonna want to import our animations and create these into montages so i've already got these here so you can just drag and drop them in i've obviously got these from miximo i've retargeted them to my mannequin here so i'll leave links to these in the description down below so once you've imported those we're just going to select both of them like this right click create create and in montage like so as we're going to be using animation montages to do these once you've got that what we're going to do is we want to open up the stunned and in montage like so so open up the stunned there and we're going to search for auto we're going to disable enable auto blend out so just untick that like so and hit save and the reason we're doing that is because obviously you can see here at the very end of the animation he falls over like that if we leave this ticked what's going to happen it's going to reach the end and then it will just restart so well i won't restart sorry we'll just get back up so he's standing like this which obviously we don't want we want to stay laid down there like that for however long we want so we untick that that will happen it won't automatically leave it so if we save and close that that's all good now the next step is we want to create our rock blueprint so this is the object we're going to be throwing at the enemy so for me i'm going to go to content i'm going to right click i'm going to get a blueprint class i'm going to get an actor i'm just going to call this rock bp like so you can name this whatever you like and just open that up straight away like so in here what i'm going to do i'm going to get a static mesh like so i'm just going to call this rock sm static mesh and i'm just going to be using the starter content rock that we get here so sm rock like that this is quite big so i'm going to untick the snapping there and just scale this down like so to get to the size i want i think that'll be good if i just put this in you can see maybe a little bit bigger like that i think that would be good then i'll just resize that again like so and then also disable snapping or moving it i'll just move it so it's in the middle like that then what i'm going to do is i'm going to add another component and get a sphere collision so it's fixed version like that leave it the name as that and then you can just scale this down to the size that you want so i reckon that'll be good i'll move the rock to be in the middle like that once you've done that i'm going to hit compile and save that i'll re-enable these and we're going to drag the rock static mesh onto the sphere so that now the rock is parented to the sphere so wherever the sphere goes the rock goes as well we're doing that because our next step is we want to select the sphere and we want to set simulate physics to be true this means that it will fall it will fly we can throw it anything like that it's going to work perfectly if we just simulate physics like so and obviously you can change its mass so how heavy it is it's only a damping and it's angular damping which is obviously how much it gets slowed down on the linear side so if you up this to let's say 10 when you throw it it will have more linear damping when you throw it like so and the same with angular i'll put that to 0.5 compile and save that you can mess about those values those are just two that i just randomly picked out my head so you can either leave those as default which is what i had at the start of the video or you can just set those people if you like and mess about with it too but compile and save that and that's pretty much all we need to do in this rock blueprint here so we can just close that like so then we want to open up our enemy ai so i've already made one here but if you don't have one you can just duplicate the character blueprint open it up and then just delete what you don't need so in here all i have is the random roam code that i have here i do have a video on that if you want to see it so that'll be linked on top of the screen and probably in the description down below as well so once you've got this set up to how you want we're going to scroll down here find some space right click and get event hit and what this is going to do is it's going to fire this off whenever this hits or collides into another blocking volume so another actor which has the collision as block it will set off as event hit here other actor or just other there it's going to be cast to a rock bp or literally just anything that you want to throw at them so if you want to throw a book a rock anything like that you can just throw anything just make sure you cast to it after this other here and you can have multiple things here as well and it will still count these are what we want to throw at the enemy to stun them as rock bp i want to do is destroy actor now this is just so that when it hits the player or the enemy sorry it's going to get rid of the rock if you don't want to do that don't do this step here but that's just what i'm going to do so that it's much more efficient for the system so once it's been used it's hit the enemy we don't need it anymore we just destroy it so actually i'm going to do is i'm going to compile that and open up the rock blueprint again just to add something extra on here because i mentioned that this is destroying it to help with performance we also want to do that if the rock misses so at the moment if the rock misses it's just going to stay there forever if we throw a lot of rocks that's not going to be very efficient at all so we can come off of event begin play and get a delay to destroy it or actually what might do is instead let you pick it back up again so actually for the moment you know what we'll leave it like this and then we'll come back to that later to pick it up so we'll go back to our ai's done forget anything there and we're going to come back to that later out of this destroy actor what we're going to do is going to disable input so actually no i won't i'll get the character movement and we're going to disable movement like that in there like so out of this what i'm going to do play it on a montage and the reason i'm doing that there this is what's actually stunning the player it's the enemy sorry so this is going to actually prevent them from moving obviously disable movement that's the stun part after this what we're going to do is we're going to play and in montage this montage here is going to be our falling over and knock down animation montage so let me just remember what i named that stunned that was it yep so and the montage here will be stunned and in montage like so out of this what i'm going to do is i'm going to hold down d left click to get a delay and this delay is how long you want them to be stunned for so i'm going to set this to 5 seconds but you can set this to absolutely well if you like this is just the duration that they will be stunned for and then after this what i'm going to do is play and montage again this one is going to be the getting up so getting up montage like that hold on d left click to get another delay put the return value in there like so so basically once this animation has finished playing out of this i'm gonna get the character movement again and then just set movement mode like that plug that in there i'm gonna set it back to walking this is just enabling the enemy to be able to move again and that is that code done so we can compile and save that so what's going to do is when the rock hits the enemy it's going to destroy the rock stop the enemy from moving play the stunned or get knocked over animation five seconds later or however long you set it to it's going to play the getting up animation so it will get up once that's finished it's going to re-enable the movement so they can then walk and move about again and that is the base code of it done setting up this stun so now what we want to do is actually set up so the player can throw this rock to do that i'm just going to minimize all this and go to my character blueprint so for me that's content third person bp blueprints that person character but for you this could be third first what if you've named it and then in here i'm going to do i'm going to scroll down and find some space and then this is where we're going to get the action mapping to actually throw it so let's make that so go to edit project settings once this loads we'll scroll down to input down here and we'll create an action mapping here so i'm going to hit the plus action mappings and call this one throw object or throw rock or just throw anything like that i'm going to change it from non to bg again change this to whatever you like but i think g is good quite common for it to be g to throw not sure why probably g for grenade or something like that i'm not sure but you can set this to whatever you like the benefit of action mappings is you can set up multiple keys you set up keys for different consoles and you can also set up key bindings but we're not going to go into that today we're just going to set up the action mapping and set it to a key we want once you've done that you can close that right click back in the event graph here and then we're going to call that action mapping so i named mine throw object so you see input action door object there at the pressed of this we want to see how many rocks we have i'm going to hold down b left click to get a branch plug that to the press there and then i'm going to hit plus variable here to get a new variable i'm going to call this rock amount or rock ammo or rocks anything like that and change it from a boolean to be an integer integer being a numerical value once you've got that you're going to drag and drop it in here to get rock amount come out of this and get an exclamation point equals which is a not equal to so if this rock amount is not equal to zero so we're going to leave that at 0 plug that in there so if it's not equal to 0 we're going to throw something if it is equal to 0 we don't have any so we're not going to throw it so obviously if it's not equal to 0 then we have some so we're going to come out to true so out of true what we're going to do is we're going to get a decrement or decrement int so decrement integer like that with the get rock amount again into the first there and then we're going to set the rock amount after that like so and what a decrement integer does is it essentially just takes one away from an integer to then set a new one so it's gonna get the rock amount minus one integer and set it to be the new rock amount now you can just do this in a normal integer minus an integer but this way it's just a little bit more clean as we're only gonna need to take one away then after this so once we've taken a rock away from the player we want to actually spawn this in so we're going to come out of this set rock mount and we're going to spawn actor from class so small actor from class like that with the class being our rock bp so rock bp like that the spawn transform we're gonna do is right click that split the structure pin and then for the location we want to create a little reference in our character blueprint so if we go to the viewport here add component add a sphere like so and i'll just call this object ref like so and then over on the right here we're going to scroll down i'm going to make sure that generate overlap events is unticked can character step up on no collision presets no collision this will mean that there is absolutely no collision on the spherical so the player won't collide with it other objects around the player won't collide with it it won't trigger off box collisions to open doors anything like that and scroll down again hidden in game true that means we won't see it in game so as far as the player is aware this circle or this sphere sorry here does not exist it's just simply for other developers to use as a reference now if we make that a little bit smaller like so in fact i'll do that to make it even smaller like that and now we can just move this to wherever we want so don't worry about these box cushions on the arms here that's from a previous tutorial but basically this sphere here is where we're going to spawn in the rock so you can put this absolutely wherever you want i think there is going to be good for me i might move it forward just a little bit but put it wherever you like once you've done that and you've got it to where you want let's go back into the event graph like so then what we're going to do is we're going to drag and drop a reference to that sphere here so mine's object reference drag out of that and we're going to get world location the return value of that will just be the spawn transform location for our rock like so so simply going to see where that object reference is and then spawn the rock there that again is why it's important that we have no collision on the sphere because otherwise the rock would probably just explode out of it as it's colliding with that it needs to go somewhere so it's going to get a massive boost and just fly off but it won't do that as the collision is better so what we're also going to do is we're going to drag and drop a reference to our capsule component so capture component there drag out of this and we're going to get world rotation return value of that is going to be spawn transform rotation so what this is going to do is it's going to spawn it where that object's reference is and also it's going to spawn it in front of the player so this rotation make sure it is always in front of the player like so the scale you can put as well if you want but i'm going to leave it as one now also with this reference here it should realistically do that put it in front of the player sorry as it's the location but sometimes it won't do it perfectly putting the rotation on there make sure it does do that however we want and it also makes sure that it is going to be upright perfectly so whichever way the player is rotated so if the player is going down a hill that will also be rotated going down a hill is going up it will be up if the player is straight that would be straight so it just makes it look a lot more realistic as if someone actually did just throw a rock so we're going to come out after the spawn actor here get a return value of this and we're going to get sphere so get sphere like so so it'll be all the way down the bottom there and that's our sphere collision so you named it something else just get the sphere collision out of that we're going to set physics linear velocity plug into the small actor there so what this is going to do is it's going to spawn the rock and it's going to add velocity to it in a linear direction or so obviously in a straight line and that is how we're going to throw it so this is spawning it this is throwing it new velocity what we're going to do is we're going to get the direction of the players facing and throw it there so we're going to come out the return value of this spawn actor again and we're going to get actor location go to location there move that down a bit right click just above it and get actor location again this time of just reference of self so it's the player so we're getting the players location and the rocks location out of the return value of the players get out of location so the top one we're going to get unit direction vector the player is in from the rock is in two so what this is going to do is it's going to get the direction from the player's location to the rock so imagine if you're drawing a line from the player to the rock it will get the direction that line is going in the return value of that what we're going to do is get a vector multiplied by a float and i'm gonna set this to be about 1000. you can set that to whatever you like that's just basically how far or how fast the rock is going to fly out at so how fast it goes also obviously determines how far it goes so just use that as a reference there a thousand is what i use at the start of the video the return value of this will go into the new velocity of the set physics linear velocity like so and that's what we need to do to be able to throw it so what it's going to do is when we press r button if we have enough rocks it's going to take one rock away from us spawn at the rock in front of us and it's going to add a bit of velocity to it in the direction in front of us so we are throwing it so if we compile and save that what i'm also going to do is i'm going to select it all and then press c to comment it and i'll just call this throw rock like so hit compile and save like that so now we're essentially done so if we minimize this we can hit play to test it i'll just delete that one and also get the ai in here as well so ai stun there and if i hit play you see that actually we won't be able to throw anything if i hit g we can't because i've just remembered i didn't actually set a variable let me delete another one of those go back to the character blueprint select the rock amount integer there we left that at zero so obviously if because it is equal to zero it's not doing anything so if i set this to let's say seven that means i have seven rocks so compile and save that hit play to set this again if we hit g we're gonna throw a rock but you see that's doing something quite weird so it's going out and then it's just going straight down and through the floor so let's have a look at why it's doing that so you can see here it's something to do with the collision so you see invalid simulate options the sphere is simulating the physics but it doesn't have the correct collision so i just forgot to change the collision settings in there so if we open up our rock blueprint again go to the viewport here select our sphere and if we scroll down until we get collision i forgot we need to change it from overlap or dynamic to be block all so go block all compile and save this should now work a lot better so if we hit play hit g so you see now it's throwing it and it's not falling through the floor anymore they're just going straight down like that however the velocity is still a little bit messed up so if we go back into our character blueprint as well so like so third person character here ah no i know why it's doing that because i changed the linear drag so if we go back to our rock blueprint and select the static mesh or the sphere sorry i changed the linear damping and angular damping i'm not going to mess about those so i'm just going to reset this to default and that will actually be why it's doing that so we can partner safe they should not look a lot better that's just because i was messing about with it earlier but i shouldn't have done that as i didn't do that was testing it so we hit play g you can see we can now throw it a lot better and a lot further so that's great what i'm going to do just mess about with a bit more so you see this is what the testing part we're just getting to see how we want it what i'm going to do is i'm going to move that out and a bit more up and i might also increase the velocity here so i'll put that up to 1200 like so or 1300 compile save it should look a bit different hit g you see that's going a bit higher now so you see this is all customizable for you you just change these values to get it perfect for you so i might set that back so a thousand as i moved it up as well so again just mess about this together for how you want and there you go you can see we can now throw rocks so now if we get one to hit this ai so this might be a bit difficult you can see what happened is it disappeared and they're not moving and now they are now why didn't they not play the animation we forgot to set up as well missing loads of steps here so one other thing we need to do is we need to make it so they can actually play animation montages so for me that's the mannequin animations third person nmbp but just open up your enemy ai's animation blueprint in here we're going to go to the anime graph like so out of the slot machine the state machine sorry what we're going to do is we're going to get slot default slot and plug that to the result there what this does is it just allows the use of animation montages so if compile save this should now be done hit g we can still throw rocks if we go over to our enemy ai for a rock hopefully hit it let me get a bit further back you see that we hit them they've fallen down they're going to stay down for five seconds then they're to get back up and carry on with their roaming around like so so this works perfectly we have it so that we can throw a rock at the enemy it's going to stun them they'll fall down for a set amount of time and then they'll get back up and continue on with what they were doing and you can see we can throw the rocks at them and they'll still hit them perfectly like so so one extra thing i might add in here is what i might do as well is make it so you can pick up rocks so to do that it's very simple we're just going to open up our rock bp here and then we'll do is go to the event graph here select the sphere collision there add event add-on component begin overlap right click in again add event add-on component end overlap other actor for both of these will be your character so for me that's cast to third person character but for you this could be first third or whatever you've named it make sure you cast those in the other actor and that just means if it is this player or if it is this actor overlapping them it will fire off if it does what i'm going to do is add third person character of the begin overlap we're going to get rock amount like so hit plus so integer plus an integer although actually i'll do an increment increment like that which again it's just going to add one so do that and start passing character again we're going to set rock amount like that and that'll be that integer there so it's going to add one to our value and what we're going to do is actually move all this down move this part out a bit and hold down g left click to get a gate this cast we're going to open the end overlap cast we're going to close exit we'll go into setting that rock amount like so now how we want to enter this gate if we want to do this on interact so again what we're going to do is go to edit project settings we're going to go down to input and create a new action mapping this one i'm going to call interact or pick up anything like that and i'm going to set this to the e key as that's what it most commonly is either e or f but you can set this to be whatever you like close that right click in here get interact interact like so put the press of that into the gate there like so so basically what this means is when we are overlapping this collision we can enter the gate by pressing e but if we stop overlapping that severe collision we can't go in and pick up the rock anymore so now of exit of this what we also want to do is just destroy actor which means it will disappear so basically we've picked it up and what we also need to do is off of event begin play here we want to just simply enable input like so target itself play controller as get player controller and this just allows us to actually use the e key or the interact key that we just set up so hit compile and save we should be able to pick these up so if we just drag in a few rocks here what we can do is if we hit play walk over to them if we hit e you see that we're going to pick them up so now the reason that's a bit tricky is because the sphere collision is quite small so i'm just going to unparent the rock there increase the size of this a little bit not too much because this is also the collision of when it hits the enemy put that back on there this should work a little better now so if we go up to it you see if we try to pick it up we hit e nothing's going to happen we're just going to kick it the reason for that is our sphere collision is set to block all so if we select this here it's going to block all meaning it will also block the player so what we want to do is we want to add another component this one again being another sphere collision like so we just leave that there see how big this is so i might increase the size of this a little bit hit compile and save and that should not work so it's overlapping all dynamic this one won't affect how it hits the ai or the enemy because this will be overlap not hit so the other one will be an event hit this will be an event overlap so now compile save again you can get that to be as big as you want so actually i might increase it a little bit more this is basically where the player is going to stand to pick it up again make sure that you also parent it to the top sphere i might also rename this so it's easier to see so interaction or pickup collision anything like that go back to the event graph and we're going to change these events here so right click now on the interaction collision add event and component begin overlap right click and open end overlap move these back up and then we're just going to reconnect these like so so now other actor goes into there again other actor like so connect the executions up so now this should work a lot better if we hit compile save and then one final thing we need to do is actually also select this interact here and untick consume input which means we can then use this on multiple instances of this rock so if we place down more than one we can still use it now if we minimize this hit play to test it we can see that if we well he's just walked into it so we've got one less now but usually that still works he's been knocked over but if i press g and throw away all of these rocks so i'm still pressing g i'm not throwing any if we walk over here to this rock and i hit e you see we picked it up so now i have another rock which i can throw and i can't throw any more and then this should still work with these ones that we already have so if i go over here hit e i can pick all of these rocks up as well that we've already thrown and i can then re-throw them again like so so this now works perfectly i can pick up the rocks i've already thrown and i'm still throwing them at the enemy to still stun him so if i can find out where he is we should still be to test this again he's over there or he just got hit by one anyway so that works so now he should be getting up afterwards and continue on again so i think that'll be if this video is we've done everything we wanted to do we've made it so we can throw a rock or an object at the enemy ai and he will be stunned fall over and then won't be able to move for a set amount of time and get back up afterwards and continue moving and then once we've thrown these rocks we can also pick them back up again anyway or we can just pick them up and throw them like so on any button press that we want and if they hit the player or the enemy it will fall over like so and won't be able to move and you can customize this completely however you want like i showed you before you can set up the velocity that they get thrown at how long he stays down for all the animations all that good stuff but 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] bye
Info
Channel: Matt Aspland
Views: 26,215
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, games design, graphic designer, ue5, unreal engine 5, ai, enemy, stun, stunned, stop, moving, chase, chasing, throw, throwing, chuck, pick up, up, pick, at, rock, rocks, distract, distraction, how to throw, ue4 throw, ue4 stun, stun ai, stun enemy, ue4 throw rock to stun enemy, ammo, daze, flash, prevent, from, running, walking, knock out, fall over, get up, shock
Id: -Lg-_EU6NoU
Channel Id: undefined
Length: 24min 39sec (1479 seconds)
Published: Wed Dec 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.