How to Aim a weapon at Mouse in Unity 2D
In this video we will learn how to Aim a Melee Weapon at mouse pointer in Unity 2D. In the later video I will show you how to perform an attack and how to create enemies to fight with.
Make a Juicy 2d Shooter course:
courses.sunnyvalleystudio.com...
How to get input using the new input system:
• How to use NEW Input S...
Assets used:
analogstudios.itch.io/
𝗛𝗮𝘃𝗲 𝗮𝗻𝘆 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀? 𝗝𝗼𝗶𝗻 𝘁𝗵𝗲 𝗱𝗶𝘀𝗰𝗼𝗿𝗱!
/ discord
𝗣𝗟𝗘𝗔𝗦𝗘 support the channel:
/ sunnyvalleystudio
00:00 Introduction
00:22 What we need to do
00:44 Weapon rotation setup - Step 1
02:15 Weapon rotation script - Step 2
06:38 Weapon sorting order - Step 3
#unity2d #gamedev #nuclearthrone
Пікірлер: 79
I think the variable *distance* should really be called *direction* for clarity. Sorry about that :)
Very well made as always!
Good, thanks for tutorials!
Nice tutorial as always!
muuuuito foda, valeu meu mano
Awesome stuff. You are a legend Peter! Really appreciate what you have been doing for us beginner developers!
@SunnyValleyStudio
2 жыл бұрын
Hey! Thanks a lot! I'm really glad that you like my vids 🙂
Hello @SunnyValleyStudio. Can you show how you animated your character in the playlist about the new input system?
you are my hero
@SunnyValleyStudio
Жыл бұрын
Glad I could help!
Thank you for the tutorial , i was having issues at first trying to get the weapon to rotate on my mouse pointer and it was rotating around my player when i was walking instead but in the end i managed to get it working because of a comment by DragonTailSB
@SunnyValleyStudio
Жыл бұрын
Sorry to hear that. Glad that you were able to work things out!
Thank you so much for the tutorial, really helped a lot !
@SunnyValleyStudio
8 ай бұрын
Glad it helped!
can you make a tutorial on how to do this with a joystick using the new input system please :)
you are good 👍
@SunnyValleyStudio
Жыл бұрын
Thanks 😁
I have a question about the tutorial: I am trying to make my game have inputs for both keyboard/mouse, and a gamepad. The mouse rotates the weapon just fine, the problem comes with the input for the gamepad. How would you make it so the weapon would rotate with the Right Stick of a gamepad?
@SunnyValleyStudio
7 ай бұрын
So if you are designing the game for mobile / console I would just make the weapon point towards an "invisible cursor" that we would offset from the player positions using the direction in which your right stick is pointing in. So when you move your right stick what you get as an input is the direction vector (representing the direction in which your are pushing the stick). You could send it directly to your weapon as the direction in which our weapon should be rotated. I hope it makes sense.
Awosome.
for some reason, my sortingorder will keep counting up or down base on the rotation
The series is very nice and usefull, but you have made stuff before, then you add something only to remove it in the next video, then add it again... It is a bit difficult to follow the tutorials in the yt playlist order.
@SunnyValleyStudio
3 ай бұрын
Sorry about that. I was trying a different approach where i don't show everything but just the specific feature and assume that you know how to use this code. Thanks for the feedback
Hey, does anyone know how I could equip / un-equip my weapon simply? Thanks in advance for help.
@Ananzi372
Жыл бұрын
You can make weapon active when you press the input and make a boolean false or true when you press the input if bool is true or false before attack lines
It says that "The type or namespace name "WeaponParent" could not be found (are you missing a using directive or an assembly reference?)" Please help.
@SunnyValleyStudio
Жыл бұрын
It means that you don't have the script called "WeaponParent" in your scripts. Maybe check the name of the script and the class.
Awesome stuff. How does the Get Pointer Input method of the Player class change if I use a virtual camera?
@SunnyValleyStudio
Жыл бұрын
Hey! What do you mean? If you are using the Cinemachine camera it still references the Main scene Camera. In general you need to grab the camera that is currently acting as the "main" camera for it to work.
@artohaapanen7764
Жыл бұрын
@@SunnyValleyStudio ok
if you want you can also use these 4 lines that are much more simple: private void Update(){ Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; difference.Normalize(); float rotation_z = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rotation_z + offset); }
@dragontailsb1971
2 жыл бұрын
ps. if you want to add the special scale change add this: Vector2 scale = transform.localScale; if(Mathf.Abs(rotation_z) > 90) { scale.y = -1; }else if(Mathf.Abs(rotation_z) { scale.y = 1; } transform.localScale = scale;
@srkydev5727
2 жыл бұрын
@@dragontailsb1971 You guys are amazing! I didn't even need to watch the vid 🤣🤣
@dragontailsb1971
2 жыл бұрын
@@srkydev5727 thank you!
@vheath4717
Жыл бұрын
@@dragontailsb1971 Thank you guys!
@dragontailsb1971
Жыл бұрын
@@vheath4717 no problem
When I flip the player to the -z the weapon rotates and rotate incorrectly. How would I change this? Do I have to add a animation?
@SunnyValleyStudio
Жыл бұрын
Sorry about that. I think at 8:07 I showed that we are modifying the scale of the weapon but in the script we are indeed affecting the scale of the WEAPON TRANSFORM. Maybe because of this you have added WeaponParent script on the weapon hence your trouble with rotation?
@itsPersonal921
Жыл бұрын
@@SunnyValleyStudio Thank this got it working! :)
I dont know if anyone is seeing this 1 year later, but Im getting errors with AgentAnimations and AgentMover, both says they could not be found and im either missing a directive or an assembly reference
@SunnyValleyStudio
9 ай бұрын
Hey! Those scripts are custom scripts that my game uses to perform a weapon swing. The point of the tutorial was all about aiming the weapon. You can use it in your projects without my scripts. Still if you want to check how they look like here is the code github.com/SunnyValleyStudio/Unity-2D-Context-steering-AI/tree/main/Agent%20Scripts
Followed your tutorial, for some reason I'm getting a null reference error, any tips?
@SunnyValleyStudio
4 ай бұрын
Null just indicates that we haven't set something. Maybe its as simple as an empty field in the inspector? Or a missing GetComponent() in a script?
How do you control the mouse using the right joystick on a game controller?
@SunnyValleyStudio
3 ай бұрын
I think that in the new input system you can get the joystick input directly as a Vector2. This is the same as the input from your arrow keys. Just normalize it and pass it to our functions
Can you make this in unity bolt
How do you get the character to flip with the mouse?
@SunnyValleyStudio
Ай бұрын
You can use DOT product using the up direction and the direction from player to mouse position. Just flip the X (I think) axis of rotation (transform component) of the character gameobjects.
How to rotate sprite without distortion? My sprite is turning into a mess on rotating
@SunnyValleyStudio
2 жыл бұрын
Are you sure you have the sprite file in the Project section to "Filter Mode -> Point(no filter)" ? It might be that or search for Pixel Perfect camera but I doubt it is the issue here.
@Llamit4
Жыл бұрын
@@SunnyValleyStudio Same issue is happening to me, when i start rotating my sprite it gets all compressed. Also happens only while being a child of the player, when you make it a stand alone object it works fine
@Llamit4
Жыл бұрын
Nevermind fixed it
@drigozx
Жыл бұрын
@@Llamit4 might be a little late but how did you fix it?
when i change the scale the weaoong points the oposite way
@SunnyValleyStudio
Жыл бұрын
Sorry about that. I think I have messed up the explanation arount 8:07 since we want to change the scale of the WEAPON PARENT object not the weapon. Sorry about that!
how are you making the character face the mouse?
@SunnyValleyStudio
Жыл бұрын
You just calculate the direction from the player to mouse and if X is > 0 face RIGHT if
@itsPersonal921
Жыл бұрын
@@SunnyValleyStudio But how?
@itsPersonal921
Жыл бұрын
did you find a solution, becuase Im having the same problem
@angelajohnson5509
Жыл бұрын
@@itsPersonal921 Went I change the direction of my player on the scale, the weapon goes weird and doesn't follow my mouse any more. I goes to the opposite direction. How do I fix this?
@twisky3751
Жыл бұрын
@@itsPersonal921 yeah heres my script: float direction = Mathf.Sign(mouse.transform.position.x - transform.position.x); transform.localScale = new Vector3(direction, transform.localScale.y, transform.localScale.z); Basically what this does is gives you either a value of 1 or -1 based on if the mouse is to the right or left of the player, and then sets the scale to be that number. Unfortunately this means you can't have your weapon be parented to the player because it will flip that too and the rotation will look weird, so put this in your weapon script: transform.position = new Vector2(player.transform.position.x, player.transform.position.y); and it will act like it is parented to the player.
what happens you want to switch weapons
@SunnyValleyStudio
Жыл бұрын
Hey! To solve this it would be best to just pass the AimDirection to the weapon and it should process it. So a sword would be at 90 degree angle so that we can swing it while a gun would be aiming its barrel at this direction and an attack button would just trigger the shooting. It would require us to spli this system into more granular parts /objects. I hope it answers your question.
How to do this in 3d?
@SunnyValleyStudio
20 күн бұрын
I will be adding more 3d projects to the channel! In 3D its a bit more challenging because you have a quaternion based rotation due to 3rd dimension. It needs clamping so you don't do 360 rotation Up/down. I will try to post a video about it soon!
@roman_gra_26
15 күн бұрын
@@SunnyValleyStudio I managed to do it in 3D, but I would watch your video about it.