How to Aim a weapon at Mouse in Unity 2D

  Рет қаралды 38,347

Sunny Valley Studio

Sunny Valley Studio

Күн бұрын

Пікірлер: 83
@SunnyValleyStudio
@SunnyValleyStudio 2 жыл бұрын
I think the variable *distance* should really be called *direction* for clarity. Sorry about that :)
@dragontailsb1971
@dragontailsb1971 2 жыл бұрын
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
@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) < 90) { scale.y = 1; } transform.localScale = scale;
@srkydev5727
@srkydev5727 2 жыл бұрын
@@dragontailsb1971 You guys are amazing! I didn't even need to watch the vid 🤣🤣
@dragontailsb1971
@dragontailsb1971 2 жыл бұрын
@@srkydev5727 thank you!
@vheath4717
@vheath4717 2 жыл бұрын
@@dragontailsb1971 Thank you guys!
@dragontailsb1971
@dragontailsb1971 2 жыл бұрын
@@vheath4717 no problem
@lonewanderer6333
@lonewanderer6333 2 жыл бұрын
Awesome stuff. You are a legend Peter! Really appreciate what you have been doing for us beginner developers!
@SunnyValleyStudio
@SunnyValleyStudio 2 жыл бұрын
Hey! Thanks a lot! I'm really glad that you like my vids 🙂
@durrium
@durrium 2 жыл бұрын
Very well made as always!
@Eaaaaaattmyasss
@Eaaaaaattmyasss Жыл бұрын
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
@SunnyValleyStudio Жыл бұрын
Sorry to hear that. Glad that you were able to work things out!
@oarl1330
@oarl1330 2 жыл бұрын
Hello @SunnyValleyStudio. Can you show how you animated your character in the playlist about the new input system?
@deniscooper09
@deniscooper09 2 жыл бұрын
Good, thanks for tutorials!
@JakeMakesGames
@JakeMakesGames Жыл бұрын
can you make a tutorial on how to do this with a joystick using the new input system please :)
@uwinhong
@uwinhong 28 күн бұрын
Thanks for your tutorial, but I found that when rotate, jaggedness occurs in pixel weapon. Is there a way to fix this🤔
@SunnyValleyStudio
@SunnyValleyStudio 25 күн бұрын
I think you could fix that by using docs.unity3d.com/Packages/com.unity.2d.pixel-perfect@1.0/manual/index.html
@yutsuneko
@yutsuneko 2 жыл бұрын
Nice tutorial as always!
@falier5795
@falier5795 Жыл бұрын
Thank you so much for the tutorial, really helped a lot !
@SunnyValleyStudio
@SunnyValleyStudio Жыл бұрын
Glad it helped!
@TiagoMaiaPereira
@TiagoMaiaPereira 11 ай бұрын
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
@SunnyValleyStudio 11 ай бұрын
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.
@whyno6807
@whyno6807 Жыл бұрын
you are my hero
@SunnyValleyStudio
@SunnyValleyStudio Жыл бұрын
Glad I could help!
@artohaapanen7764
@artohaapanen7764 Жыл бұрын
Awesome stuff. How does the Get Pointer Input method of the Player class change if I use a virtual camera?
@SunnyValleyStudio
@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
@artohaapanen7764 Жыл бұрын
@@SunnyValleyStudio ok
@Antdromeda
@Antdromeda Жыл бұрын
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
@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.
@mikebb7999
@mikebb7999 6 ай бұрын
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
@SunnyValleyStudio 6 ай бұрын
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
@Bruno-vn3qq
@Bruno-vn3qq Жыл бұрын
muuuuito foda, valeu meu mano
@minima_studios
@minima_studios Жыл бұрын
for some reason, my sortingorder will keep counting up or down base on the rotation
@neppallv
@neppallv 6 ай бұрын
How do you control the mouse using the right joystick on a game controller?
@SunnyValleyStudio
@SunnyValleyStudio 6 ай бұрын
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
@senordygus919
@senordygus919 7 ай бұрын
Followed your tutorial, for some reason I'm getting a null reference error, any tips?
@SunnyValleyStudio
@SunnyValleyStudio 7 ай бұрын
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?
@aimeeharper5130
@aimeeharper5130 Жыл бұрын
Hey, does anyone know how I could equip / un-equip my weapon simply? Thanks in advance for help.
@Ananzi372
@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
@Blue_Grego
@Blue_Grego 4 ай бұрын
How do you get the character to flip with the mouse?
@SunnyValleyStudio
@SunnyValleyStudio 4 ай бұрын
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.
@itsPersonal921
@itsPersonal921 Жыл бұрын
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
@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
@itsPersonal921 Жыл бұрын
@@SunnyValleyStudio Thank this got it working! :)
@rigbard4825
@rigbard4825 2 жыл бұрын
you are good 👍
@SunnyValleyStudio
@SunnyValleyStudio 2 жыл бұрын
Thanks 😁
@twisky3751
@twisky3751 Жыл бұрын
how are you making the character face the mouse?
@SunnyValleyStudio
@SunnyValleyStudio Жыл бұрын
You just calculate the direction from the player to mouse and if X is > 0 face RIGHT if
@itsPersonal921
@itsPersonal921 Жыл бұрын
@@SunnyValleyStudio But how?
@itsPersonal921
@itsPersonal921 Жыл бұрын
did you find a solution, becuase Im having the same problem
@angelajohnson5509
@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
@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.
@gamedevbaiyi936
@gamedevbaiyi936 2 жыл бұрын
Awosome.
@tarkerro
@tarkerro 2 жыл бұрын
How to rotate sprite without distortion? My sprite is turning into a mess on rotating
@SunnyValleyStudio
@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
@Llamit4 2 жыл бұрын
@@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
@Llamit4 2 жыл бұрын
Nevermind fixed it
@ind1ewave
@ind1ewave 2 жыл бұрын
@@Llamit4 might be a little late but how did you fix it?
@Blue_Grego
@Blue_Grego 2 ай бұрын
@@Llamit4 how?
@jackeygaming7454
@jackeygaming7454 2 жыл бұрын
Can you make this in unity bolt
@angelajohnson5509
@angelajohnson5509 Жыл бұрын
when i change the scale the weaoong points the oposite way
@SunnyValleyStudio
@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!
@Shergon_41k
@Shergon_41k 4 ай бұрын
How to do this in 3d?
@SunnyValleyStudio
@SunnyValleyStudio 3 ай бұрын
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!
@Shergon_41k
@Shergon_41k 3 ай бұрын
​@@SunnyValleyStudio I managed to do it in 3D, but I would watch your video about it.
@tredutore
@tredutore Жыл бұрын
what happens you want to switch weapons
@SunnyValleyStudio
@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.
@Lohimies
@Lohimies 2 ай бұрын
There is a weird flip of the weapon at 180 degrees that assigns the 180 to rotation.y. Fixed it with a snippet of code: if (transform.eulerAngles.y == 180 || transform.eulerAngles.y == -180) { transform.rotation = Quaternion.Euler(0,0,180); } Why is this happening? The solution I found on unity discussions forum explained that it has something to do with transform.right.
@SunnyValleyStudio
@SunnyValleyStudio 2 ай бұрын
Thanks for pointing that out and sharing the solution 👍
Create Melee Attack / Combat in Unity - P1 - Attack Animation
16:05
Sunny Valley Studio
Рет қаралды 52 М.
The Trick I Used to Make Combat Fun! | Devlog
8:12
Game Endeavor
Рет қаралды 1,7 МЛН
Will A Basketball Boat Hold My Weight?
00:30
MrBeast
Рет қаралды 142 МЛН
Каха и лужа  #непосредственнокаха
00:15
бабл ти гель для душа // Eva mash
01:00
EVA mash
Рет қаралды 7 МЛН
Creating SMART enemies from scratch! | Devlog
5:40
Challacade
Рет қаралды 350 М.
I Paid Fiverr Game Developers to Make the Same Game
10:25
BadGameDev
Рет қаралды 735 М.
I Built Skyblock In Minecraft Hardcore
23:40
Beppo
Рет қаралды 340 М.
Why Is It Bad That My Game Looks Good?
16:40
Deynum Studio
Рет қаралды 186 М.
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 724 М.
3 Hours vs. 3 Years of Blender
17:44
Isto Inc.
Рет қаралды 5 МЛН
Why Solo Developers Should Use Unreal
9:51
Thomas Brush
Рет қаралды 415 М.
When Optimisations Work, But for the Wrong Reasons
22:19
SimonDev
Рет қаралды 1 МЛН