In this lecture we'll learn how to use the mouse position in our Unity games. Before we do that, you should take an in-video quiz telling me how you feel about using the mouse for input in our Unity games. Okay, let's go to Unity and see how we can use the mouse position to make a character follow the mouse around in the game. Here's the project that we'll start with for today's lecture. You'll see that I have four sprites over here on the left and those are four different characters from a game that P Games Studios built to teach fourth graders about Colorado history. We also have prefabs for all of those characters and in fact let me add one of those to the scene. So this will be the character that we have follow around as we move the mouse. As we know, to add behavior to a particular game object, one of the ways to do that is to write a custom script that we add as a component to that character. So I'm going to come over here and I'm going to create a new folder for scripts. And I'm going to add a script to it. And I'll call that new script character. And in fact before I forget, I will add the script to each of my prefabs. Now one way you might find yourself doing that is you might drag the script over to the character in the hierarchy window. If we select that game object we see that the script has been attached. But if we look at the character zero prefab, that prefab doesn't have the script attached. So as you modify instances of the prefab in the scene through the hierarchy window, you need to actually go up to the top and on the line that says prefab, click apply and that will actually apply any changes you made to the instance of the prefab to the prefab itself. So if we come over to character zero, we can see it now has a script attached. I'm going to do it the easier way for the other prefabs. I'm just going to drag and drop onto the prefab. And I'm doing this now because I'll forget if I don't do it now. Okay, so as you can see from character three we have the script attached and so on. Okay, so we know we've got the script that doesn't do anything at this point. Let's open it up and mono develop. So I will add a documentation comment that just says this is a character. We don't need the start method yet. Though looking forward I know we will need it. So I'm going to leave it there. Our work for this lecture will be in the update method. So what we want to do is on every frame, we want to find out where the mouse is and then we want to move our character to that same position. Your next question should be where do we find out where the mouse is? And we do that by reading the scripting reference documentation. So here's the documentation for the input class which we will use regularly when we're doing input in Unity. And so you'll build some familiarity with the variables or fields if you'd like to think of them that way or properties in the class, as well as the methods that we can use. This time we just need to find out how to get mouse position. And if we scroll down to the static variables, we see that there is a mouse position variable, that is the current mouse position in pixel coordinate. And we'll click that to find out that the mouse position is a vector three. So when we retrieve the mouse position which we'll do momentarily, we need to put it into a vector three. The other piece here is that the current mouse position is in pixel coordinates. And so we will have to take care of that in our script as well. So the first thing we'll do is we'll convert mouse position to world position. So remember we saw this in the Spawning Teddy's lecture that we have screen coordinates and we have world coordinates and they're not the same thing. And so if we have something in screen coordinates like the mouse position, we need to convert it to world coordinates if we need it in world coordinates. And in this particular case we do because we're going to move the character to that location. So the first thing we'll do we checked and we saw that the variable is a vector three. So I'll put it into a local variable called position. And because it was a static variable I put the class name, input dot and then the mouse position. So now I'm holding the mouse position in my local variable. As we discussed in the Spawning Teddy's lecture, if I want this conversion to put position onto the Z equals zero plane, then I need to set it here to negative camera dot mean, dot transform, dot position, dot Z. And now I can convert to world coordinates by using camera, dot mean dot screen to world point. And the point I want to convert is called position. So the way this works is I pass in my vector three, called position, that's currently in screen coordinates this method to convert it to world coordinates. And I put the resulting vector three back into position. So now position is the world coordinate location of mouse position. So now we can just move to mouse position. And I can do that by accessing game object. The game object this script is attached to, dot transform dot position. So that's the position of the game object the script is attached to and setting it to the position that I calculated here. And because we so commonly accessed the transform of the game object a script is attached to, the Unity folks have made it so we can just in fact do that and it will still go to the game object the script is attached to. And now when we run the game, the character follows the mouse around. The character can also leave the game world, but we will solve that problem in the next lecture. Before we recap this lecture, you should go do another in-video quiz telling me you understand how the input mouse position method works Okay, to recap, in today's lecture, we learned how we can use the mouse position to make our character follow our mouse around in our game. And we also reminded ourselves of the fact that screen coordinates are different from world coordinates, and we need to make sure we're working in the right coordinate system as we process the mouse position and use that to adjust our character's position in the game world.