‫Hello and welcome.
‫In this lecture, we're going to be getting a line of sight to our player from the enemy so that we
‫are able, as the player, to hide behind obstacles and not have that enemy followers not have it.
‫Keep aiming at us until we pop back out and are visible to them.
‫So you can see here I've hidden behind a corner and he can't find me.
‫Let's dive in and see how this can be done using unreal tools.
‫So it's not ideal that this enemy A.I. can currently follow us round corners.
‫No matter how far we go out, no matter how much we hide behind pillars, it's still going to find us.
‫It's still going to come and keep aiming at us.
‫We want to correct that, and we're going to do this by checking the line of sight from the enemy to
‫the player.
‫Now, as many things here in Unreal, there is already a function for this in the eye controller called
‫line of sight.
‫And if you go and search for that, you can find that there's a function that returns a boolean.
‫So telling us whether or not we have line of sight and then it takes the actor that we should try to
‫check for line of sight to, and it automatically takes into account things like the eye position of
‫the pawn in question.
‫All this stuff is already coded into Unreal is really cool, so it should be fairly straightforward.
‫If we go over to our AI controller DHCP, what we basically want to do is change things up in TIC so
‫that we first of all do an if line of sight.
‫Then what we want to do if we've got line of sight, is we want to move to and we want to set focus.
‫However, if we do not so in the else, what we want to do is clear focus.
‫Remember, we saw that function before and we want to do the let's see the stop movement, basically.
‫And that's another function that exists that we haven't seen yet.
‫So what I'm going to do here is I can challenge you to implement the whole of this yourself if you feel
‫confident.
‫Otherwise, this else is the more complex bit that we haven't seen before because the other two, we've
‫already done a move to actor, we've already done our set focus, we've done that before.
‫And there's just this if line of sight, which we have just seen.
‫It's pretty straightforward.
‫You could implement that.
‫But how about this else?
‫Let's give a go at implementing the just the ELT clause.
‫It's not going to compile because you obviously need an if before an else, but let's go and just write
‫out the ELT clause.
‫So the first thing clearing focus, basically we're saying stop trying to track the player because you
‫can't see the player.
‫You shouldn't be giving off the impression that you know where the player is, even though the eye obviously
‫can, because it can access the player's location and so on and so forth.
‫But we need to give the illusion that the AI only knows as much as it can see.
‫So what we're going to do is we're going to call to clear focus.
‫And if we go over to the AI controller H and have a look at clear focus, you can see that it takes
‫in the priority that we need to clear.
‫Now, a reminder that these priorities are basically different levels of focus.
‫So we have typically default movement and gameplay gameplay being the highest priority thing.
‫And what we set by default is gameplay priority.
‫So what we want to do is clear the gameplay priority, which means that if we have focused based on
‫movement, for example, if our eye then wants to move back to its guard point, then that focus will
‫not be cleared and it will be off moving back to its guarding point.
‫So that's the way focus works.
‫That's the way the priority system works there.
‫So what we just want to do is copy this a focus priority code on colon gameplay, and that is the priority
‫that we're wanting to clear on this line here.
‫And then the other thing is to stop the movement.
‫So we want to go over to the AI controlled RH again and have a look at the stop movement.
‫And sure enough, there is a simple no arguments function called stop movement, which allows us to
‫do just that.
‫So there you go.
‫That is basically the statement written.
‫So now just reminds it remains a challenge for you to implement the if portion with the if line of sight
‫and the move to and set focus.
‫And you basically want to remove this code here.
‫I'm just going to comment it out and sort out where you want that to be.
‫So pause video.
‫No challenge, slide here.
‫Just go and implement these three lines for yourself.
‫Okay.
‫Welcome back.
‫So let's start with that.
‫If so, if open brackets line of sight to and then we want to do line of sight to the actor, which
‫is the player pawn that we want to get access to.
‫So let's just uncomment that player pawn here and it's mucked up our indentation a little.
‫Let's fix that.
‫And then we want our brackets, our curly brackets here, and we want to do a move to actor.
‫Well, in fact, we can just copy and uncomment our move to actor here.
‫And we want to uncomment it.
‫Yes, there we go.
‫And the focus, we can move that as well.
‫Now, the focus, it doesn't matter whether you do it before or after move to actor, because while
‫move to actor does set a focus, it sets a lower priority, the movement focus rather than the gameplay
‫focus.
‫So it doesn't matter which order we do those two operations in.
‫And then finally we can remove this commented out line where we were getting the player pawn in begin
‫play because we no longer need it and everything's in tick now so we could remove begin play.
‫I'm going to leave it around in case we need it later on just to save me the hassle of adding it back
‫in.
‫Let's head over to Unreal and compile and when that's done, let's play.
‫So let's see if we can hide from this actor.
‫I'm going to try and move around the corners reasonably quickly.
‫This is a little bit tricky because they can move just as fast as you can, but if you get round a corner
‫like that, they go, He's lost sight of me.
‫He's stopped trying to chase me.
‫I can just peek around the corner and see that as I'm moving.
‫He's not changing his aiming because we've cleared the focus.
‫Can peek around the corner from the other side.
‫Let's see.
‫Pick round over here.
‫There you go.
‫You can see he's still aiming at the last location where I was.
‫But if I pop up into sight, then he carries on aiming and moves within the acceptance radius.
‫Talking of which, I want that acceptance radius to be configurable by the designer.
‫So let's move it into a new property.
‫I'm going to create a private section here with a new property.
‫Which is going to be an edit anywhere.
‫And we want that to be a float called acceptance radius and give it the default value of 200.
‫And then this acceptance radius we can go ahead and use in our move to actor.
‫And if you compile that, it's going to give you exactly the same behavior, except now that you can
‫change that in the AI controller blueprint.
‫So that's it for this lecture.
‫We have discovered that line of sight function, allowing us to check whether the eye can or cannot
‫see something.
‫And we've implemented this basic hiding functionality so the AI doesn't follow us when it can't see
‫us.
‫Next lecture.
‫We're going to take things up a notch from this custom programmed AI and use behavior trees.
‫A really powerful tool in unreal to make our AI's just that much cooler.