Aim at Mouse in Unity 2D (Shoot Weapon, Unity Tutorial for Beginners)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to make a 2d character aim towards the mouse position this is a very basic video and essential for making a 2d top-down game let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you learn how to make your own games with enough tutorials made by a professional indie game developer so if you find the video helpful consider subscribing okay so here's what we want to make over here I have my basic character moving around and you can already see that the weapon is aiming straight at the mouse position so wherever I move the mouse or move my character you can see that it's looking at the mouse position perfectly now when I click on the mouse if there you go the weapon shoots perfectly onto the mouse position so here is something that is pretty new and essential for any sort of shooting game also you can see a bunch of effects that make the shooting feel great so we have an animation screen shake and a shoot flash then we have the bullet tracer sprite this was made in a previous video and finally also a bullet shell particles which was also made in the mesh particle system video so go check those out for more in-depth look into these effects alright so over here we have our goal let's get to it okay so here we are in our starting scene oh I have is a simple character just standing around with some basic movement controls as you can see he doesn't have any arms or hands so let's take care of that here in the editor let's first build the players hands and weapon so here is the player game object now it's not visible because this particular animation system only runs when the game is running but this is the game object so it's filmed what we're going to use to rotate towards a target position so we make a new child empty game object let's call this our aim and then inside let's make a sprite and I'm going to drag over here the pistol sprite okay so there is the sprite now here as you can see the pistol sprite itself is drawn looking to the right this is important because pointing to the right is a zero angle in unity as we won't see for now let's just test just like this and if there is the player with the weapon on top okay now let's make our script to handle it so we're going to create a new C sharp script call this our player aim weapon and we drag it straight into the player game object okay you know here since we are attaching this script to the player the first thing we need is to grab reference to our aim game object okay we have our aim reference now let's make our prep boy update and in here the first thing we need is to grab the mouse position so here we can use this nice function from the utilities which is always you can grab for free from unity code monkey comm here is the function in case you want to make it yourself in essence it just takes the world camera and as a screen to run a point on the input dot Mouse position so with that here we have our vector three for our mouse position now here we're working on a 2-d game so that means that rotation works differently than in 3d now in 3d you could simply do go into the aim transform and come look at and here we transcend the mouse position and this would pointy transform towards that position but if we test this and just like this we cannot see our sprite anymore but if we pause the game we can go into our hierarchy select the aim game object and there you go it doesn't need have some rotation and if we put our scene in 3d you can see that it is indeed pointing right forwards so this is not how we want to handle rotation in our 2d game in 2d we want the X and y to always be at zero and the only thing we're going to modify is these that just like that okay so let's go back into our code and here we're not going to use transform done look at instead let's get the Euler angles for our lock Direction so first we have our target position which is the mouse position with that we can calculate the direction so a vector three for the aim direction which won't be the mouse position minus our transform position and since this is a direction let's normalize this vector okay so now we have our aim Direction this is a vector with values on the x and y pointing towards emails position now we need to convert this into a Euler angle so to do that we use some map okay so we use this function we pass in the Y and the X this returns the angle in radians then we multiply it by this in order to convert our radians back into degrees and now over here we have our oiler angle now we just need to apply this to our aim transform so we go into the aim transform and modify the Euler angles and we said he vector three and again here we are working in 2d so we pass in a zero on the X zero on the Y and now an angle on the EZ and just like that it should be working let's just add a debug log in order to see what the angle says all right let's test and here we are and yep you can see our pistol is indeed pointing perfectly towards our mouse position and if we look at the console there it is you can see our angle and now you can see that when I put mouse on the right yep there you go that's the angle zero and over here we have a 90 and so on so you can see that default for unity the angle zero is pointing to the right so that is why we set up our aims right to be a piston pointing to the right so whenever you draw a sprite that is meant to point in a certain direction always draw it pointing right okay so just like this we have our weapon aiming towards the mouse awesome now let's actually make it look a bit better instead of just being an empty weapon let's add some hands and offset it a little bit so back in the editor okay we have our aim game object this is what we're going to rotate on the set to point towards the mouse position so now the first thing we need is to ensure that our inner sprite our pistol sprite is actually correctly sorted so as you can see it's a bit higher so let's drag the sprite a bit like that so that it's right there on this axis and now let's also push it a bit forward and add some sprites for the hands okay so there it is offset a bit forward as you can see straight on the same axes and the pistol and we have two hands let's see okay so here we are and yep our pistol is now offset and aiming correctly so as you can see perfectly following now the hands are a bit lower so let's modify our actual aim transform so here the aim is currently on zero zero and let's just move it up a bit any of there it is now it's more at shoulder height so it looks much better right awesome so now with this working let's handle some basic shooting so back in our code let's first make a function to Himmel its so let's call it handle shooting and we can also put this into its own function okay so we have our two functions being called on our update first we aim then we test if we can shoot so in here let's do a input get mouse button down on the left mouse button so when we press on the on left mouse button when we do then let's actually shoot our weapon so for that let's add an animation so here in order to an admission we just add the animator component and then here I have some animations already set up so here I modify the game object to work with the animations that I prepared previously but as you can see everything some works it's only the same so we have our in-game object then inside we have an extra sprite for a nice muzzle flash as well as d2 hands here is the animator as you can see it's extremely simple we just have a idle and a shoot animation and we're going to go from idle to shoot whenever we find the shoot trigger so there it is a very basic animator so back in our code and let's first grab the animator okay so we have the animator that is on the aim transform game object and down here when we are testing for the mouse click we simply go in to the animator and we set the trigger so it's called shoot okay so that's it now we should be able to see the animation playing whenever we left click on the mouse button okay so here's our character is still pointing to the mouse position and when I click there you go we have a nice shoot animation so just like that nicely shooting alright awesome ok so here we have our basics working now let's set things up like I do in the normal aiming class that you've seen me use in my videos so all the extra effects will be separate from the aiming class and they're added through an event so back in here and let's make that event so first of all we need to be using system and then we can add a public event type event handler and let's call this on shoot you know here we want to pass in multiple values so let's make a custom event arts we're going to set it to passing the gun endpoint position as well as the shoot position okay so now we can make this event handler of type on shoot event arts all right that's our event now let's go to where we are shooting so right here here we're going to trigger our event so we do an invoke pass in the center the center is this and now we need to pass in the shoot event arts so for that we need to set the gun endpoint position so let's make that using an empty game object so back in our scene we have our aim now inside let's create an empty game object call this the gun endpoint position and now here we can visually place it right there so that's where our booth will be fired now we just need to know this name so back in here let's make another transform okay we have this and now here we can set that gun in point position to be this one dot position all right so that's that now we need the chute position and that's going to be our mouse position so whenever we do a left-click we're going to play the shooting animation and we're going to fire the event which is going to pass in both of these arguments so now with this we can easily add some effects without having to modify this class anymore so let's make a new script let's call this just testing and make an empty game object and call it also testing and drag the script onto it okay now here let's first add a field for our player so we have a certain wise feel for our player aim weapon and now I can go back into the editor and here we can set it so there's the field just write the point of reference onto it okay and now that we have this reference let's do a private void start and I'm start to go into the player aim weapon and we subscribe to the on shoot event so just like this you can already see our event and as you can see it contains a on shoot event Argus parameter so here let's add a bunch of effects so first of all let's add some very simple screen shake so in the utilities I have some very basic screen shake so we just shake the camera and now I also have a class to spawn a weapon tracer this was created in a previous video so go check it out so here we take a front position and a target position so we use exactly what we get from the event arc so we pass in the gun endpoint position and the shoot position and for another event just adding he should flash ok so let's test just like this ok so here we are and everything so pointing and when I shoot if there you go we have some very nice effects so there are here we have some screen shake we have our bullet tracer and we have a shoot flash all right so just like this summary sorry talk great now it's had some bull chance now in order to fire the shells we also need to know where they should come from so for that we can add a game object in order to locate it perfectly so for this let's put it right in there and I'll back in our script you just need to do the same thing we did for the gun end point in order to pass in more values whenever we fire our event we can simply add it in here so we just do this and then down here we do pretty much the same thing okay so you can see how simple it is to add more values whenever we fire our event so now we can go back into our testing class and in here we can use the shell particle system that we made in a previous video and here we spawn the shell so we go into our event and use the shell position and now we need direction so the Russian is going to be our aim Direction offset by 90 degrees so again we're going to do the same thing that we did on the mesh particle system video all right so just like that let's see and here we are in shoot any of their yeah we have our shown particles leaving exactly where they should so just like that and looks great again this is part of the mesh particle system so go watch that video to see how this effect works as you can see the particles stay there forever okay so here I've polished things up a bit so now the player is also animated and as you can see the pistol once it goes past that there you go it inverts so the player no longer holds the weapon upside down so here it is everything is working I can move I can shoot there you go some nice particles nice effects and everything is looking great and again the way we set up our things makes our code very nice and clean so all these particle effects are own completely decoupled from the player class so the player has no idea that all of these effects are being added on top so we could add or remove more effects and we wouldn't have to touch the player class at all so again here is our nice effect and here we have our simple aim shooting as always you can download the project files any zombies from unity code McKee comm subscribe to the channel for more unity tutorials post any questions you have in the comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 172,060
Rating: undefined out of 5
Keywords: unity 2d aiming, unity top down 2d, unity 2d shooter, unity top down, unity 2d mouse aiming, unity shooting, unity mouse aim, unity mouse, unity aiming, unity aim, unity shooter, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, programming, coding, c#, code, software development, learn to code, learn programming, unity3d
Id: fuGQFdhSPg4
Channel Id: undefined
Length: 14min 32sec (872 seconds)
Published: Fri Nov 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.