UE4 - Tutorial - Grabbing Objects!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and gals we are back with another tutorial so yesterday in a dev vlog I showed you guys this this nice system for picking up objects I'm gonna show you how to put this together today so we've got one here that's like free-roaming you can do it at once we've got this other one here that's locked in for specific axis so it doesn't rotate at random I quite like this one but I'm going to show you how to do both it's just a couple of tick boxes to change those between each other so let's do this without any hesitation let's hop into a new project I'm using a first person template here so I'm going to be assuming that you guys are using first person as you hop into the game because it's just loud you can see that it's just the basic first person thing that we've got here so what we need to do is first we're going to check all these boxes these are all already simulate physics turned on none of them are using any any mass right now so their weight doesn't really matter but I'm going to show you guys how we can check for weight and how we can only pick up objects above a certain weight and everything so let's get comfortable what we're gonna do is we're going to go into the first person character blueprint here wheel and we're gonna start saying some stuff up so find yourself an empty space excuse me and now what we're going to do is we're actually going to create a variable and this is going to be a boolean which is basically an on or off situation so it's going to check yes or no true or false I'm gonna say is holding object I'm going to compile that and now we get a default value which is going to be untouched which means false and that's what we want we want to we want that to be false if we drag out from the is holding an object here on the Left we can get this and I've got this cool little thing here so let's just reference this in the blueprint so we're going to do is we're going to right-click and then we going to search for right mouse button and this is what's going to be used for our input from the pressed we're going to branch this and we're going to plug in the is holding object and then we're on the released we're going to put this in as well and ploidy is holding object in here now what we're going to do is we're going to make a couple of custom events so that we can keep this under control so right-click custom event I'm going to call this pickup I'll right-click again custom event and we're going to call this one drop now we can do is from these from these branches we can call these custom events so that we can make sure that these are only firing the correct event at the correct time so in fact we don't need the branch for this right now we don't we don't need to match so we're gonna say right mouse button I was getting ahead of myself the branches for a fix that we're gonna do another wall I'm right mouse button what we're gonna do is we're going to fire so we're going to say a pickup and I'm released we're going to say drop so again next what we're going to do is we're going to start the pickup event line so we'll drag from the pickup execute pin we're going to choose we're going to line trace for objects now you've got quite a lot of information here that we need to get through but it's quite simple so don't worry about it what I'm going to show you don't be scared of all these different pins you see here we've got object types and it's asking for an array if we drag off of this backwards and you can make an array here make an array and this would give us all the different kinds of things that we could possibly put into this object types we only want physics bodies so we're only gonna be checking for physic physics bodies because we only want to check if an object actually has physics so if the object has physics we're going to say okay it's got physics it's hit this object every other object should get ignored by the line trace and that's exactly what we want so how do we determine our start and end positions well we're going to be using the first person camera now on the left hand side here under the components you can see we actually have a first person camera that's already attached to this character I'm going to drag this out and that would get it for us and now we're gonna drag from this and we are going to say get world location and we're going to drag out again and we're going to get forward vector everywhere now if for whatever reason your forward vector comes out a little bit dodgy which I've noticed sometimes I get a different node I mean let's see if we can break this here now if we get forward nope sometimes I get this weird forward vector you get a different type so yeah we'll get this one if you get this one while dragging out from this pin it's not going to work so just drag in another first person camera and drag off of there and you should be able to do it's a weird book and I don't know what quite causes it but that's a quite an easy one to fix so what we're gonna do is from the forward vector we're going to drag off of this and I'm gonna say multiply by a float so we get vector times float and what this is going to do is it's going to multiply this and give us more distance than before okay now what we're gonna do is the get world location this is just going to go into the start position because we want add line trace to begin where the camera is then we are going to take the world location and we're going to plus so we get a vector plus vector what we're going to do is we're going to take that location we're going to add to it the length that we've multiplied for the forward vector which is going to give us a distance for the line and we're going to use that for the end and now that's going to shoot the line for us now if we change this draw debug type to persistent and we compile if we head into the level if we right click we now fire this nice red line so you see that there how do you see here that we can see that it's hitting this object and it's ignoring this one so you'll notice that it doesn't have a red box on it it's just going through the red box means it's been a successful hit so it is only checking for these for the physics objects so it's doing what we want so we'll go back into here now and we're just going to tidy this up a little bit because I've made a bit of a mess from the line trace what we're going to do is were actually going to branch and then we're going to put the return value of the line trace into the condition so we're going to make sure that whatever happens after this point is only happening if the hit object is a physics body so what we're going to do is we're going to grab component although we can't do that yet because we need to add something else to our player character so over here in the components on the left if we click Add component we can add a physics handle a physics handle is going to be like a little virtual hand that allows us to pick up an object no it doesn't have a widget here so we're gonna have to manually do our thing at positioning we can do that by adding another component and we're just going to get seen so we have the scene component here and that actually has a widget I'm gonna call this grab location so that we can actually you know know what we're getting inside of our blueprint and you notice I've just moved this forward in up a little bit because I want this to be in a position where that it's not going to obstruct the players for you Chris compile and go to the event graph and now we'll drag out the physics handle we'll drag from this and we're going to say grab component at location or put the true into this and now from the out hit of the line tracer objects we're actually going to break the hit result and this will give us this really long list of stuff what we want here is we want the component because it's looking for a component not an actor and we're going to plug this into the component and the grab location is just going to be here where we have this location we're going to plug that in I know well that's going to do is it's going to allow us to pick up the the objects as long as we're holding it although right now it's not going to work as you can see it doesn't work problems oops so let's head back in now it's firing everything off currently it's doing what we want it to do it's not really grabbing anything yet because we need to do a little bit more so we can say what we're gonna say we're going to say tick so we run event tick and let's move this somewhere kalenna put it here for now so an event tick we want to set target location of the physics handle now up here where we have our grab location we're just going to drag this out get the world location I'm going to plug that into the new location here and it's going to put our physics handle where we told it to be so you can see we can pick up the subject that's still a little bit close for me and now you know I've let go of fright nervous but we're still holding it because we haven't done the drop event yet so let's go back into here and from the drop what we're going to do from drop is we're just going to release component from physics handle so now if we were to let go it should do the drop so let's walk up to this will hold right now so we pick it up and if we let go you dropped it now we have no weight limit for this we bring we and we can do things like that we can actually just launch these away which is I mean if you might want that but for me and what I was doing with my project this was totally undesirable so let's head back in here our show you're just gonna fix this up a little bit so the event tick is fine so it's just comment us out sets handle location for a comment just press C with anything highlighted and you'll get a comment box so we're done with the tick I'll place this over here now the mouse button we actually don't want this to just happen I'm pressed and released I want this to happen I'm pressed so I'm gonna break the link to drop and from the pressed I'm going to branch and this is where we gonna plug in the is holding object so if you press it and it and is holding an object is true we actually want to drop and if the holding object is false then we want it to pick up so there we are we have dropped and pick up so true drop false pick up and what we need to do now is make sure that these are actually getting set so it's holding object we're going to track this out and we're going to press set so after it grabs the thing is hotting objects going to become true and then set again and set it to false when we let go so if we compile now and we go in if we right-click it should pick up I know I've only collected my holding and if I click again we let go now if you don't want that to be if you want it to be a hold button then relieve it as it was but this is how you do it for picking up and dropping by pressing the button twice input for pickups okay so you have that doing this thing now it's just a couple more things that we're gonna do to tidy this up a little because it's it's still not quite doing what we wanted to do so what we going to do this because I don't want to be able to fling those objects you know drag DS along a little and now from the hit component we're going to right-click this and we're going to promote this to a variable we're going to call this I'm gonna call this variable physics object and we'll plug this branch into the physics object and then the set physics object into the grub component at location now what this is doing is it's making sure that every time we get a new object it's setting this variable to be that object so only the subjects going to be affected and the reason I'm doing that is because we're going to change the way that this object works so that we cannot fling it around white so dramatically so what are we going to do here we are going to drag from the is holding object we're going to search vision object no not quite we're going to drag off the physics object we're going to get this we're going to set collision object up no not the set collision response to channel this one we want we're going to do for this now is the channel we're going to set this to porn and the new response is going to be ignore so what this is going to do is when we pick up an object our character can now walk inside of it and now the reason I'm doing this is so that we cannot ever get blocked off by the object that we're holding you'll notice if I let go while we're inside of it blink we can get some really undesirable effects so like we can make this bounce away from us like like crazy whoops I keep forgetting that I got the sound on I'm sorry guys we're gonna head back into this again and we're gonna do the same sort of thing so grab this paste it here and here when we let go we're going to set this back to block okay because right now it was continuously not letting us do anything with it so it's going to now be inside of us and blink we can't bounce this away boink wink wink okay so few more things to do what we're going to do is we are going to that thing what are we going to do in fact if we play this again let's see what we've got so far so you've got this spinning around I wouldn't be a little bit further away yeah as always I forget to move it far enough so I'm going to grab the grab location that we created a bit further away there feather up compile that and see how that looks oh yeah so we've got a good grab location but we can still fling it well and the reason that we can still fling it is because we're actually we're giving it force without wanting to give it force while it is picked up so here we have this physics object on the drop event we're gonna drag from this I'm going to set velocity so set all physics linear velocity I'm going to plug this in and we're just going to leave that at zero zero zero and what that's going to do is it's going to make sure that when we let go with the Box all of its velocity set to zero so it just drops like a normal box so we can't throw it any more but we can't get this weird spin the reason we can get this weird spin is because it's still able to to rotate so one thing that we can do to make sure that we can't quite do this is we can either go into the blueprint and from the physics object set angular dampening and what we're going to do here is we're going to set this angular dampening to about 10 and what that's going to do is it-it's going to add a drag on any kind of angular motion and then we can say over here again but we'll add a slight delay of maybe point to yeah we'll leave it at point two and then we'll set it back down to its default of 0 we'll make sure to plug this object in here so recap all this and we head back in now we shouldn't be able to make them spin and we can't good which is nice so it can no longer spin because we've added the dampening which gets turned off with a slight delay but if you don't want any of this spin at all if you select any of these boxes under the physics here in the right hand side under the details panel we have constraints if you open constraints up you actually have these locked positions and rotations if you lock all of the rotation axes and you go in if we head over to this thing and now cannot rotate you'll rotate around the object but this object cannot rotate so there we are now so another thing I just noticed that we've forgotten is that we have not parented the the seam that we added so that means that we cannot look up and down it's always going to stay in that exact position so we head into the first person camera we get to viewport what we're doing we need to get two people here we have our grab location we're just going to drag this up to the first person camera and drop it there which will add it as a child to the camera so now it will follow any of the cameras movement so I'm just gonna press compile to make sure that works I'm gonna walk up we got press right and there we go now can we can lift it up into the aerosol yay boink so there we have it we've got this nice thing shows one other thing I was going to do very very quickly I'm sure it was in the blueprint there's one more thing that I wanted to do with it all was there we've got the angular dampening we've got the the inputs we've got the locations I've got all of the things change all the stuff we're doing the velocities I believe that is your lot guys yep so if you want the grab location to be a little bit further like how far away you can grab from this if we increase this to a thousand you can use a variable for this as well if you so wished but it will shoot the the location far oh I know what I was going to do I know what it was so you see now he can pick this up quite a far distance which isn't too realistic to fight 500 actually quite a nice number so did that so I was going to do is I was going to show you guys how weight works so what we're going to do is we're going to just drag all this over after the branch from the line trace we're going to add another branch here and then from the hit component we are going to get mass so type in get mass and you'll get this lovely get massing from the return value less than and we'll get float less than float and here you'll put in the amount of weight so let's say we have 500 weight we plug that into the condition so this is 500 kilograms I mean this guy is going to be strong so it's now only going to be able to pick it up if the weight of the object is less than 500 so if we were to select this box and we turn on the mat see we've got mass here in kilograms and say if we set this to 499 and we press play he'll be able to pick this up yay if we set this to 501 which is one way to move it you shouldn't and he cannot you see it still hitting get buddy it's not picking up the object this one I think is quite heavy yep so this one's still quite light so he can still pick that one so that's how you you'll add weight in it's quite simple just another branch get mass and check the dimmer of weight that you want with any physics object if you pick the mass in kilograms in under the physics you can overwrite be the weight you can also customize the dampening of everything so in our blueprint we changed angular dampening when we picked it up but say we wanted to stab a linear dampening of ten anyway this is gonna give it I didn't change the weight silly me that will give it a lot of drag so that it will feel like it's a heavier objects you see how it's dragging behind a little bit now see this okay you can see it's dragging a little and there we have it so look at that drag that's that's your lot for this one goes that's how we create this pickup system in first person in unreal 4 I hope you guys find that useful and that's something that you can use in your projects before sewing off with this one somebody asked me to set up a patreon I've done that I'm gonna start leaving links so that I don't expect anybody to follow it but if you do I then thank you very very much it's very very helpful I also set up a discord server for anybody that does sign up for the patreon so you guys can chat to me whenever you want as long as I'm online of course what else I've set up a Facebook page for a Niffleheim which is the game that I'm doing the dev logs for I gonna start leaving that in the descriptions on niflheimr not going to leave those in the regular videos though so there you have it that is it that's this video don't understood I gotta go make some textures for this coins that I promised you guys I'll see you in a little while
Info
Channel: Dean Ashford
Views: 51,731
Rating: undefined out of 5
Keywords: UE4, Unreal, Engine, Unreal Engine, Tutorial, Student, Indie, Dev, Skyrim, Object, Grab, Pick up, Blue Print, Physics
Id: HnR1Gf5gXcY
Channel Id: undefined
Length: 22min 53sec (1373 seconds)
Published: Fri Apr 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.