﻿1
00:00:04,350 --> 00:00:04,760
‫Hello.

2
00:00:04,800 --> 00:00:05,190
‫Welcome.

3
00:00:05,190 --> 00:00:09,750
‫In this lecture, we're going to be making the grabber much more reliable.

4
00:00:09,750 --> 00:00:16,860
‫And also we are going to be making it possible to drop a grabbed component so that we can throw it around

5
00:00:16,860 --> 00:00:19,890
‫and generally have it be a little bit more useful.

6
00:00:19,920 --> 00:00:21,870
‫Let's dive in and see how this is done.

7
00:00:22,850 --> 00:00:28,430
‫Now, the reason we're often not able to pick up this gargoyle is that components that are simulating

8
00:00:28,430 --> 00:00:35,990
‫physics for performance sake will go to sleep when they have been still for a certain amount of time.

9
00:00:35,990 --> 00:00:37,310
‫And that's what we're seeing.

10
00:00:37,310 --> 00:00:43,310
‫This gargoyles physics simulation is going to sleep and it's not being woken up when we attached to

11
00:00:43,310 --> 00:00:45,290
‫it with our physics handle.

12
00:00:45,290 --> 00:00:51,400
‫So what we need to learn to do is to wake it up when we hit it with a sphere trace.

13
00:00:51,410 --> 00:00:56,420
‫Now let's go and have a look at the you primitive component documentation we've seen.

14
00:00:56,420 --> 00:01:02,420
‫The you primitive component is the thing returned by our hit result, but what actually is a primitive

15
00:01:02,420 --> 00:01:03,260
‫component.

16
00:01:03,440 --> 00:01:10,010
‫So it says here in the documentation that a primitive component is a scene component that contains or

17
00:01:10,010 --> 00:01:16,160
‫generates some sort of geometry and is rendered or used as collision data.

18
00:01:16,160 --> 00:01:21,770
‫So generally speaking, primitive components are ones that can simulate physics.

19
00:01:22,130 --> 00:01:27,830
‫So we can expect to find some physics simulation methods on here.

20
00:01:27,830 --> 00:01:32,030
‫And what I'm going to look for is a function with the keyword wake in it.

21
00:01:32,060 --> 00:01:37,820
‫We're going to look through a few functions until we find one that says Wake all rigid bodies.

22
00:01:38,270 --> 00:01:43,610
‫Rigid bodies are essentially a physics term for objects that simulate physics.

23
00:01:43,610 --> 00:01:44,780
‫So you can see it has a void.

24
00:01:44,780 --> 00:01:46,700
‫Return type doesn't take any arguments.

25
00:01:46,700 --> 00:01:52,010
‫So we should be able to just call this when we are trying to attach to it.

26
00:01:52,010 --> 00:01:58,220
‫So if we go to our grab a CP inside our if statement, we've hit this component, we're about to grab

27
00:01:58,220 --> 00:01:58,610
‫it.

28
00:01:58,610 --> 00:02:04,550
‫The first thing we need to do is maybe let's make a variable to store this you primitive component.

29
00:02:04,550 --> 00:02:15,170
‫So we'll have a you primitive component pointer called hit component set that equal to the hit result

30
00:02:15,470 --> 00:02:17,990
‫dot get component.

31
00:02:18,530 --> 00:02:27,770
‫And then using that hit component pointer, we can arrow wake all rigid bodies and then put the parentheses

32
00:02:27,770 --> 00:02:29,690
‫and the semicolon at the end.

33
00:02:29,690 --> 00:02:35,900
‫So this should just give that component a kick, wake it up so that it responds to the physics handle.

34
00:02:35,930 --> 00:02:39,160
‫Now let's replace where we've done hit results.

35
00:02:39,200 --> 00:02:46,400
‫Don't get components in our physics handle call to grab component location because we've now got a variable

36
00:02:46,400 --> 00:02:49,460
‫for that and let's recompile when that's done.

37
00:02:49,460 --> 00:02:56,480
‫Let's go ahead and hit play and let the gargoyle sit a little bit on the floor for now and let it go

38
00:02:56,480 --> 00:02:58,910
‫to sleep and then try picking it up.

39
00:02:58,910 --> 00:03:05,120
‫And you'll notice that we've got no problems picking it up, letting it lie on the floor, whatever.

40
00:03:05,120 --> 00:03:12,440
‫It's going to be fine and wake up those physics rigid bodies and start simulating them so that it can

41
00:03:12,440 --> 00:03:13,910
‫be lifted and grabbed.

42
00:03:13,910 --> 00:03:18,560
‫Now, you may have noticed that we aren't currently able to release the physics handle, so that's going

43
00:03:18,560 --> 00:03:23,300
‫to be your challenge to do that in the release function.

44
00:03:23,360 --> 00:03:26,570
‫So you're going to call release component on that physics handle.

45
00:03:26,570 --> 00:03:29,660
‫That is a function that's available on the physics handle pointer.

46
00:03:29,840 --> 00:03:34,910
‫You're going to check first if we have a get grabbed component.

47
00:03:34,910 --> 00:03:40,040
‫So if you get grab component that returns a pointer to the grab component, that points will be null

48
00:03:40,040 --> 00:03:43,010
‫if the physics handle isn't currently grabbing anything.

49
00:03:43,010 --> 00:03:48,110
‫So you can use that to check whether we've got anything and only release if we are currently grabbing

50
00:03:48,110 --> 00:03:48,530
‫something.

51
00:03:48,530 --> 00:03:53,090
‫And as a bonus, you can have a go to see if we can set the target again.

52
00:03:53,090 --> 00:03:58,070
‫Only if we are grabbing something, but watch the video and have a go.

53
00:04:01,590 --> 00:04:02,470
‫Welcome back.

54
00:04:02,490 --> 00:04:06,090
‫So let's go over in to grab a cup.

55
00:04:06,230 --> 00:04:10,490
‫Going to go into the release function and remove the release log.

56
00:04:10,500 --> 00:04:16,650
‫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

57
00:04:16,650 --> 00:04:18,360
‫of our physics handles.

58
00:04:18,360 --> 00:04:25,650
‫So we're going to, first of all, do a you physics handle component pointer, which we're going to

59
00:04:25,650 --> 00:04:31,410
‫call the physics handle and set that equal to get physics handle.

60
00:04:31,590 --> 00:04:40,770
‫So then we're going to say if the physics handle is equal to null pointer, we want to go ahead and

61
00:04:40,770 --> 00:04:44,400
‫return because we're not going to be able to do anything, release anything.

62
00:04:44,400 --> 00:04:51,330
‫If we don't have a physics handle at all, then we want to check whether we are currently holding on

63
00:04:51,330 --> 00:04:52,650
‫to anything with our physics handle.

64
00:04:52,650 --> 00:05:03,390
‫So if, say, if the physics handle arrow get grabbed component parentheses not equals null pointer,

65
00:05:04,080 --> 00:05:09,420
‫then inside the body of this if statement means we've got something currently grabbed, well then we're

66
00:05:09,420 --> 00:05:16,980
‫going to do a physics handle arrow release component, which is going to let it go, as the name would

67
00:05:16,980 --> 00:05:17,730
‫suggest.

68
00:05:17,730 --> 00:05:24,450
‫And one thing I didn't do in the code here, but it's probably worth adding to yours, is that before

69
00:05:24,450 --> 00:05:32,160
‫we release the component, it's worth going ahead and calling get grabbed components and then adding

70
00:05:32,160 --> 00:05:36,180
‫the weak all rigid bodies just like we did before we grab the component.

71
00:05:36,180 --> 00:05:41,520
‫Because if we were to grow a component and not move around for a little while, it may also go to sleep.

72
00:05:41,520 --> 00:05:43,470
‫And so releasing it would do nothing.

73
00:05:43,470 --> 00:05:51,300
‫So this is worth adding as it would catch that corner case and we want to not set the target location

74
00:05:51,300 --> 00:05:56,790
‫in tick if we have got a physics handle that's not currently grabbing anything.

75
00:05:56,790 --> 00:06:02,190
‫So here in our tick components, we're going to put an if statement that's going to be exactly the same.

76
00:06:02,190 --> 00:06:11,730
‫It's going to be saying physics handle arrow get grabbed component not equal to null pointer.

77
00:06:11,760 --> 00:06:18,810
‫Then in the body of the if statement, we're going to put the calculation of the target location and

78
00:06:18,810 --> 00:06:22,380
‫the setting target location in that physics handle as well.

79
00:06:22,380 --> 00:06:24,330
‫Let's recompile and try this out.

80
00:06:24,330 --> 00:06:26,070
‫Let's go ahead and hit play.

81
00:06:26,970 --> 00:06:28,920
‫And try and grab the gargoyle.

82
00:06:28,920 --> 00:06:33,420
‫And then if I let go of the mouse, it drops down to the floor again.

83
00:06:33,420 --> 00:06:39,000
‫So and it seems to not be affected by whether it's awake or asleep.

84
00:06:39,000 --> 00:06:43,610
‫We seem to be waking it up no matter whether we're every time we're picking it up, essentially.

85
00:06:43,620 --> 00:06:46,950
‫So that seems to be working exactly as I would like it to.

86
00:06:46,950 --> 00:06:47,910
‫It's not getting stuck.

87
00:06:47,910 --> 00:06:49,950
‫It's feeling just about right.

88
00:06:49,980 --> 00:06:56,520
‫The only weird thing slightly is when I move into it, you can see it does this weird jittering because

89
00:06:56,520 --> 00:06:59,550
‫it's actually colliding with the player.

90
00:06:59,550 --> 00:07:06,840
‫Now we can fix this just a little bit by going to our gargoyle in the details pane, selecting the all

91
00:07:06,840 --> 00:07:13,950
‫category, scrolling down to collision, and looking at our custom collision preset and down in the

92
00:07:13,950 --> 00:07:15,510
‫object responses.

93
00:07:15,510 --> 00:07:17,760
‫We'll see a bit more about this later on.

94
00:07:17,760 --> 00:07:20,910
‫There is a pawn object response.

95
00:07:20,910 --> 00:07:25,650
‫This basically says, Should I collide with the pawn or what?

96
00:07:25,680 --> 00:07:27,570
‫What should my response be to the pawn?

97
00:07:27,570 --> 00:07:31,740
‫Well, I'm going to set it to overlap with the pawn.

98
00:07:31,740 --> 00:07:36,690
‫And that means that if it collides with me, it essentially just ignores it.

99
00:07:36,690 --> 00:07:41,880
‫And then one more thing we have to do is because this is actually made up of two static meshes, they've

100
00:07:41,880 --> 00:07:43,890
‫got their own collision settings.

101
00:07:43,890 --> 00:07:46,320
‫I'm going to go down to the dirt mound.

102
00:07:46,320 --> 00:07:48,720
‫That's the thing underneath the gargoyle.

103
00:07:49,110 --> 00:07:52,350
‫And here I'm actually just going to to simplify things.

104
00:07:52,350 --> 00:07:57,480
‫I'm going to make the dirt mound part, have no collision.

105
00:07:57,490 --> 00:08:00,300
‫So I'm going to switch it to the no collision preset.

106
00:08:00,510 --> 00:08:05,250
‫Then I'm going to go ahead and hit play, grab the gargoyle from the floor.

107
00:08:05,250 --> 00:08:09,540
‫And now you can see when I'm running with it, there isn't any of that weird glitching because it's

108
00:08:09,540 --> 00:08:14,730
‫not trying to collide with the player collision mesh.

109
00:08:14,760 --> 00:08:15,540
‫Great stuff.

110
00:08:15,540 --> 00:08:22,170
‫In this lecture we have learned how to make our grab a much more reliable and also how we can release

111
00:08:22,170 --> 00:08:23,790
‫the grabbing component.

112
00:08:23,790 --> 00:08:24,510
‫Great stuff.

113
00:08:24,510 --> 00:08:26,190
‫I'll see you in the next lecture.

