 How can you find missiles so that the enemy and the player can attack each other? That's exactly what we're going to do in this tutorial as we take a little bit of a breather from Trigonometry. But hey, for the super keen out there, I'm going to show you a solution and link to it that uses this hand function. But FYI, it's a little bit impractical to use in Scratch. But anyway, let's get those missiles firing in just a sec. Hey, what's up, crew? It's the Surfing Scratcher here, teacher-surfer programmer, and I help curious people just like you along on their learning journeys through video tutorials. Welcome back to our Scratch Trigonometry series. If you just jump into this series, check out the starter project down below in the description so you can follow along. In this tutorial, we're going to get some missiles firing from our rocket ship and our turret. I'm going to show you the basic way how to do this. And the trick part is going to be optional because it's kind of complicated, but it's going to be useful to explore the tan function. So let's get stuck into it. First up, we're just going to paint a sprite. Zoom in a little bit. Get the rectangle tool. And draw a rectangle from the center part. And mine's looking a little bit bigger. So I might even make it one. There we go. And significantly reduce the size. I'm going to change the color. And I'm going to make it this orange color here. I might just reduce the outline as well. There we go. Kind of looks like a health bar. Now position your missile so its left edge is on the center point. Just like that. Of course, let's rename it to missile. Back over here in the code editor, I'm going to get a when the space key is pressed. When the space key is pressed, we're going to go to the position of the rocket chip. We also want to point in the direction. Grab a sensing block. We're going to sense the direction of the rocket chip because we want to point in the same direction as the rocket chip. So now I'm going to press the space bar and boom, that's looking pretty nice. I'm also going to make sure that we go to the back layer. So our missile is behind the rocket chip. There we go. Then we're going to repeat until we're touching the edge of the stage. Then all we need to do is move some steps. We're going to move five steps. Now if we look over here at the rocket chip and I press the space bar, you'll see that our missile files from its tip. You'll see down here, it kind of gets stuck. So the next thing we want to do is hide it after it's finished repeating until it touches the edge of the stage. But if we hide it, then when we press the space bar again, it's already going to be hidden. So we better make sure that we show it. Let's just quickly test that out. Fire the missile from the rocket and when it gets to the end of stage, it disappeared. So that's cool if our missile goes to the edge of the stage, but we want our missile to do something when we collide with the turret. So that's the basic collision detection. If you want the trig solution, go check out the card in the top right hand corner right now to revisit when the rocket ship collides with the turret. It's the same logic. But for our solution, we're going to grab an if block. We're going to grab a touching block. And if we are touching the turret, we're going to get that if block and place it beneath the move block. So that way the missile is going to move until the edge of the stage and we're going to check each time if we're touching the turret. We need to do a couple of things. We need to hide the missile so it's not stuck when it touches the turret. And we also want to stop this script. We no longer need to keep it executing. If we didn't do this, we would keep looping until we touch the edge of the stage. The next thing we need to do is create a new event. We want the turret to lose a life. So lose turret life. But there's an issue because we've only got a health bar set up for our rocket ship. So let's duplicate this and create a health bar for our turret. Right click on the health bar, click duplicate. Let's rename it to turret health bar. Scroll down and there's only a couple of things that we need to change. We need to create a new variable. Make variable turret lives. Anywhere where you see a ship lives, you need to change it to turret lives. So we're going to set the turret lives to 10, switch the costume to turret lives, and we're going to change turret lives by negative one. Also, when I receive the event lose life, so when we receive lose turret life, the last two things we need to do is go to the position of the turret no longer the rocket ship and have the Y at negative 30 and we will add 60 to it if the Y position of the rocket ship is less than zero. Okay, let's test that out. So you can see there's the health bar of the turret and when the Y position of the rocket ship moves beneath zero, you can see the health bar flips to the top of the turret. So that's looking pretty good. How about when we fire a missile into the turret? Well, let's orient to the turret, fire a missile and boom, there's our little indicator going down. That's exactly what we're looking for. Currently only our rocket ship can fire missiles. We need our turret to fire missiles back at the rocket ship. So let's duplicate all that work that we've just done with that missile and rename it to turret missile. Okay, so let's just work our way through the sequence of instructions and tweak it. Starting at the top, we're not going to do this when the space key is pressed. We're going to do this when we receive a new event and that new event, it's kind of just going to be like a custom block and it's going to be called shoot turret missile. You'll see why I'm using an event shortly. Move those sequence of instructions across to shoot the new event and just do a bit of repositioning. We're going to go to the position of the turret. We want to point in the direction of the turret. We're going to repeat until we're touching the edge again. We're going to move five steps for now but we might tweak this. Then if we're touching not the turret, but the rocket ship, we're also going to hide and we're going to broadcast lose life instead of lose turret life. So let's test this out by clicking the set of instructions, boom, there we go. Now that we've created all the actions when we shoot the turret missile, we need to initiate this. So to do that, so when the green flag is clicked, we're going to forever wait around, I'd say three seconds and then we're going to broadcast an event and that event is going to shoot the turret missile. So let's click the green flag and move our rocket ship around the screen. You'll see that every three seconds our turret is going to shoot a missile. Now let's let our turret missile hit our rocket and something weird happens there. When our rocket is sort of away, it sends it straight to this position. I'm just going to hit the stop sign. So what's going on there? It's going to jump over to the rocket ship sprite and you'll see that in that event, when I receive lose life, previously we would call this event when our rocket ship would collide with the turret. So basically this event is just doing too much. It's doing the animation part and it's doing a repositioning and all we want is an animation part. So I'm just going to create a new custom block and call it spin ship because that's what it does. And in our definition of spin ship, we're just going to grab that whole spin animation and put it in there. So now when we lose a life, yeah, we want to spin the ship, but we're also doing some repositioning of the ship. And these instructions are the reason behind our bug. I'm going to create a new custom block and call it expel ship because the expel just moves it a distance away from the turret. I'm going to grab those instructions and put it under that function definition. Then if I scroll up in the detect collision custom block where we're broadcasting that lose life event, I can just expel the ship before that happens and that should solve our bug. Let's click the green flag, boom, and now our ship just spin. Notice that if I click the green flag, the missile just stays there stuck on the screen. So what I'm going to go ahead and do in the turret missile is just go to the position of the turret. And I'm also going to hide it so you can't see it. So now when we click the green flag, the missile goes to the position of the turret and it's hidden until it fires. And that's the basic implementation of how we're going to get missiles firing from our rocket ship and our turret. There's a couple of variations that you could make. Currently, we're only using the one missile sprite and we're shooting that missile every three seconds. If you wanted to have more missiles in your game, you could create clones instead of just using it. The way I do that is instead of broadcasting, shoot turret missile, you might just create a clone and then when you start as a clone, you would execute these instructions. Just be sure that you delete the clone after you create it. Here's a couple of other cool things that you could do. The longer the play is playing the game, you could increase the speed of the missile. You could make the time faster the longer the user has been playing at this game. The way that you could do that is using the timer block because the timer block is just a measure of time since you clicked the green flag. Grab a division block and you could say every 30 seconds or every 15 seconds, you want to do something. So currently if I press this, it comes to 24.5 essentially. If I want to get rid of all this business, I'm just going to round that up to the ceiling so it rounds it up to the next whole number or next integer. So I click this and there you go. Now we're up to 26. So I could use this block without mood steps. I could just get a multiplication operator block right in the code I just created and where it says five steps, that is what we're going to multiply by because when timer divided by 15 is equal to something less than one, it's going to round it to one. So that's cool. One times five is just five. Every 15 seconds, the speed is going to increase by five. That could be too high of a value. So you might want to start with something like three seconds or two seconds and you just place that in there. I challenge you to test it out and do something similar with the time that the missiles fire. Now on this move block, we've just used the built in scratch way but there is another way that you can do it and I've created a video if you're really curious about it and that video shows you how to use the tan function. There's a card in the top right hand corner right now to go check it out. We're nearing the end of our game, which is really cool. The next thing that we're going to be doing is creating an asteroid that will be orbiting around our turret and orbit is very similar to a circle. So we're going to explore how we can use trig functions to create the asteroids orbit. And this one, it's going to be pretty simple. So good on you for making it this far and I can't wait to see you in the next tutorial. Stick at it, we're nearly at the finish line. Hey, thanks for checking out this tutorial that shows you how to find missiles in scratch. We've just got two more videos left in our series. Next week will be the final trig video and the week after that we're going to tie up some loose ends in our game. But until then, I'm off to go find a wave. I'll catch you in the next one.