2D Top Down Shooting POINT & CLICK Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
last time we covered how to make a square move around the screen in eight directions this time we're going to be doing something more interesting we'll make the square rotate to follow along with our mouse when we left click we'll now fire projectiles this will help us lay the foundation for any good tap down shooter so let's go ahead and get started back in our unity editor our scene is left with our player which has a player movement script we also have these four square objects which are these four walls right now they just have a sprite renderer but I'm gonna go ahead and add a box Collider 2d to each one of these and you'll notice if I disable the sprites we see our bounding walls so on our player game object we have this player movement script you could make a new script here to handle your shooting but I'm gonna go ahead and just expand this player movement script before we do that we want to keep things named properly so we don't get into trouble later on it's not required but it's a good practice to make sure your code is maintainable and keeping things named correctly is one way of doing that so I'm gonna click on our player movement script and press f2 and rename this to player controller and again this is the script we made in the previous video so here's the code currently but if you want to take a more in-depth look at it make sure you watch the previous video so we renamed the file but now we got to rename this class so original studio code I'm gonna click on player of movement and press ctrl f2 you can also right-click and change all occurrences and this will rename everywhere we're calling player movement so we'll now call this player controller so we're gonna need to add two variables here the first one will make more sense we need to add a private variable of vector 2 and we'll call this mouse position we want our player to face where our mouse is moving in the game and we obviously need the mass position to do that but in order for us to actually calculate our Mouse's position in unity we need a reference to the camera you can use camera main red I would never recommend that as it's just doing like a hard-coded search for your main camera and your scene so it's not ideal but what we can do is make a public variable so a public camera scene camera before we forget we'll select our player and under player controller we should now see scene camera and we'll drag in our main camera game object there so since we already have our process inputs method setup this is very easy for us we want to capture the mouse position so in order for us to do that what we can do is say mouse position is equal to scene camera that screen to world point what screen to world point does is when your mouse is in the game view it takes what the camera sees and then converts that into your world space position so what we're converting is the actual mouse input so we can say input dot Mouse position that unity provides us and that's it that will capture a vector2 of the mouse position for us so now we can actually utilize this in move and what I'm gonna do is actually rotate our player to follow the mouse so what we can do is say Victor to aim Direction is equal to the mouse position minus the rigid body position this is where it's gonna get a little mathy for people but we need to calculate the angle that we want to rotate by so what we can do is say float aim angle is equal to math F is the class dot atan2 and what we want to provide is for the y-value aim Direction y and for the x-value aim Direction dot x we then want to multiply this float by math F dot grad two degrees so radians to degrees minus 90 F so if you're not brushed up on your linear algebra you're gonna have a hard time with this but you don't really need to worry about it if you'd like to know feel free to research this once we have this angle calculated though we can simply just say rigidbody that rotation is equal to the aim angle and since move and process inputs are already being called an update and fix update we should be good to go so when we play the game we now notice our player is rotating towards our mouse position just great this is exactly what we want so how do we actually make our players shoot something I'm gonna drag in another square this is a 64 by 64 pixel square I'm just gonna shrink this down to maybe 0.25 in the X and point 5 and the Y and I'll change the color to anything it really doesn't matter it's a white square so we'll make it we'll make it yellow you can make what everyone and I encourage you to actually use sprites here I'm just being lazy and using squares so you could imagine that's like the blaster or whatever we want our player to actually be shooting from we can rename this one to weapon and we'll actually put this under the player we'll make it a child you can just drag that onto your player object and so now when we play we should notice that it follows our player with us I'm also going to drag one more square in and this is just to save us some time later and I'll do the same thing I'll shrink it to 0.25 in the X in point to 5 in the Y and this is gonna be what we're shooting from the gun so this will actually be a bullet so I'll call this bullet and I'll drag this bullet object down into our assets folder to make a prefab and delete it from the scene we now need to make a weapon script and a bullet script so first thing we'll do is create a c-sharp script and call it weapon ok we'll open this up so we need to make a couple variables we need to know what type of bullets the weapons gonna shoot so we can make a public gameobject reference to a bullet we need to have a position for our bullets to actually be instantiated from so we can make a public transform fire point we'll use this one instantiate a bullet so we can get rid of this start and update method all we want our weapon to people do is fire so we can make a public void fire method and in here what we want to do is simply say instantiate the bullet at the fire point dot position and the fire point dot rotation this utilizes our two variables we created but let's make sure we actually hook this up correctly in the editor so on our weapon we can drag in our bullet prefab into the bullet variable and we need to make a fire point so what I'm gonna do is right-click on our weapon and create empty and using the move tool I can just click on this arrow and drag it to the tip of the weapon and that's fine so we know I'll rename this to fire point and on our weapon I'll drag fire point into the variable for the weapon is to actually call the fire and while we could have handled the inputs in the update function of this weapon script I think it makes more sense to do it in the player controller where we're already processing inputs so in order to do that though we would need the reference so we want to make a public weapon weapon I know that's a little weird but we're not referencing the game object we're referencing the script itself that's actually attached to our weapon game object and then in our actual process inputs method what we want to say is if input that get mouse button down and if we open it up it requires an integer so zero is the left mouse button one would be the right mouse button we want to just use the left mouse button so we'll use zero and we just want to tell our weapon script to fire and so now as we move a mess around our player is rotating when we press down the left click button we are in sanh shooting bullets but obviously they are not moving or doing anything so the last thing we need to do is wire up our bullet script so let's actually create one we'll right-click on assets create c-sharp script and we'll call it bullet we'll drag it onto the bullet prefab option so in our bullet script in order for our bullet to move we want to add a rigidbody 2d so that's the first variable we're gonna make just a public rigidbody 2d I'll call my RB back in the editor on our bullet prefab will now add a rigidbody 2d to the object but make sure to set the gravity scale to zero because we're in a top-down game we don't want gravity what we could do is click and hold on our rigidbody 2d and drag it into the rigidbody variable cool something that's different from side scroller to top-down is we actually want our weapon to give the force of the bullet because we're storing our fire point in the weapon itself so we can make another variable to store this force so I'll say public float fire force and after we instantiate this bullet what we want to do is give some force to it so we need to pull the bullets rigidbody so after we instantiate this bullet we need to be able to apply a force to it so what we can say is game object projectile equals this instantiation so we have a reference to it and we can now say projectile dot get component of type rigidbody 2d and add force of fire point dot up which is a vector to direction multiplied by our fire force and then it also wants a argument of a force mode 2d mode so we could say force mode 2d which is an enumeration that impulse we can then set this fire force to something like 20 and now once we play our game we can press down the left Mouse and shoot some bullets which is great we're so close now to finishing you'll obviously notice the boats are going through the walls so let's stop that first this is very simple on our bullet prefab we have a rigidbody but we don't have a Collider so since my bullets are square I'm just gonna use a box Collider but use whatever shape works for you you use a circle Collider to the year what are the other ones it doesn't really matter so here's my box Collider if I hit play now to notice that they bounced off the walls which is better but still not ideal we won't árboles to disappear when they hit into an object I'm gonna click on one of the walls and shift-click to select all four of my walls and go to tag in the inspector and add tag we'll create a new one and I'll just call this wall and then I'll select all the walls and give them the wall tag for example I'll just drag in another square make it red call it enemy and I'll make another tag called enemy we'll assign it to this red square going back to our Collider 2d there is an is trigger boolean field and so we'll just make sure that's checked to true this lets us bypass collision but we're gonna have be handling it anyway in a second and script so with trigger set to true we'll open up our bullet script and we'll get rid of start an update and instead will say void ontriggerenter 2d and we'll notice we get provided this other Collider which would be the thing we're colliding against so I'm gonna show you one way of handling multiple different types of objects we want to react against I'll just be using a switch statement and I've showed this in some of my other videos but we could say switch other dot game object that tag so we're switching on the tag and I'm gonna be using some magic strings but what I recommend is making like a static utility class that just holds like constants and use those instead I've shown that in another video as well but what we can say is case and then in strings wall which was the tag we just made then we want to destroy game object which is the bullet and then we want to break we could say case enemy I don't have my enemy configured but if you had let's say an enemy health script or something like that you could say other dot game object dad get component my enemy you know script so this enemy script could be like enemy health or enemy controller or you know what however you have your project setup and then might take damage something like this and you can actually have on your bullet or your weapon passing a damage value and then provide that here but again I don't have this set up so you can have a whole bunch of these different cases of how you want to handle it and one thing I'd like to point out will also destroy it for the enemy as well one thing I do want to point out we're in this case you're destroying this object and you're instantiating new ones every time I made a video on object pooling which you don't have to worry about right now but it is way more performant implementing something like an object Pooler then constantly creating and destroying game objects in this case you're just making new references in memory whereas with object pooling you'll create like 20 bullets at the start of the scene and then you just are recycling basically the same game objects and activating them and deactivating them it's just more efficient and if you're shooting a lot of bullets it makes sense to implement something like that but that's it you can now shoot and the game objects are being destroyed with the two different types we have the enemy and the wall just make sure your enemy has a Collider 2d on it that pretty much wraps up our shooting some last-minute things to show off one thing you can do is make a particle effect that you can use as an impact effect and then reference a game object and instead of destroying you could make this impact method where you instantiate this impact effect at the bullet position and then destroy your bullet and so in your bullet you could pass it in a particle effect prefab and then when your bullets collide you'll notice they set off a particle effect you can also play a sound in here which I show in a SAN tutorial in your weapon script in your fire method you could also play a sound here as well but again I have a whole other tutorial on how to do that but with that said I hope this tutorial helped you guys out you should now be at a point where you can really customize how you want the shooting to be for your game I would challenge you guys to look into adding Oh into your game so you could only shoot when you have certain amount of ammo and maybe different types of weapons you have the structure all set up for you you just need to now add more to it I believe in you but if this helped you out let me know leave a like if you have problems join our new discord and post your problems in there instead we have a channel for that and of course make sure you subscribe lots more tutorials on the way and other types of video content so do it [Music]
Info
Channel: BMo
Views: 7,939
Rating: undefined out of 5
Keywords: bmo, unity, tutorial, 2d, 3d, unity2d, unity3d, topdown, top, down, shooter, shooting, bullets, fire, projectile, top down shooter, top down shooting, 2d top down, weapon, PlayerController, c#, howto, beginner, learn, lesson, instructions, point, click, point and click, crosshair, mouse, unity tutorial, basics, unity basics, for beginner
Id: mgjWA2mxLfI
Channel Id: undefined
Length: 13min 54sec (834 seconds)
Published: Thu Jun 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.