Join Dan Violet Sagmiller for an in-depth discussion in this video Navigation support, part of Build a Real-Time Strategy Game with Unity 5.
- [Voiceover] We want our units to move around the map. In order to accomplish that, we'll need a way to find out where on the map we've right clicked. We'll start by creating a new interaction called right click navigation. So, I'm going into the interactions folder, and I'll create a new C# Script called right click navigation. I'm not going to edit the right click navigation right now. We just need the class name, but we'll edit RTS manager and come back to this interaction later.
So, going into Script, let's open up the RTS manager. Alright, so the first thing we need to know about is the collider for the terrain itself. So, to get a hold of that, I'm going to say public, terrain collider, and I'll call it map collider. Next, I'm going to need a new function here. I need to find out where on the map we've just clicked, or there's a possibility we might not have clicked on the map. So, I'm going to say public, vector 3, question mark which means it's nullable because there's a chance we might not click on the map and if that's the case, then there is no valid point, we'll just return null.
And we'll call the method screen point to map position, and I'll pass in a vector 2. Now, we're going to be working with ray again, so let's create that ray. Var ray = camera.main.ScreenPointToRay.
and we'll pass in that point. Next, if we do actually collide with something we're going to need a ray cast hit to store the object. So let's create it. RaycastHit. We'll call the variable Hit. Now, we're going to check to see if we've clicked on something, if we've actually hit a point in the map. If we haven't, let's just return null right now. So, if not, mapCollider.Raycast, ray will pass in the ray and will pass out the hit, but we also need to give it a distance.
So, the max distance will be mathF.infinity. So, assuming that we haven't actually hit something here, we'll just return null. Assuming we've made it past that point though, then we do have a valid hit. So I'll say return hit.point. Next, we need to work with the starting functionality. Remember, it doesn't make sense for us to be able to control the enemy units. We can select them and find out information about them, but we can't direct them and guide them where to go.
So we'll make sure that no AI players actually get the navigation script. So after we create a game object, which we can't use yet, we have to create a reference for it. So that's create that reference now. Var go =. Now the instantiate function only returns an object, not a game object, so we'll need to cast it back into a game object. This will give us things like transform. Okay, so now that we have this game object, I'm going to check to see whether or not the player is AI.
So if not, p.isAI, so if the player is not AI, then we can add the right click navigation. I'll say, go.addComponent right click navigation. Now that we've done that, I'm going to return to Unity and in Unity, I'm going to select the manager, and in the manager we have a new field called map collider.
Let's give it the terrain. We'll find that under Map, and then Terrain. So I selected manager, and I'm dragging Terrain onto it, and now we've grabbed the terrain collider from the Terrain. This now gives us all the support architecture and code we need to find out where on the map we've right clicked.
- Adding player positions and start info
- Loading prebuilt levels
- Adding camera moves
- Adding a unit
- Supporting navigation
- Adding unit owners and player objects
- Importing and setting up a HUD map
- Adding unit info and credits
- Creating HUD actions
- Building a command base
- Adding artificial intelligence
- Adding battle actions