‫Hello and welcome.
‫In this lecture, we're going to be learning about line traces and sphere sweeps, which are going to
‫help us be able to query the world from our code.
‫We're also going to set up a collision channel so that we can have objects respond to the grabber or
‫not respond to the grabber.
‫Let's dive in and see how this works.
‫So as far as our code is concerned, it's pretty much blind.
‫It cannot tell what I am looking at right now and it cannot tell whether I am trying to pick up a gargoyle
‫or whether I might be trying to pick up something else entirely else.
‫As far as the code is concerned, it's standing in a pitch black room and it cannot see anything.
‫So how do we solve the problem of figuring out from code what is out there?
‫Well, essentially, we need to switch on a high beam flashlight and look in a certain direction to
‫see what there is.
‫Essentially flash a light into a dark scene.
‫So imagine that hidden in our dark scene.
‫We've got these shapes here.
‫What we might do is take a starting point and an ending point and essentially trace a line through from
‫the start to the end and see what that line overlaps with.
‫This called a line trace.
‫So when you do a line trace, you essentially can get back either a result, which is all of the objects
‫that the line intersected with on its path to the end.
‫Or you can just get the first object that it hit and some optimizations that mean that if it hits an
‫object, it actually doesn't do the entire trace at all.
‫So this is how we often do things like shooting guns in first person shooters or even like we're doing
‫here, picking up objects.
‫There's an alternative to the line trace, however, because the line trace can mean that we have to
‫be extremely accurate.
‫If we miss an object by a few millimeters like this sphere, it won't be picked up.
‫So the alternative is a shape trace and sometimes more specifically known as a sphere trace.
‫If that's the shape we're using or more generally it's actually known in code as a geometry trace.
‫What we do here is we take a sphere starting at our starting point, and as we as name would suggest,
‫we trace it or we sweep it, actually, we sweep the sphere through the world to the end point.
‫And if it intersected with anything along the way, then that is something that the trace will hit.
‫So for example, we will overlap here with this triangle and the sphere because we've expanded the radius
‫of the search space.
‫Now there's one more additional thing that we can add to our traces to make them ignore certain shapes
‫in the scene.
‫Because, for example, we know that we can't grab hold of this wall and maybe we want to be able to
‫grab something through the bars, even though we might be hitting the bars as we do so well.
‫The way to do that is using something called the Trace Channel, and this essentially creates a list
‫of objects that can respond to the Trace and others will that might want to ignore it entirely.
‫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.
‫Now, the place to go and do this is over in the settings.
‫Project settings.
‫Then go to engine and collision because this is all about object collision.
‫And in here you can see we have got objects, channels and we have got trace channels.
‫We're looking at particularly the Trace Channel because what we want to do is figure out how we should
‫respond to a certain type of query.
‫If we go back into our scene and click on an object and if we scroll down to an area called Collision
‫and there's a bunch of settings here about how this object should respond to other objects in terms
‫of colliding with them.
‫So if we go at the moment that's got a default collision preset, if we have a look here, there's a
‫lot of different collision presets here.
‫But let's go to the custom one so we can see what is actually involved in collision settings.
‫So we've got an object type, we've got the types of query it responds to, which is query.
‫In this case, that's things like line traces and sphere trace and sweeps and so on.
‫And then there's physics.
‫So that's the simulation of physics and whether it bounces off other objects.
‫But further down you can see there's the collision responses and this is split into trace responses
‫and object responses.
‫So what it's saying is here are some of the trace channels that are already built in.
‫There's a visibility trace channel.
‫This is basically all visible objects.
‫And there's a camera trace channel, which is basically whether the camera should be allowed to penetrate
‫through this object or not.
‫But it's isn't too important.
‫These are the types of channels that we have and we can respond to them differently.
‫You can see here that we can have a block option.
‫There's an overlap, which is an intermediate option and a completely ignore option.
‫So this is what we're interested in.
‫We're interested in adding in a new trace response channel here.
‫So let's go over to our project settings, go to our trace channel.
‫And Click Add New Trace Channel.
‫Then we are going to go in here and call it grabber and we get to choose how by default objects are
‫going to respond to this channel.
‫I want it to ignore this channel by default so that we have to specifically say that an object responds
‫to this trace channel.
‫I've got to go and click Accept, go over back into our dungeon and you can see that we're not actually
‫showing the new trace response just yet.
‫So what we're going to do is close down the editor, save the scene and relaunch from the new project
‫file.
‫So once that's launched up, let's select our gargoyle again and go down to our collision settings.
‫And sure enough, here we've got our new trace response channel grabber, which for the gargoyle we
‫want it to block basically, meaning that it will block the line of sight and so it will show up in
‫either our sphere sweep or our line trace.
‫Great stuff.
‫Hopefully now you've got a better understanding of line traces and sweeps.
‫I'll see you in the next lecture.