﻿1
00:00:04,350 --> 00:00:05,340
‫Hello and welcome.

2
00:00:05,340 --> 00:00:11,250
‫In this lecture, we're going to be learning about line traces and sphere sweeps, which are going to

3
00:00:11,250 --> 00:00:15,450
‫help us be able to query the world from our code.

4
00:00:15,450 --> 00:00:22,200
‫We're also going to set up a collision channel so that we can have objects respond to the grabber or

5
00:00:22,200 --> 00:00:23,820
‫not respond to the grabber.

6
00:00:23,850 --> 00:00:25,980
‫Let's dive in and see how this works.

7
00:00:26,650 --> 00:00:29,950
‫So as far as our code is concerned, it's pretty much blind.

8
00:00:29,950 --> 00:00:37,390
‫It cannot tell what I am looking at right now and it cannot tell whether I am trying to pick up a gargoyle

9
00:00:37,390 --> 00:00:41,300
‫or whether I might be trying to pick up something else entirely else.

10
00:00:41,320 --> 00:00:46,390
‫As far as the code is concerned, it's standing in a pitch black room and it cannot see anything.

11
00:00:46,600 --> 00:00:51,790
‫So how do we solve the problem of figuring out from code what is out there?

12
00:00:51,820 --> 00:00:58,780
‫Well, essentially, we need to switch on a high beam flashlight and look in a certain direction to

13
00:00:58,780 --> 00:01:00,160
‫see what there is.

14
00:01:00,160 --> 00:01:02,740
‫Essentially flash a light into a dark scene.

15
00:01:02,740 --> 00:01:05,510
‫So imagine that hidden in our dark scene.

16
00:01:05,530 --> 00:01:07,600
‫We've got these shapes here.

17
00:01:07,630 --> 00:01:15,520
‫What we might do is take a starting point and an ending point and essentially trace a line through from

18
00:01:15,520 --> 00:01:20,140
‫the start to the end and see what that line overlaps with.

19
00:01:20,140 --> 00:01:22,480
‫This called a line trace.

20
00:01:22,780 --> 00:01:28,720
‫So when you do a line trace, you essentially can get back either a result, which is all of the objects

21
00:01:28,720 --> 00:01:31,990
‫that the line intersected with on its path to the end.

22
00:01:32,410 --> 00:01:38,170
‫Or you can just get the first object that it hit and some optimizations that mean that if it hits an

23
00:01:38,170 --> 00:01:41,180
‫object, it actually doesn't do the entire trace at all.

24
00:01:41,200 --> 00:01:48,550
‫So this is how we often do things like shooting guns in first person shooters or even like we're doing

25
00:01:48,550 --> 00:01:50,500
‫here, picking up objects.

26
00:01:50,770 --> 00:01:56,290
‫There's an alternative to the line trace, however, because the line trace can mean that we have to

27
00:01:56,290 --> 00:01:57,730
‫be extremely accurate.

28
00:01:57,730 --> 00:02:02,240
‫If we miss an object by a few millimeters like this sphere, it won't be picked up.

29
00:02:02,260 --> 00:02:08,260
‫So the alternative is a shape trace and sometimes more specifically known as a sphere trace.

30
00:02:08,260 --> 00:02:14,820
‫If that's the shape we're using or more generally it's actually known in code as a geometry trace.

31
00:02:14,830 --> 00:02:22,750
‫What we do here is we take a sphere starting at our starting point, and as we as name would suggest,

32
00:02:22,750 --> 00:02:28,690
‫we trace it or we sweep it, actually, we sweep the sphere through the world to the end point.

33
00:02:28,690 --> 00:02:35,000
‫And if it intersected with anything along the way, then that is something that the trace will hit.

34
00:02:35,020 --> 00:02:43,420
‫So for example, we will overlap here with this triangle and the sphere because we've expanded the radius

35
00:02:43,420 --> 00:02:45,180
‫of the search space.

36
00:02:45,190 --> 00:02:52,450
‫Now there's one more additional thing that we can add to our traces to make them ignore certain shapes

37
00:02:52,450 --> 00:02:52,990
‫in the scene.

38
00:02:52,990 --> 00:02:57,820
‫Because, for example, we know that we can't grab hold of this wall and maybe we want to be able to

39
00:02:57,820 --> 00:03:03,580
‫grab something through the bars, even though we might be hitting the bars as we do so well.

40
00:03:03,580 --> 00:03:09,790
‫The way to do that is using something called the Trace Channel, and this essentially creates a list

41
00:03:09,790 --> 00:03:16,930
‫of objects that can respond to the Trace and others will that might want to ignore it entirely.

42
00:03:17,410 --> 00:03:25,750
‫So let's take a look at how to set up our own trace channel so that we can pick up objects in our scene.

43
00:03:25,990 --> 00:03:28,990
‫Now, the place to go and do this is over in the settings.

44
00:03:28,990 --> 00:03:30,220
‫Project settings.

45
00:03:30,220 --> 00:03:35,320
‫Then go to engine and collision because this is all about object collision.

46
00:03:35,410 --> 00:03:40,840
‫And in here you can see we have got objects, channels and we have got trace channels.

47
00:03:40,840 --> 00:03:46,870
‫We're looking at particularly the Trace Channel because what we want to do is figure out how we should

48
00:03:46,870 --> 00:03:50,500
‫respond to a certain type of query.

49
00:03:50,830 --> 00:04:00,280
‫If we go back into our scene and click on an object and if we scroll down to an area called Collision

50
00:04:00,280 --> 00:04:07,540
‫and there's a bunch of settings here about how this object should respond to other objects in terms

51
00:04:07,540 --> 00:04:08,590
‫of colliding with them.

52
00:04:08,590 --> 00:04:14,800
‫So if we go at the moment that's got a default collision preset, if we have a look here, there's a

53
00:04:14,800 --> 00:04:17,080
‫lot of different collision presets here.

54
00:04:17,080 --> 00:04:24,220
‫But let's go to the custom one so we can see what is actually involved in collision settings.

55
00:04:24,790 --> 00:04:30,250
‫So we've got an object type, we've got the types of query it responds to, which is query.

56
00:04:30,250 --> 00:04:34,630
‫In this case, that's things like line traces and sphere trace and sweeps and so on.

57
00:04:34,630 --> 00:04:35,740
‫And then there's physics.

58
00:04:35,740 --> 00:04:39,400
‫So that's the simulation of physics and whether it bounces off other objects.

59
00:04:39,790 --> 00:04:45,070
‫But further down you can see there's the collision responses and this is split into trace responses

60
00:04:45,070 --> 00:04:46,720
‫and object responses.

61
00:04:46,720 --> 00:04:50,590
‫So what it's saying is here are some of the trace channels that are already built in.

62
00:04:50,590 --> 00:04:52,660
‫There's a visibility trace channel.

63
00:04:52,660 --> 00:04:54,970
‫This is basically all visible objects.

64
00:04:54,970 --> 00:05:01,360
‫And there's a camera trace channel, which is basically whether the camera should be allowed to penetrate

65
00:05:01,360 --> 00:05:02,590
‫through this object or not.

66
00:05:02,590 --> 00:05:04,060
‫But it's isn't too important.

67
00:05:04,060 --> 00:05:08,530
‫These are the types of channels that we have and we can respond to them differently.

68
00:05:08,530 --> 00:05:11,320
‫You can see here that we can have a block option.

69
00:05:11,320 --> 00:05:16,870
‫There's an overlap, which is an intermediate option and a completely ignore option.

70
00:05:16,870 --> 00:05:18,190
‫So this is what we're interested in.

71
00:05:18,220 --> 00:05:23,110
‫We're interested in adding in a new trace response channel here.

72
00:05:23,110 --> 00:05:25,810
‫So let's go over to our project settings, go to our trace channel.

73
00:05:26,610 --> 00:05:29,430
‫And Click Add New Trace Channel.

74
00:05:29,460 --> 00:05:37,320
‫Then we are going to go in here and call it grabber and we get to choose how by default objects are

75
00:05:37,320 --> 00:05:38,990
‫going to respond to this channel.

76
00:05:39,000 --> 00:05:45,660
‫I want it to ignore this channel by default so that we have to specifically say that an object responds

77
00:05:45,660 --> 00:05:46,890
‫to this trace channel.

78
00:05:46,950 --> 00:05:52,440
‫I've got to go and click Accept, go over back into our dungeon and you can see that we're not actually

79
00:05:52,440 --> 00:05:54,840
‫showing the new trace response just yet.

80
00:05:54,840 --> 00:06:00,900
‫So what we're going to do is close down the editor, save the scene and relaunch from the new project

81
00:06:00,900 --> 00:06:01,410
‫file.

82
00:06:01,410 --> 00:06:06,960
‫So once that's launched up, let's select our gargoyle again and go down to our collision settings.

83
00:06:06,960 --> 00:06:12,510
‫And sure enough, here we've got our new trace response channel grabber, which for the gargoyle we

84
00:06:12,510 --> 00:06:19,170
‫want it to block basically, meaning that it will block the line of sight and so it will show up in

85
00:06:19,170 --> 00:06:22,080
‫either our sphere sweep or our line trace.

86
00:06:22,110 --> 00:06:22,710
‫Great stuff.

87
00:06:22,710 --> 00:06:27,680
‫Hopefully now you've got a better understanding of line traces and sweeps.

88
00:06:27,690 --> 00:06:29,580
‫I'll see you in the next lecture.

