TRANSCRIPT
Welcome back everyone. In this lesson, we are gonna be continuing on with selecting our units. In the previous lesson, we set it up so that we can click on a unit to select it and then click off of it to deselect it. Now in this lesson, we are gonna be working on group selecting units, so we can then click and drag to select a wide range of units here. And as a matter of fact, let's actually start by duplicating this unit a few times. So I'm just gonna Control + D, move him over here, Control + D, move him over there, Control + D, move him over there. So we now have a group of units and since we have multiple units now, we need to select our player object and add these to our units list here. So I'm just gonna drag and drop them in this array here because all units that we want to control or are recognized as ours, need to be inside this playlist right here. So like before, we can click play, we can select units, as you can see here, I'm selecting these different units, but we can only select one at a time. So what we're gonna be doing now is setting up that box select so we can select, for example, all of these at the same time and then control them. Now to begin with this, what we are gonna be doing is setting up a UI to actually have this square which is going to be the selection rect box and what I'm going to do is go right click, I'm gonna go UI, Canvas, to create a new UI canvas here in the editor and as a child of that canvas, I'm gonna create a UI image. Now, to get a better view of this image, let's going into 2D mode here. Click the 2D button, press F to focus on the image, and there we go. If we look in the game view, we can see we have the image right here on screen. But we don't want it like this 'cause right now, this image is pretty much all white, so if we do hover it over on top of, and let's make it a bit bigger. If we do hover over a unit, we can't really see what we're looking at, so we need to make this slightly translucent. So for the color here, I'm gonna set this to about 255 divided by two, so about 128, that's what we're gonna have. So we can see the unit and we can still see what we're selecting. Let's rename this here to our SelectionBox. And what we also want to do then is change a few things here in the rect transform. Right now, the actual anchoring is set to the center of the screen. So zero, zero is in the center of the screen and if you look here at the position X and the position Y, these values will changed based on where I move this. If I move it to the left, you'll see that it goes into negative and if I move it to the right, you'll see that it goes into the positive. Same with the Y, vertical, it goes into the positive and downward, it goes into the negative. Now we don't want it this way because the way we're gonna be setting it up with our mouse cursor and all that, the way that screen position works, which is what our mouse cursor works in, it's different than this. Zero, zero isn't the center of the screen, it's actually the bottom left of the screen. So we need to change this so that zero, zero for this selection box here is actually in the bottom left corner of the screen and to do that, we can just change the anchoring. So let's click on the anchor box here and I'm gonna set this here to bottom left. So now, as you can see, zero, zero is down here in the bottom left, which is what we want, which is great. Okay, so I'm just gonna move this back here. I'm gonna deactivate it so it's not visible by default and now let's go inside of our unit selection script and I'm gonna start by adding in a new variable. This is going to be our public RectTransform object which is gonna be our selectionBox. And we also need a few other variables. First of all, we need the start position for our click. So when we initially click down on the screen, we need to figure out what that position is so we can then figure out how big, how wide we make the box. So I'm gonna have a private vector2 for out startPos and there we go, that's all we need for now. So down here in the update function, we are gonna be adding in two more if statements.
Free Tutorials:
- Unity:
- Phaser:
- Machine Learning:
- Web Dev:
- Android:
- Swift:
- VR:
0 Comments