How to Aim a weapon at Mouse in Unity 2D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we will make the weapon of our character point towards the cursor of our mouse hi i'm peter and welcome to sunny valley studio tutorials here is my starter project where i have a character that can move and it looks constantly at the mouse pointer and i want to add to it a weapon that will also follow the mouse pointer for our system to work we need to do three things first we need to figure out a way to rotate our weapon around our player next we need a script that will ensure that our weapon follows the mouse pointer third step is to adjust the look of the weapon so it looks good when we rotate it towards our mouse pointer okay let's get started so first of all we need to create a child object of our player game object so select the player right click create an empty game object as a child of my player and i will call it weapon current the idea is that the weapon parent if we select the move tool will be the object that rotates but it will not be the weapon itself the weapon will be the child of our weapon parent just right click on this weapon parent create an empty and let's call it weapon okay and we need to add to it a sprite renders let's type right render okay and i'll select the sprite that represents my weapon so i'm going to select this first one and here is my weapon so i need to first select the move tool and move it a bit on the x axis it all depends on your surprise but basically i want to place it somewhere here so now what we can do is select our the weapon parrot and if we select the z rotation and try to rotate it you can see that it rotates around the player head so that's perfect but we need to play around with how it looks so on the weapon we can check the order in layer and if we set it to something higher than the order in layer of my player sprite is 10 so if i set my weapon to be on the order in layer 11 it will always be rendered on top of my player sprite we're going to tackle the problems with this in this third step for now this is enough now it is time to create a script that will actually make our weapon parent follow the most pointer so make sure that you select your weapon parent and set in the scene view to see the pivot of it and you can see that this red axis is pointing towards where the mouse pointer should be so basically what we want to do is we want to rotate our weapon in such way that our red axis so the x-axis is pointing towards the mouse pointer and that's what we need to code in our script so let me set it back to zero and i'm going to right click in the scripts folder of mine create a csharp script i'm going to call it weapon parent and let's open it up great i'm going to delete the starting update and i'm going to create a property here so let's type rob if you are using a visual studio code or visual studio click tab and you can create this prop it will be a vector too and we are going to call this tab to move between those fields i'm going to call it pointer position and we are going to have a getter and a setter because we want to set it from a different script this way we can reuse this weapon from the perspective of our enemy or our player because both of those will not use the mouse the enemy will use some ai script next all we need to do is create update method and in this method what we want to do is ensure that our red x is so transformed dot right so this is the red axis of this transform is equal to and we are going to take this pointer position minus and we need to cast the position of our transform to vector 2 transform dot position and we are going to normalize get the normalized output of this so that we can set it to be the transform dot right direction and this way we will always point towards the mouse position and basically this is it now of course we can implement some attack using this weapon and we are going to do it in a future video but for now let's save the script let's go back to unity great i'll assign this weapon parent script on my weapon parent game object and now i need to set its pointer position now i'm getting an input from the new input system so you would have this sort of a window where you have all the inputs set up if you are not sure how to use this i have a separate video about this the link will be in the description basically what i do is i get this input in my player script i'm going to open my player script go right and here i'm getting in the update the pointer input using get pointer input so to get the pointer input i'm calling the new input system to get a vector too so the position of my mouse and i need to convert it using this camera main screen to world point from the screen position to the world position now be sure that your camera has the main camera tag on it otherwise this will not work if you have your mouse positioned now we need to get a reference to our private weapon parent and we are going to call it weapon parent okay and i'm going to get it in the awake so weapon parent equals get component i'm going to use get component in children i'm going to use look for the weapon parent component and this is how i'm going to get the reference to it and now all i need to do is in the update where i get the pointer input i'm going to call my weapon current dot pointer position equals the pointer input so i'm going to assign the pointer position in every update so my weapon will react to where my mouse pointer is let's save the script let's go back to unity okay if you press play you should see something similar so our weapon is constantly following our mouse if i click ctrl shift p to pause my game and i take a look at my weapon and i select the move tool you can see that the red axis is indeed pointing where my pointer was at this time so basically this is it but now our weapon looks a bit strange when we move by our pointer to the left from our character's position let's do something about this and also we may want to hide this weapon behind the players or the character's head just to make it look a bit nicer let me stop this if you are enjoying this tutorial please subscribe to the channel leave a like leave a comment it would help me a lot thanks and let's go back to our weapon script okay so currently we are just rotating our weapon what we want to do first is calculate vector 2 direction and we are going to calculate a direction based on the pointer position minus the transform dot position and we are going to again normalize this of course i need to again cast it to vector 2 and we can just get this in front and assign the direction to the transform and now if we can check if our direction dot x value so the value between the pointer and transform if on x we have value less than zero all we need to do is flip our sprite so just to show you let me go back to unity okay i'm going to use ctrl shift p to pause my game while the sword is pointing to the left and if i select my weapon and if i change the y value to be negative you can see that the sword is now pointing in the correct direction when we point to the left but the other way around it doesn't again work so all we need to do is to ensure that we change the value of our scale of the transform to be negative so we may want to type at the top vector 2 scale equals transform dot local scale now we are going to change this scale dot y will be for me it is minus one but if you have scaled up your object you may want to save this load value here in the weapon parent and then assign it with the negative number the negative value we are going to add else if direction dot x is greater than zero only if we have the x greater than zero we're going to assign the scale y value to be positive value so for me it will be one okay so let's assign our transform local scale to be our scale and this should be it let's save this script let's test it okay and we should see now that our swords flips when we move our mouse pointer to the left from the character that looks pretty good but now we still have this issue where the sword is always rendered on top of our character we may want to do this when the sword is at the bottom but when the sword is at the top we may want to render it behind our character's head now let me pause the game actually let me stop the game and stop maximizing this and if i now press play we will be able to see in the transform the rotation on the z axis so basically when we hold the weapon up the z value of the rotation is zero and this is where we would like to start hiding the sword behind our player's head and if we move our mouse to the left basically this value goes up 280 when it turns to -180 so basically up to 180 between 180 and 0 we would like the sword to be rendered behind our player's head and the best way to do this is again select our weapon and set the ordering layer to be 1 minus what is the layer of our sprite of the player so this is 10 so i would like to set it to be 9 and now it is constantly rendered behind our player and that's what we want to code in our script so let's stop this and let's go back to our script okay great so the top of our class let's create a public right render and we're going to create character render and weapon renderer we will assign both of those through the inspector let's slide it down and we are going to create an if statement if transform dot euler angles dot z if this is greater than zero and we're going to copy this statement because basically we need to check if this is now less than 180 degrees in this case we want to set our weapon render dot asserting order we're going to set it to be our character render dot sorting order -1 this will make our weapon render behind our player's head else of course we need to copy this and basically add it to it one so this will be rendered the weapon will be rendered in front of our players sprite and this should be it so let's save this and let's go back to unity great now let's select our weapon powered let's assign the weapon render and player sprite renderer and this should be it so now if you press play we should see the correct behavior when our weapon is just above the player's head we are going to render it behind it and when it is in front we are going to render it below it we are going to render it in front great so we have our basic weapon rendering and it follows our mouse and it looks pretty good if you want to learn how to implement shooting to this kind of game check out my make a juicy 2d shooter in unity of course the link will be in the description in the next tutorial i will show you how to perform an attack using your weapon and next we are going to tackle how to create simple enemies thanks a lot for watching see you in the next tutorial
Info
Channel: Sunny Valley Studio
Views: 31,017
Rating: undefined out of 5
Keywords: unity how to aim at mouse 2d, unity 2d aim weapon, unity aim weapon at mouse, unity sprite renderer sorting order, nuclear throne unity
Id: DPqc7qYDtzM
Channel Id: undefined
Length: 12min 40sec (760 seconds)
Published: Thu Jun 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.