‫Hello.
‫Welcome.
‫In this lecture, we're going to be making the grabber much more reliable.
‫And also we are going to be making it possible to drop a grabbed component so that we can throw it around
‫and generally have it be a little bit more useful.
‫Let's dive in and see how this is done.
‫Now, the reason we're often not able to pick up this gargoyle is that components that are simulating
‫physics for performance sake will go to sleep when they have been still for a certain amount of time.
‫And that's what we're seeing.
‫This gargoyles physics simulation is going to sleep and it's not being woken up when we attached to
‫it with our physics handle.
‫So what we need to learn to do is to wake it up when we hit it with a sphere trace.
‫Now let's go and have a look at the you primitive component documentation we've seen.
‫The you primitive component is the thing returned by our hit result, but what actually is a primitive
‫component.
‫So it says here in the documentation that a primitive component is a scene component that contains or
‫generates some sort of geometry and is rendered or used as collision data.
‫So generally speaking, primitive components are ones that can simulate physics.
‫So we can expect to find some physics simulation methods on here.
‫And what I'm going to look for is a function with the keyword wake in it.
‫We're going to look through a few functions until we find one that says Wake all rigid bodies.
‫Rigid bodies are essentially a physics term for objects that simulate physics.
‫So you can see it has a void.
‫Return type doesn't take any arguments.
‫So we should be able to just call this when we are trying to attach to it.
‫So if we go to our grab a CP inside our if statement, we've hit this component, we're about to grab
‫it.
‫The first thing we need to do is maybe let's make a variable to store this you primitive component.
‫So we'll have a you primitive component pointer called hit component set that equal to the hit result
‫dot get component.
‫And then using that hit component pointer, we can arrow wake all rigid bodies and then put the parentheses
‫and the semicolon at the end.
‫So this should just give that component a kick, wake it up so that it responds to the physics handle.
‫Now let's replace where we've done hit results.
‫Don't get components in our physics handle call to grab component location because we've now got a variable
‫for that and let's recompile when that's done.
‫Let's go ahead and hit play and let the gargoyle sit a little bit on the floor for now and let it go
‫to sleep and then try picking it up.
‫And you'll notice that we've got no problems picking it up, letting it lie on the floor, whatever.
‫It's going to be fine and wake up those physics rigid bodies and start simulating them so that it can
‫be lifted and grabbed.
‫Now, you may have noticed that we aren't currently able to release the physics handle, so that's going
‫to be your challenge to do that in the release function.
‫So you're going to call release component on that physics handle.
‫That is a function that's available on the physics handle pointer.
‫You're going to check first if we have a get grabbed component.
‫So if you get grab component that returns a pointer to the grab component, that points will be null
‫if the physics handle isn't currently grabbing anything.
‫So you can use that to check whether we've got anything and only release if we are currently grabbing
‫something.
‫And as a bonus, you can have a go to see if we can set the target again.
‫Only if we are grabbing something, but watch the video and have a go.
‫Welcome back.
‫So let's go over in to grab a cup.
‫Going to go into the release function and remove the release log.
‫And in here we're going to put an if statement and we're going to say if and now we need to get hold
‫of our physics handles.
‫So we're going to, first of all, do a you physics handle component pointer, which we're going to
‫call the physics handle and set that equal to get physics handle.
‫So then we're going to say if the physics handle is equal to null pointer, we want to go ahead and
‫return because we're not going to be able to do anything, release anything.
‫If we don't have a physics handle at all, then we want to check whether we are currently holding on
‫to anything with our physics handle.
‫So if, say, if the physics handle arrow get grabbed component parentheses not equals null pointer,
‫then inside the body of this if statement means we've got something currently grabbed, well then we're
‫going to do a physics handle arrow release component, which is going to let it go, as the name would
‫suggest.
‫And one thing I didn't do in the code here, but it's probably worth adding to yours, is that before
‫we release the component, it's worth going ahead and calling get grabbed components and then adding
‫the weak all rigid bodies just like we did before we grab the component.
‫Because if we were to grow a component and not move around for a little while, it may also go to sleep.
‫And so releasing it would do nothing.
‫So this is worth adding as it would catch that corner case and we want to not set the target location
‫in tick if we have got a physics handle that's not currently grabbing anything.
‫So here in our tick components, we're going to put an if statement that's going to be exactly the same.
‫It's going to be saying physics handle arrow get grabbed component not equal to null pointer.
‫Then in the body of the if statement, we're going to put the calculation of the target location and
‫the setting target location in that physics handle as well.
‫Let's recompile and try this out.
‫Let's go ahead and hit play.
‫And try and grab the gargoyle.
‫And then if I let go of the mouse, it drops down to the floor again.
‫So and it seems to not be affected by whether it's awake or asleep.
‫We seem to be waking it up no matter whether we're every time we're picking it up, essentially.
‫So that seems to be working exactly as I would like it to.
‫It's not getting stuck.
‫It's feeling just about right.
‫The only weird thing slightly is when I move into it, you can see it does this weird jittering because
‫it's actually colliding with the player.
‫Now we can fix this just a little bit by going to our gargoyle in the details pane, selecting the all
‫category, scrolling down to collision, and looking at our custom collision preset and down in the
‫object responses.
‫We'll see a bit more about this later on.
‫There is a pawn object response.
‫This basically says, Should I collide with the pawn or what?
‫What should my response be to the pawn?
‫Well, I'm going to set it to overlap with the pawn.
‫And that means that if it collides with me, it essentially just ignores it.
‫And then one more thing we have to do is because this is actually made up of two static meshes, they've
‫got their own collision settings.
‫I'm going to go down to the dirt mound.
‫That's the thing underneath the gargoyle.
‫And here I'm actually just going to to simplify things.
‫I'm going to make the dirt mound part, have no collision.
‫So I'm going to switch it to the no collision preset.
‫Then I'm going to go ahead and hit play, grab the gargoyle from the floor.
‫And now you can see when I'm running with it, there isn't any of that weird glitching because it's
‫not trying to collide with the player collision mesh.
‫Great stuff.
‫In this lecture we have learned how to make our grab a much more reliable and also how we can release
‫the grabbing component.
‫Great stuff.
‫I'll see you in the next lecture.