Game development on .NET

Unity Navigation – Part 1

dotGame
Unity Navigation – Part 1
5 (100%) 1 vote
(Video Transcript)
Hi guys. Welcome to this episode of That Game. I’m Stacey Haffner and today we’re going to be looking at moving our character around in the scene that we’ve set up. So the style we’re going to create is a point-and-click. So we’ll click somewhere and she’s gonna move to it. We’ll actually have a little bit of a sprite that’s gonna be a circle and it’ll pulse to show the user where they clicked. And then we are going to explore the navigation mesh, the nav agent, and then the nav obstacle inside of Unity. So, we probably won’t get to all of that in this video. I’m guessing this will be a two part series, but we’ll see how far we end up making it. So, diving in, we have the scene where we have left off in the last video. We have our witch/wizard player here. And then we ,of course, have our merchant that we’ll eventually mosey our way on over to. So the first thing to talk about is really what Unity’s navigation system is. So If we click over, under the Inspector there should be, if you have the default layout you’ll have this Navigation tab.

If you don’t have it, just go under Window and somewhere around here it’ll be. There it is. It’ll be here and you can click that and bring it up. But the first thing we wanna do is basically define the navigation area. Which is basically where we want the person to be able to walk. So, click on the environment and then ground. What we can do is turn that on. So again in this navigation section, what I’m gonna do is enable navigation static. We’re not gonna touch mesh off mesh links. We’re actually really not gonna even talk about them in this video, but we’ll come back and do some more complex navigation stuff, which will involve the off mesh links. So for now I’m just gonna skip over that. And then what were gonna do is define this area as walkable. And there’s actually, you can see there’s a couple different options and this is, you can specify the different areas so. If you look up here we have Object, Bake, and Areas. If I clicked Areas, we could actually add one here.

Come back over and pick that as our area, if we wanted. So we don’t need to do anything a little bit more complex with this one, so it’s really not needed. So I’m just gonna clear it out. Oops. That should have cleared it. So you can think of wanting to do kind of the areas if you have something slightly more complex. So, for example, a game that I was working on previously, we had a whole AI where we had a city and cars were driving around and you had pedestrians that had to walk across a street. And so we had different zones separated out for a navigation based off of whether or not we wanted somebody to be able to walk across a certain area, and what time that we actually wanted them to walk during that. So, for example, obviously you don’t want them to go darting across the intersection when the light is red. They happen to do that a lot anyway, but it does allow you to at least fine tune that behavior a little bit more especially when it comes to the coding side of things, okay.

So with that setup we just kinda have the basic stuff, I’m gonna hit this bake button. What we’ll see happen is when and it did some processing very quickly, and it overlaid this blue area. This is basically the navmesh that got generated. So, what Unity does when you’re working with the navigation is it takes your mesh, which is essentially your raw object, for lack of a better way to put it. And it will define the walkable paths based off of that mesh. So if you had some stairway and some hallways and things like that you could define that like your navigation maps up the stairs and through the hallway. And you can fine tune this a little bit more in the bait settings of the navigation window. So you can see there’s actually a whole set of different options. So just kind of briefly taking a look. Radius or agent radius as they call it, is how close the character can get to the obstacle. So the number will basically define a hallway. It will define how wide really this mesh is inside a particular area.

The next is the height which is how tall the ceiling must be for the player to walk underneath it. So we might explore something like that, maybe within the cave if we want to determine whether or not the person can actually navigate inside of this area. There might be an area where that’s useful. The next one down is max’s slope and what this is basically how steep the slope has to be in order to be considered. And then the last one that we have is the step height. And that’s how tall an individual step or bump needs to be before it blocks the player. So again that will be, and handy if maybe you have a hill or stairs or something, that you wanted them to navigate up. So again, I said earlier we’re gonna skip the off mesh stuff. So I’m gonna not talk about this, but to give you just a brief, what is that kind of explanation. That comes into play when you have maybe two meshes that aren’t connected, but you want to somehow navigate between them. So you have a really, really tall hill and you maybe want, this is a bad example of a hill, but maybe one where it’s like a bluff, where they come up.

This is a better example. Where they can come up, and then they’re at the peak. But maybe you want them to be able to jump off of that here so that they’re traversing non mesh area. So that’s where working with off mesh links come into play. So we’ll explore more in-depth kinda navigation stuff eventually. And that’s where we’ll look at it. Okay, so this is great in terms of defining the area that our player can walk on. But we really haven’t defined who or what object we want to be able to navigate that. And that comes into play with what’s known as a navmesh agent. And that’s a component that you can actually add to your character. So if we again come over to our body. And whoops. I’m gonna figure all of this out and go the long way around this. It’s actually probably in a, yeah. I was gonna say, are you sure you actually know where that. I’m so organized, cuz I always type. So under Navigation, you can actually see there’s a few. We’re gonna add this navmesh agent component to our game object.

So, we can see a couple things have happened here. The game object is here but more interestingly enough is this kinda green circle around our character. Which is basically, so we have the radius, the height, and the base offset. This is defining the navmesh agents kinda where, oops where it’s located in. So I guess I’ll just do. I’ll do that for now. So, it’s really quite strange that it’s so Really that’s even positioned remotely where it’s at. I’ve never had it do this before. Oops, [INAUDIBLE] buttons. Height is See it. So the base offset. [LAUGH] Our numbers are so tiny. This is why. This scaled so weird. I don’t now how, I’m not sure how that happened. At least, I’m not fixing that right now, I will fix that later. So point Wonder if, alright, so we’re gonna go into slightly uncharted territory right now. Just because my model looks like I set that up a little bit. So I have been learning modeling. 3D modeling. So I am not, by any means, an expert. There we go.

I turned on the wrong one. An expert of it, and I’m guessing I probably set it up a little bit [INAUDIBLE]. All right, so at least our navmesh agent is really her height. So about [INAUDIBLE]. There we go, okay sorry about that little deviation. Alright so taking a look at the agent itself, as you can tell there’s a lot of different properties tied to it. So you’ve just seen pretty in depth the radius, the height, and the base offsets so I’m gonna skip talking about those. And we’ll jump into the steering. So, steering speed is pretty obvious, it is the agent’s max speed in which it can move. Angular speed is how quickly they can turn. And again, I’m kind of in this area. Acceleration is how quickly they can accelerate. Stopping distance is the distance from the target that the agent will slow down. Now, We’ll talk a little bit, I think, about targeting and pathfinding a little more cuz we’re gonna actually code this up a bit, so that when we click, it sends where we said we want this person to go to.

And that’s essentially, once we do that, it’ll send that information to it, and then it’ll try to calculate a path in which it can get to where we told it to get to. And then the last thing kind of in this area is the auto braking. And that indicates if they’ll braking as the agent nears the target. Which is really good for kinda keeping it away from overshooting where it’s trying go. Now we have obstacle avoidance. And then there’s two items under here. So there’s quality, so you can see there’s a few different options. So the higher the quality, the more accurate avoiding will be. The lower the quality, the more efficient it will be, but a little bit less accurate. Priority is which agents have priority when navigating. So the higher the priority, the lower this number will be. For some reason, I guess that makes sense. Priority one tends to be a higher priority. For some reason, my head actually tends to reverse that, I don’t know why. Okay, so the last section is path finding.

So this one, that you can’t really read all, is Auto Traverse Off Mesh Link. Again we’ll kinda skip that, and we’ll talk about it later. We have Auto Repath, which lets the agent rebuild the path if it becomes invalid. And reasons that it might be invalid is, say you have an object. So maybe this crate, and that has an obstacle, NavMesh obstacle game object on it, which we’ll talk about shortly here. And that’s blocking the path. So that basically, the auto repath would say like I can’t make it here, and then it’ll calculate a different way if possible for it to go. [INAUDIBLE] And then our last one is the Area Mask. So this says what layers it can traverse. So you can actually see Walkable, Not Walkable, Jump. These are the ones that we looked at earlier. So had I kept that temple in here, that would also be here. So this is where, again, kind of fine-tuning that navigation can come into play a bit, Which is great. So what we’ll do is hook up a little bit of code to get it to move or to get our wizard/witch to move a bit.

Cuz right now nothing will actually happen. So, [INAUDIBLE] I still have my testing, get rid of that, adding a script. And this script we will call Movement Controller. Okay, so now with Visual Studio up and running, we have our Movement Controller script. We’ll zoom in a bit, so this is fairly easy to read. So what we want to do is get an instance of MeshAgent, of our NavMeshAgent, so we’ll call that navigationMesh. And then when this first starts, so remember, this is gonna run one time. We just wanna get that component. So if I do navigationMesh = getComponents, Navigation [INAUDIBLE] I am just having trouble typing today. Okay, so basically what we’re doing is we’re defining a variable to hold our NavMeshAgent, and then we’re just gonna get that component. And so this works as long as this component or script lives on the same game object as this. Otherwise, we’d have to traverse a hierarchy or do something different basically to obtain it. But because I know this is going to live on the same thing, that’s fine to do.

So inside of our update, which, again, is a loop that’ll occur every frame, we’re gonna check to see if the mouse button is down. And we’re gonna look for mouse button 0, it’s gonna be the first mouse button. Then we’re gonna do a raycast. And you’re probably, if you aren’t familiar, a raycast, I will explain it shortly. But basically, what we’re gonna do, well, I’ll explain it now. So raycasting is basically a physics call. And what it’ll do is it’ll draw a line from one point to another point. And then it’ll tell you whether or not it hit anything. And if it did hit something, what it hit, as long as that object has what’s known as a collider on it. Which we haven’t talked about in this series, but I’ll have some point of material to reading about colliders, and eventually, we will start to dive and explain it to them a lot more. Okay, so basically what I’m doing is I’m saying hey, if our raycast and then I have to define where it’s going, but if the raycast, so we’re gonna start from our camera, camera, our main camera.

And then we’re gonna do screen point, ScreenPointToRay. We’re basically gonna take the mouse position as our starting, and then we’re gonna write it out to our hit variable. We’ll just do our distance, so we’re gonna do a distance of 100. And then we’ll probably need to make. This actually should take a vector. Sorry I’m all over the place here, vector3, a new position. So we’re gonna pass in where it hit to our method, and then we’ll do something with that. So we’ll do hit.point. And you can see the impact in world space where it hit a collider, and the collider it’s gonna hit is over ground. So, saving that and then we will do, so you can work with a NavigationMesh, so we’ll work with our agent. And we’re actually gonna set the destination, To be the position in which we clicked. All right, so again, we got our NavMeshAgent, we’re gonna check to see whether or not the left mouse button, the first mouse button is down. We’ll do a raycast, sorry, I should highlight this, we’ll do a raycast.

If we hit something, which we likely will if they’re clicking on the ground, we’ll pass where we hit the coordinates, which is vector3, so XYZ positioning of where we hit over to our NavigationMesh, which will automatically force or have our character move. So coming back over here, drop movement controller over to our guy. In theory, it should work. She moves very, very slowly, but she moves. So if I do, let’s bump it up to 10, That’s better. So you can kind of see that you can mess with these settings. So that’s fantastic. If I undo play, you’ll see that actually my speed reverted back from 10 to 3.5, which was what it was originally. So this doesn’t apply to everything, but the majority of stuff when you’re editing the properties, and you edit it while it’s running, when you come back out, it will not keep those properties. An exception to that rule, I think, is materials if I remember correctly, that was the one where if you did edit it, it would actually keep it.

So if I change this to 10, I’m actually gonna show you too, so before I wrap this video up, because I think we’re actually gonna stop here and then do the rest in another video. One thing that’s super helpful when working with this is you can actually mess with, no, it’s not the editor settings, excuse me, it’s been awhile since I’ve done this, but it’s so insanely handy, maybe it’s just the preferences. So one of the things that you can do is change the colors. So you can actually alter the play mode tint. I usually go for blue, and that will help you remember that you’re in play mode. Cuz that is the second most common mistake that I have made in the past. Is forgetting I’m in play mode, altering some values, popping out, forgetting what I altered them to, and then just kinda rinsing and repeating. So setting that tint can be very, very handy. All right, so that is it for this part of the video. In the part two, we’re gonna take a look at NavMesh obstacles, and basically, and then we’ll take a look also at animation.

So very basic animations, making a sprite appear, and kind of pulse when they go to click. So I will see you guys next time. Thanks.

 

Read the video

In this episode we will add point and click style movement to our character using Unity’s Navigation System. More specifically, we will go over baking a navigation mesh, setting up the NavMeshAgent and writing code to make our character move to a position when the user clicks on the ground. This video will also introduce the concept of Raycasting. 

Resources


dotGame

dotGame is a blog dedicated to game development with a primary focus on .NET technology. The episodes will focus on tutorials, tips and tricks and interviews. All tutorial project files can be found on the .GAME GitHub repository. Have a topic you'd like to see covered? Great! You can request a topic by opening an issue on the GitHub repository or by emailing dotgame@microsoft.com. General feedback can also be directed to dotgame@microsoft.com.

    Comments to Unity Navigation – Part 1

    • hi

      RAhmat afghan January 21, 2017 12:31 pm Reply

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Loading...
    1Code.Blog - Your #1 Code Blog