UE4 How to Grab or Pick Up Object with Gravity Physics Gun in Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi I'm Andre I'm gonna show you how to grab an object using the physics constraint so let's see how it works so if I like Li left-click I can grab the object it holds it's near the camera and if I left click again it lets you go also if I right click I can throw the object or I can poke other objects without grabbing them there's also the functionality that when I scroll it increases the distance where I hold the object so usually this is done in most tutorials using the physics handle but I've chosen to use the physics constraint because it has two advantages one is that it's easier to configure and the other one is that in the event take we don't have to update anything every frame like we do for the physics handle we just set it up and it works so let's see how you can do this so well get rid of this I have a player character here it's basically empty except the movement code so we're going to use this so let's just make this posess here so you can play with it okay so let's go ahead and here so we're going to use a physics constraint now because the physics constraint needs two objects to work we're gonna create the first one for it so I'm just going to use a static mesh that we're gonna call the object slot so this is gonna be used as an anchor when the physics constraint grabs the object that we're looking at and to this we're going to parent a physics constraint like this to call this grab constraint like that now let's go ahead and quickly configure this so we'll free everything linear and angular limits and will impose we'll use linear motor and angular motor to keep the object in place so let's put here 1000 and here 104 the angular we activate twist and swing and again 1000 and 100 so now we configure the fizzy's constraint let's go ahead and configure the how the objects slot which is we're going to just use a sphere for this it doesn't matter because we won't actually see it in game but what matters is so we're gonna hide it in game here check hidden in game and for the collision go to custom make sure that collision enable query and physics activated and check ignore everything okay so we can see you know we've configured this now let's see how we can do this self first we'll do this and the input fire action fire so when we left click so let's see so first when we press left click we're gonna trace for objects then we're gonna if there is an object we're gonna grab it so this cell is creative functions for this trace objects and grab object and we'll also have drop object for now and we're also gonna need a variable that remembers which component we've grabbed so we'll have held object like that it's gonna be a primitive component like this so we'll see when we press fire we're gonna check if this is valid not like this like this if it's valid then that means we're holding an object so we'll gonna have to drop it so we'll execute drop and then if we're not holding an object then we need to trace four objects and if we find any objects we're gonna grab them okay so let's go ahead and trace make the trace so we'll use line trace for objects and we're gonna have to give it object types here so drag from this and say make and we'll select physics body but you can add others if you want and we need to set the start and end so where we want it to be from the camera from where we're looking at so we drag the camera and we say get location get world location actually so this is going to be out start point and the end point is going to be we're taking the forward vector of the camera so the direction in which we're looking and we're gonna multiply that my float which is gonna be the distance to which we're looking for an object so let's create here interaction distance like that there's gonna be a float then we drag this now let's put it right out to 1000 she's gonna be ten meet ten meters so we drag this into the end okay so we have our trace now for the parameters that go out of the function we're gonna have a hit result of type hit result like that sorry and we're gonna have a hit which is gonna tell us if it hits something or not of type boolean so we'll just connect them from the trace and this is our trace function okay so got the trace function here now we're gonna see if we hit something we're gonna branch from that so if we've actually hit something then we're gonna grab otherwise there's no point so let's go ahead and create a grab object function so when we're grabbing an object what we're gonna do is move the held object because we've put the grab constraint parented to this it's gonna move with this e so we're simply going to take the held object slot and place it where the trace hit hit the object okay so if we hit here we place it here and then we activate the constraint and it's gonna lock the objects together if we want it we could put also in the middle of the objects so we'll see that now let's go ahead and move the held objects slot so we're gonna set its world position locations actually like that and the location we'll know from the hit result that we're gonna add so we'll add the heat result input here of type hit result and we're gonna break that down so again I said you can put it either at the impact point where the trace hit at the surface or if we want in the middle of the object if we want to grab the objects from the middle we just take the heat component and say get world location location like this but we're gonna grab it from the back point like this so let's go ahead and check teleport here so when we move the object us and calculate physics or something so now we have to activate the constraints we take the constraints from here and set its constraint components like this now the first component is going to be the head objects lot like we've taught before and the second one is going to be the component of the object that we've hit so we take the heat component from here and we'll also drag the hip bone name because this is gonna be used when we are dealing with the skeletal meshes so it's actually grabbing the bone itself not the whole body okay so we said this now we have to disable the collision between the object that we grabbed and the player-character so you it's not mandatory to do this but it's just better so we'll take we have from here the heat component like this and we'll say set collision response to channel like this and for the porn because the character is a pawn we'll say ignore and then we just have to set the health object like this and so this is it so now if you try it out it should work see it doesn't let's see why let's take this a little bit of debugging so there's a problem here let's see did we set the interaction yes we did the hit result physics body so this should work actually yes as you can see we didn't connect the hit result but I think it stopped at the branch so we might have another problem yes so let's see here if I do this again so it doesn't pass the heat here there's a problem grab will get the location yes because we need a plus here so I've made an error so I actually have to add the direction to the location of the camera yes sorry about that okay so let's try again so now as you can see we can grab it no problem but we can't drop it so let's see how we can wrap it in the event graph here so let's go into the function so we'll just say take the held object first we're gonna take the grab constraint and we'll break it so if you break this then it should let the object go now we're going to have to reactivate this collision set response to channel and we'll see before the porn will say block and then we're gonna have to set the held objects to null so we do this here so let's try again now so I can grab it and then I can let it go okay this is fun now let's see how we can poke it so I have this here it's so we poke it with the right click so at the mouse button like this and then we're gonna have the same thing here we're gonna check if we're holding or not an object if we're holding then we're gonna throw the object if not we're gonna trace so if we're not holding then we're gonna trace like here like this okay let's create a function for poke like that so we'll take this poke and yes so let's go ahead and create define the function so first we have an input we have an input a hit result of type hit result and yeah this is sufficient we're gonna break this and to poke the object we're gonna use add impulse at location like this and this is not this the target will be the hit component let's also plug in the bone name and let's see what we do with the location and the impulse so the location will be actually the point where we hit it like this the impulse will be the the the orientation of the camera so the forward vector of the camera so the direction in which we're looking so let's take the camera from here take this so say forward vector and because we want to give it a certain amplitude then we're gonna multiply it by a float which this we're gonna head and variable here for book power like that normal multiplied here and let's give this so this will be a big number so let's see hundred thousand so we'll input this into the impulse so this is the book function they should already work like this we just have to connect the heat result to like this so let's test this out and we'll come back for when we're holding the object so if I click as you can see I can poke and the bigger the object the less it's gonna affect it so that's normal because it's an impulse okay so here let's drag this down a bit so here we're all there already holding an object so for the pork function to work we're gonna have to make a hit result for it so it's gonna be a fake hit result because we're not gonna trace for anything but we already know in here in the poke function we already know we use the impact point the hit component and the bone name so the bone name were not gonna use because we're just gonna affect the entire component but take the hit hold object and connect it to component here and then for the impact point we're going to use its location so we get world location like this so we're actually gonna wanna be holding it we're gonna poke it in the middle in its center like this so now we can poke it when we hold it but we have to let it go first otherwise you will just jerk a little bit and it will stay there because the physics constraint is still active so we'll drop object like this and then we can pocket so this happens is one frame so it shouldn't you shouldn't see any difference because you drop it now there is one problem with this here in the drop object we set held object to nothing so we'll just take this from here because if you look here it would it was gonna drop object then you said the held object nothing so when it reaches this it tries to get the health object and then you would have nothing here so there would be no data so we'll have to move the set to set the head object to null at the end like you see here otherwise we wouldn't have any results for this so it wouldn't work so let's try this out again so I'm grabbing this and I right-click grab and right click ok so everything works nice so let's see we said that we gonna implement when we scroll to increase and decrease the distance so we're gonna do that using the mouse down mouse wheel down and mouse wheel up like this so this is when we scroll up and this is when we scroll down so when we scroll up it's gonna we're gonna take the held object and move it further away like that so let's see we get its relative location and we're gonna add to it like that so let's add that's 50 and then we're gonna set its location again like this so we drag from here and we put this to the location and the same thing goes for the other one and but just with a minus here like that of course you can make this into a function so what we're basically doing if we're looking here we're taking this object and we when it scrolling up we're doing this on the scroll down we're going back okay so let's see so if I grab this and I scroll it's actually the other way around why is it that so if I do yes it should be the other way around so it's like this it's minus here so if I try again it works as it should okay so it made this work the last thing to make is the now now we're we're grabbing the object and it stays in place we want it to come back to the camera so to come to a certain distance the camera so for that we're gonna need an object which tells us the default location from the camera so we're going to use an arrow for that like this so we'll say held object default location like this so we're going to bring the held object here if the grabbing place is not true so for that we're gonna create and variable here that's called grab in place which is gonna be a boolean so let's think about it we've grabbed it and we want it to come towards the camera so we do this in the production fire here after we grab the object so we're going to take the health object slot and we're gonna move it move component to like this the rotation is gonna be the same so we'll talk we'll take get rotation from itself which is gonna be the relative rotation like this but the location is gonna be the location of the adult held object default location because this one and hold objects lot are within the same parent which is first person camera we can all take it take it relative location because they are compatible so get relative location like this and we're gonna use easing is out and let's say over 0.3 seconds so this should do the trick but we just need a branch here so we won't do it every time we're just doing it when grabbing in place is false actually so we drag from false okay so let's save and because by default it's false grab in place let's see here where is it I just search for it well that's weird maybe it's not yes because we have to make it editable for the instance and so we should find it here so if I don't check grab in place it will take the object to the camera and yes we have a problem because our arrow that we set here should be somewhere in front of the camera like this let's try again now so you can see it comes to camera I can drop it I can take it back again no I can't because there is a problem so currently with a the mistake I can't I can't take it back let's see why just a small bug here so let's see so if I try to grab it again it finds that yes because that because we poked the object and we and we forgot to remedy this so we've taken this out to set to null the hold object but we forgot to put it here also so now if we look again then it should work yes so as you can see I can affect the objects no problem so this is kind of it now just for a minute I could show you how we can configure the physics constrained to be different stuff so I've seen that some people want the object to remain with the same world rotation and because if you look here when we grab it now it actually Orient's to the camera and this might be good some for some but not for everybody so if we want it to stay like this relies the same rotation and only slide then what we have to do is it in the physics constraint leave the angular limits free but deactivate the angular motors like this uncheck them and now we go to well the object if we lock its rotation then as you can see it's locked so we have to do the same thing here so when we grab it like this we would want to set its rotation so heat component like this so settle okay so we're gonna have to set this custom plane so it's drag from here [Music] well just let's just set a plane for example well actually it's constraint mode custom plane type it's an enum I think this is done from the actor now so if you look here constraints lock rotation mode custom plane default now it should be here so yeah I can't remember this now but you should set the constraint mode here like you have it here so you just set it here and then redo it when you let the object go yeah so I yeah it's xscape me right now I can't I can't remember how we use the idiom but yeah so that's how you do it so there's also so let's reactivate this again as you can see in this mode right now because it's free to rotate we'll do something like this which is interesting yeah and you can do the same thing with the yeah so let's grab it from the middle like we said before so if we go here in the grab we'll just get rid of this you can do this very easily I have to look for it right now so instead of grabbing it from the impact point here we're just gonna grab it from its location so I say get your location like this and if we compile again I'll see that it's grabbed from the middle and it does actually stay at the end with the same rotation but not because it it's not but because we are not affecting it because if I if I touch the wall you see you start rotating yeah so this is it I hope this has been useful please like comment and don't forget to subscribe
Info
Channel: Lusiogenic
Views: 32,715
Rating: undefined out of 5
Keywords: tutorial, unreal engine, gamedev, ue4, grab, gravity, gun, manipulate, physics
Id: irk5NAuFPMc
Channel Id: undefined
Length: 32min 28sec (1948 seconds)
Published: Wed Jun 27 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.