In this lecture, we'll implement the methods in our fish class. As a reminder, here's the piece of the class diagram we're going to implement. Let's go write the code. The first method we'll implement is the fire method. But before we can do that, we should think about what that actually means. How should we actually fire the fish? There's lots of different ways we could do this. But since I'm the person who's running this video, I've decided that we're going to click on the fish to make it go. To shoot the fish will click on the fish to make it go. Based on our previous work with the Unity game engine, we can imagine that we're going to add a force to a rigid body 2D to get the fish moving. But the question is, how do we know when to do that? It will help us to go look at the scripting reference. Hearing the mono behavior class, we already know that there are a number of onmouse something methods that are exposed by that class. We have OnMouseDown and OnMousEnter and so on, and it turns out that there's an OnMouseUp as button method that does exactly what we want. This is called when the mouse is released over the same Collider as it was pressed, so this is a click. We'll press the mouse button on the collider for the fish, and then we'll release the mouse button still on the collider for the fish, and that will shoot the fish. You may feel some discomfort that we said in our design that we were going to implement a fire method, but instead we're going to implement the OnMouseUp as button method. But this is a natural part of software development where we evolve our design as we discover more details, as we try to do our low-level implementation. We'll open up our fish script, and we'll finally get rid of the Start and update methods because we don't need those in our fifth script. I'll add a region for methods and we know that we're going to implement the OnMouseUp as button method and as you can see, Visual Studio auto completed that for us. We'll say fires the fish. Because when the OnMouseUp as button and method gets called by the engine we're going to fire the fish. As I mentioned, we already know we're going to add a force to a rigid body 2D to do that. But our fish GameObject doesn't have a rigid body 2D. Let's add one, now. We'll select the fish game object. I'll add a component. It's a physics 2D component, and it's the rigid body 2D. I'm going to leave all of the default values as they are. But I I'm going to move this up because as you know, I like my scripts at the very bottom. Okay, so now we have a Rigid body 2D component attached to our fish. The first thing I'll do is get a reference to the Rigid body 2D component and I'll accept all the suggestions except I like to call this rb2d instead, and then we're going to add a force. Rb2D and we're going to provide both a force vector and add force mode 2D. For my force vector, we should think about the x and y component of the force vector, and although it would be interesting to be able to aim our first with the mouse before we shoot it or something, we're going to just shoot our fish straight up, and that means that the x-component is 0 and the y component needs to be non-zero, and in fact, it needs to be positive to make the fish go up. This is a little bit of trial and error. We'll pick a number and if the fish isn't moving fast enough, we could change that number. We'd probably wait to do that final tuning of the force vector until we've actually integrated the fish into a game. But for now, I'll pick 20, in for my force modes 2D, I'll pick impulse, as we've been doing, adding the semicolon as well. We'll build over here and we come back to the editor and we play our game, and we click on the fish and nothing happens, and we're sad. But if we think back to what the documentation said, it said that the OnMouseUpAsButton method gets called when we've pressed down the mouse button and released the mouse button on the collider. We don't have a collider yet for our fish, so let's add another component. I can just use Box Collider here. Again, I'll make it so my script is at the very bottom. Now let's try running our game again. Now when I click on the fish, you'll see he goes straight up. That's exactly what we wanted. In case you didn't see that as well as you wanted to. I'll move the fish down a little bit and I'll run the game again and click on my fish again. As you can see, the fish moved up. We've implemented the shooting the fish functionality that we had in our design even though we implemented it using the OnMouseUpAsButton method. The other method that we said we were going to implement was the OnCollisonEnter2D method so that when the fish collides with something, we destroy the fish. Here in our methods region, I'll say void OnCollisonEnter2D. I'll just pick that by tabbing it and it fills in the details for me. I'll add a documentation comment that says Destroy fish on collision. In this parameter is some collision information about the collision that we've just detected. We don't want to just destroy the fish although we will want to do that. But we also want the fish to blow up. We need to instantiate a game object. It's the prefabExplosion we want to instantiate. But we want to make sure that we instantiate the explosion at the location of the fish, not just at the default location of the prefabExplosion. But we know how to get at the current location or position of the fish, that's transform.position. Finally, we want to rotate the explosion even though that won't matter for an explosion. I can build define. Back in the editor, I have nothing for the fish to run into at this point, but I can solve that problem. I'll right-click the Fish and I'll say "Duplicate". This Fish(1), I'll put at 0. When I play the game, I'll shoot this fish, and when it collides with this fish, both fish will detect the collision and both fish will blow up. Let's go ahead and test that. Just as I said, it works the way I said it would work. We've implemented the behavior for both of the methods that we had in our original design for the fish class. I want to point out that this is substantively different from developing console app classes. When we're developing our console app classes, we're just building standalone classes. But when we're developing our scripts for use in the Unity game engine, we have to pay attention to how our C# code interacts with functionality from the game engine. As we saw, the OnMouseUpAsButton method, we had to make sure we had a collider attached to the fish so that method got called. We needed to make sure we had a rigid body attached to the fish so that we could add a force to it. We have to pay much more attention when we're developing Unity classes to the interactions between the C# code we're writing and the functionality that comes in the Unity game engine. To recap, in this lecture we implemented the methods in our fish class. As you saw, we decided not to implement the fire method we included in our original design. Instead, we decided to use the built-in mono behavior MouseUpAsButton method. That's a perfectly natural part of doing development. When we start dealing with the implementation details, it's perfectly reasonable for us to evolve our design as we discover more about our solution to the problem. This doesn't mean we screwed up the design or anything, this is just a natural part of software development. Of course, we also implemented our OnCollisonEnter2D method. Now we've completely implemented our fish. In the next lecture, we'll actually include our fish in a Unity game.