‫Hello and welcome.
‫In this lecture, we are going to be seeing things from the player's point of view.
‫We will be getting hold of the players viewport, the location and rotation of the camera, essentially,
‫so that when we hit the trigger button, we can draw a debug camera.
‫These little red guys here, these little red boxy cameras that show us where that viewport is.
‫And we can confirm that.
‫We can then use that later on for raytracing for the gun.
‫Let's dive in and see all of that in action.
‫Let me start this lecture with a question.
‫So if you are aiming through your viewport like this and you're trying to line up the center of your
‫screen with that D on D command center or zero command center, whatever that's meant to be.
‫And I aim what would I aim to?
‫What would I expect to hit?
‫You might say, well, I'd expect it to hit that zero.
‫Sam, that seems like the obvious answer.
‫Well, that's not necessarily what you are going to hit, depending on how you do things, how you do
‫your recasting.
‫So let's have a look at the strategies we have for recasting.
‫The most obvious is you say, Well, we're firing out of the gun, so we're going to recast down the
‫barrel of the gun.
‫Or by the way, this set up, which is very, very crude, is basically our third person set up.
‫We've got a camera back here.
‫We've got the character, we've got the gun.
‫And he's trying to shoot at another target character over here.
‫So, well, first of all, we recast out of the gun like that pure.
‫Maybe it misses because our camera is not pointing where the gun is pointing, not necessarily.
‫Even though they might be pointing in the same direction, they're slightly offset from each other,
‫so they won't necessarily hit the same things.
‫Instead, you might choose that you want to recast from the camera.
‫That means that whatever the camera is looking at is going to be the thing that is hit and that generally
‫feels more natural in third person games.
‫When you're in first person games, things get a little bit closer because often you're very close to
‫the barrel.
‫But even in first person games, this rule tends to be the case.
‫You don't want to be moving around where the aiming point is on the screen.
‫You typically have a little piece of UI that overlays here in the middle of your screen, and that's
‫where you're going to aim down.
‫And so that's why we tend to do our ray casting with ray casted weapons down this viewport.
‫Now, fortunately, there's a way for us to get this viewport and we actually do it from the controller,
‫we get the controller's viewport.
‫So that requires us to go to the character, find the character's controller, and then find its viewport
‫location and rotation.
‫And what we're going to do in this lecture is try and visualize and confirm that that viewport location
‫and rotation is what we would expect.
‫And we're going to do that by drawing some debug helpers.
‫Now the debug helpers are in a header file and the header files name is a debug.
‫Helpers.
‫Funnily enough, I believe or draw, draw debug helpers.
‫That's the one.
‫And if we go and have a look at this one F 12 to go navigate to a header file if you've already got
‫it in your includes, we're going to look for a one called camera draw debug camera.
‫And this draws something that looks a little bit like my diagram here, a little bit of a triangle or
‫a nostrum that we can use.
‫So what's it going to take in?
‫It's going to take in the world a location of that camera, a rotation for said camera.
‫So location, rotation, field of view.
‫Well, we'll set that probably to a default like 90 degrees.
‫There's a scale, there's a color defaults.
‫These are all defaulted.
‫So we can just use that defaults.
‫So let's try draw a debug camera down here in my gun and we'll put it in the pull trigger method because
‫that's why we're doing our work.
‫It needs first of all, what was it?
‫It was a new world.
‫So we get our world.
‫But we need to draw into the next thing would be a location.
‫Let's just get actor location for the time being and do a get actor rotation as well.
‫And then what was the final thing that we needed was a field of view.
‫Let's just go for a 90 degree field of view, which is just a random thing there, and we can go ahead
‫and compile and see whether this shows up.
‫And then let's hit play and hit the.
‫Trigger button.
‫I'm not seeing anything here.
‫Let's do an F eight and see if we can get closer.
‫Still not showing any kind of camera in here.
‫I'm wondering if it's really, really small.
‫So one of the other options here is that we might want to have a persistent lines.
‫And so that means we need to specify everything up to it in C++ so we can set our scale to one.
‫We could even increase it maybe to a scale of two, just in case it's the size that's a problem.
‫We can choose a color that's more visible, maybe red.
‫So F color, colon, colon, red.
‫And then we can try having persistent lines be true and try that one out for size.
‫So compiling again.
‫And jumping into play.
‫And there we go.
‫We hit.
‫The trigger.
‫And you can see that it shows up a little camera box here where we hit trigger and it's rotation and
‫location are set by the guns and location and rotation.
‫So now we want to try and get the viewport instead.
‫So as I've already said, this is going to be on our controller.
‫So let's look at Controller H and again, we want the one in the game framework and we're going to look
‫for viewport in here.
‫I beg your pardon?
‫Actually, viewpoint is the word we're looking for and there's a few here.
‫Get actor eyes a viewpoint that would be looking through the actor's eyes.
‫But we actually want the get play of viewpoint, which for an eye controller will use their eyes, but
‫for a player will use the player's camera.
‫So that's what we want.
‫We want the get player viewpoint.
‫But first of all, we have to get hold of that controller in the first place.
‫And the way to do that is, first of all, we need to get our owner remember that when we had our shooter
‫character CP open and we go down to or go up to our Begin play, you can see we're setting the gun's
‫owner to be the character.
‫That means that in the gun we can go the other way.
‫We can say get owner and that's going to get us the shooter character.
‫Now it's not going to know that it's a shooter character.
‫All it knows is that it is an actor.
‫So we're going to have to do a cast here.
‫So we'll do a cast to the A Shooter character.
‫And in fact, we don't even need to know anything as much as the fact that it's a shooter character.
‫It's sufficient to know that it is a pawn and any pawn can then possess this gun, even if it's not
‫a shooter character.
‫So try and be as nonspecific as possible.
‫Why does it need to be a pawn, though?
‫It needs to be a pawn because we need to get a controller and only pawns can be possessed by controllers.
‫So let's put this in a variable.
‫It's going to be a -- star and then owner -- is going to be the name of the variable.
‫Then we need to check that this is not null.
‫And I'm just going to do this with a one liner that says if it is null, we return early from our pool
‫trigger so that we don't have any risk of trying to call anything with a null pointer.
‫So then we can do a pawn owner, get controller and we want to save that in to the A controller.
‫Star And this can be the owner.
‫Controller.
‫Like so.
‫And again, we want to just check that this isn't null because we're going to be calling a method on
‫that and we can't call methods on null pointers.
‫It makes unreal, unhappy and crash.
‫So this is where we can now do our owner controller.
‫And we should be able to call to that get play a viewpoint.
‫So here comes the challenge for you to set that location and rotation.
‫Using that new function, you can see the signature for it.
‫Again, we go to Controller H ahead of file and look at that get play a viewpoint.
‫Try and see if from this signature you can work out how to use it to get that location and rotation
‫that we want to pass in to our draw debug helper.
‫And so you're going to remember that this is using out parameters.
‫So revise your out parameters, draw the debug camera using that snippet that we've just written and
‫test it in the viewport.
‫Go and click the trigger button a few times in different locations and see where that camera ends up
‫showing pause video and have a go.
‫Okay.
‫Welcome back.
‫So get play a viewpoint.
‫I'm going to copy the name of the method and bring it over here because it's an out parameter.
‫I will need to declare first the variables that I want those values to go into.
‫I've got an F vector location.
‫We have got an F rotator rotation.
‫And we want to pass those in as the arguments.
‫Let me just fix up my indentation there a little bit.
‫And we're going to pass in the location, the rotation.
‫And we know that because it's just been declared here when reading this, we can guess that these are
‫now out parameters, even though there isn't any syntax helpfulness from C++ on that regard.
‫And then we can pass this location instead of the actor location, and we can pass this rotation instead
‫of the active rotation into our draw debug camera.
‫And we can go ahead and build that compile and try it out in the editor.
‫Well, it looks like the compile is complete.
‫Let's go and play.
‫And when I hit Trigger, you could see a camera appearing right over my viewpoint.
‫That's a good sign.
‫If I step back a little bit, you can see there you go.
‫We've got these cameras that are pointing in exactly the right direction from where I was just looking.
‫So you can just imagine how we're going to be able to shoot a ray out of that camera down the end of
‫the camera and try and hit something over there that I was looking at at the time.
‫And that's what we're going to be doing in the next lecture.
‫See you there.