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

  Рет қаралды 186,253

Code Monkey

Code Monkey

Күн бұрын

Пікірлер: 471
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Stay tuned for a complete Top-Down Shooter comign soon! 🎮 Play 7 Awesome Games (Action, Strategy, Management) and Help Support the Channel! ✅ Get the Game Bundle 67% off unitycodemonkey.com/gameBundle.php
@klarnorbert
@klarnorbert 4 жыл бұрын
Quick question: why not use raycasts? You can just skip the maths with that.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
@@klarnorbert I do use Raycasts to detect hits, not sure what you mean by "skip the maths", you need to first calculate the direction so you can use a raycast.
@klarnorbert
@klarnorbert 4 жыл бұрын
@@CodeMonkeyUnity I mean, using raycast to detect the mouse position in the game area, and then tranfsform.LookAt() that position.
@shalzy_
@shalzy_ 4 жыл бұрын
Bro can I just ctrlC+ctrlV the code without downloading the entire project
@Rayanthebest1
@Rayanthebest1 3 жыл бұрын
@@shalzy_ can you say where is the code
@MalikenGD
@MalikenGD 4 жыл бұрын
I wish you did more 3d work, but I gotta say, I absolutely love your commitment to the channel. It really shows.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Yeah I loved making the Hookshot so I'd definitely like to do more 3D stuff but there's still so much cool 2D stuff I haven't covered!
@kouni.i
@kouni.i 4 жыл бұрын
i started learning c# today and u are just explaining everything good and from nothing i still can understand something what are u doing. Good job dude!!! and thanks ofc
@broganking9830
@broganking9830 4 жыл бұрын
Man you're pumping out videos like there is no tomorrow!
@vijaychavan1625
@vijaychavan1625 3 жыл бұрын
its true... actually
@EggBlunt
@EggBlunt 3 жыл бұрын
I'm planning on remaking some Max Payne levels in a 2d side scrolling shooter, this helped out a lot thanks
@berkcan-upssslayer6521
@berkcan-upssslayer6521 4 жыл бұрын
The video is beautiful. I'm sure you appeal to many people from Turkey Turkish content we would love you to do.
@technoo4891
@technoo4891 4 жыл бұрын
Bro, your utils saved me with the get mouse position, I tried doing it "manually" and nothing was working correctly, thank you.
@charliegaming766
@charliegaming766 Жыл бұрын
if you are having a issue of not pointing your aim properly and aim away when you get closer to the player & you are using cinemachine victual camera to follow the player, just remove the camera controller script from the player if you are using one to follow the main camera behind the player. it will fix the issue. it fixed mine. hope this helps.
@nelyx5804
@nelyx5804 Жыл бұрын
Thx god i was searching for a long time to fix this, cinemachine are new to me so i was having difficulties to setup it, thx you man !
@charliegaming766
@charliegaming766 Жыл бұрын
@@nelyx5804 you are welcome 😊
@ClaytonWasTaken
@ClaytonWasTaken 4 жыл бұрын
If anyone is also using a script that updates the Camera's position in LateUpdate, you will notice that the aiming stops working correctly the further you move away from your player's starting position. A simple fix that I found was moving HandleAiming(); into a LateUpdate instead of Update. You should keep HandleShooting(); in a regular Update as it uses Input.
@TheFunnyWizard_
@TheFunnyWizard_ 2 жыл бұрын
Why does this fix it?? Mine did not work when I did it
@BillyMan
@BillyMan 4 жыл бұрын
Super helpful man, love it!
@nilss.6367
@nilss.6367 2 жыл бұрын
I have a Problem, because in my console when i start it says Shoot_Flash can not be found in script Testing (line 20) can sb help me please, thanks
@indraayy849
@indraayy849 4 жыл бұрын
You can just do: Vector3 aimDirection = mousePosition - transform.position aimTransform.right = aimDirection Less code and more performant
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Hmm interesting, I've never seen anyone modify the transform.right directly, not sure if that works the same or bypasses anything. Knowing how to do math with angles is still helpful though, it's used to fire the weapon shell.
@teebs2721
@teebs2721 4 жыл бұрын
Where would you put this code?
@sturmente
@sturmente 4 жыл бұрын
Thank you for the nice Tutorial. This is the first Tutorial what will work for my Game PS: Sorry for my Bad Englisch i am a 14 year old Kid from Germany :)
@ScribbleVicious
@ScribbleVicious 3 жыл бұрын
Great vid! I plan on trying this with a sword and sword trails for melee attacka
@edu2428
@edu2428 3 жыл бұрын
me too! Wish me luck lol
@hiddio
@hiddio 3 жыл бұрын
i’m making a guy who always has his hands in his pockets but can take out one of his arms to shoot laser beams PEW PEW PEW
@tobiasveith4838
@tobiasveith4838 2 ай бұрын
How can I make it so that I have a crosshair that rotates at a certain distance from the player to the mouse but does not rotate itself?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 ай бұрын
Since you know the direction vector from the player towards the mouse, you can just manually position it through code, instead of making it a child game object visual crosshairTransform.position = playerPosition + dirToMouse * distanceOffset;
@Bruzen
@Bruzen 4 жыл бұрын
Seems complicated you do a lot of fast motion I can’t understand
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
What couldn't you understand? You can pause the video to follow along at your speed.
@Bruzen
@Bruzen 4 жыл бұрын
Code Monkey I’ll try.
@chossy5098
@chossy5098 4 жыл бұрын
😆CodeMonkey the speed runner of programming. I don't like it either it puts me off using anything he puts out play at 0.5 pause go back ect.. but as he said you can pause the video. I normally get 1/4 in and think na & stop watch shame as it's great content.
@Bruzen
@Bruzen 4 жыл бұрын
Chossy yep
@Matt-zx5jj
@Matt-zx5jj 4 жыл бұрын
Great video, one question though: At 13:30 you add the code to flip the gun depending on the angle, but I dont see how you are getting the whole player to point in that direction as surely that is handled in an animator elsewhere? Are you just adding a trigger to your main player animator to change on pointer angle?
@ShadowScorp99
@ShadowScorp99 3 жыл бұрын
Same, I'm still trying to figure out how he did that.
@caiquemoa
@caiquemoa 3 жыл бұрын
@@ShadowScorp99 any of you guys discovered how he did?
@keni124
@keni124 4 жыл бұрын
I discovered this piece of code accidently but the code were much cleaner than to what I has coded
@alrexomandam1774
@alrexomandam1774 4 жыл бұрын
May I know how do you make the camera follow the cursor a little bit when you move the cursor into a specific place?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
The camera follows a position that is shifted towards the mouse position by half the distance cameraFollowPosition = playerPosition + mouseDirection * (mouseDistance * .5f);
@alrexomandam1774
@alrexomandam1774 4 жыл бұрын
@@CodeMonkeyUnity is there any video about that so that I can follow on along. I don't know where those codes came from. Sorry still a newbie
@alrexomandam1774
@alrexomandam1774 4 жыл бұрын
also how do make the character face the cursor, i followed everything on the video but mine is not working
@MontyEditz
@MontyEditz 3 жыл бұрын
how do u change from LWRP to URP also another error about Light 2d how to solve these plz help
@syndcake6807
@syndcake6807 4 жыл бұрын
Hi, i have a problem with the code, i copied your code exactly but, i keep getting an error message "NullReferenceException: Object reference not set to an instance of an object PlayerAimWeapon.Update () (at Assets/scripts/PlayerAimWeapon.cs:21)"
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
You have some variable set to null, check that line to see what it is. You probably forgot to drag a reference in the editor.
@syndcake6807
@syndcake6807 4 жыл бұрын
@@CodeMonkeyUnity That line is aimTransform.eulerAngles = new Vector3(0f, 0f, angle); and there aren't any references to drag
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
@@syndcake6807 In that line the only thing that can be null is aimTransform That should be set using Find(); so check the name, it has to be exact, it's case sensitive
@victoonix2802
@victoonix2802 2 жыл бұрын
@@CodeMonkeyUnity I checked but it still doesn't work. It's set to "Aim", which is absolutely right
@almaalma3464
@almaalma3464 Жыл бұрын
Player follow mouse: For the character I just calculate the Vector towards the mouse, convert that into a Euler Angle and split that into 8 then play the specific animation for that direction.
@Ventinky
@Ventinky 3 жыл бұрын
thanks code monkey, i subscribed, you helped my game :)
@WAMBHSE
@WAMBHSE 4 жыл бұрын
Could you please do a video on how to do this for a twin stick controller set up and also include how to have a cross hairs move with the direction of the controller input, Thank you very much! another great video.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
For that you really just need to replace the mouse position with a vector that you modify based on your stick input, everything else is the same, your character will point towards that vector position.
@twobits7310
@twobits7310 4 жыл бұрын
how to rotate character's weapon in moving direction, Shooting in direction of the character movement. I don't want to use mouse.
@JentlerYT
@JentlerYT 4 жыл бұрын
Damn it I wanted a platformer Aim at mouse Tutorial...
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
It works exactly the same, apply gravity to the character and all of a sudden you have a platformer.
@JentlerYT
@JentlerYT 4 жыл бұрын
Code Monkey oh thanks!
@speedrob
@speedrob 4 жыл бұрын
Hehe for once I'm ahead of the game here with this one, but still very cool and sought after.
@berogames3256
@berogames3256 4 жыл бұрын
Many thanks for your effort ,you are really make me happy by seeing your vedio
@jdaggrrr575
@jdaggrrr575 3 жыл бұрын
Could we get a tutorial on how to make this burst fire? also how would i make the weapon flip instead of being upside down when i point it left? im very new to coding (:
@dianpratama9507
@dianpratama9507 3 жыл бұрын
Thks for tutorial bro!!
@bemtheman1100
@bemtheman1100 Ай бұрын
this may be a bad question. I am just wondering if it is at all possible to have something like this switch between mouse and the right stick of a gamepad. Using the mouse the gun follows the cursor but with game bad the gun will go in the direction of the right stick. Is it possible to do something along the lines of moving between these two control schemes?
@CodeMonkeyUnity
@CodeMonkeyUnity Ай бұрын
Sure, you can listen to either mouse input or gamepad input and set the latest one as the active one. I did something like that in my Virtual Cursor video kzbin.info/www/bejne/oGO7qq2JdqlnjLc
@bemtheman1100
@bemtheman1100 Ай бұрын
@@CodeMonkeyUnity does the same aiming logic work for both mouse and right stick?
@JentlerYT
@JentlerYT 4 жыл бұрын
Code Monkey I need help the pistol doesn’t follow the player correctly. When the mouse is on top of the screen the pistol is facing right and when the mouse is at the bottom of the screen the pistol is facing left!
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Make sure the default orientation of your aim game object is pointing to the right. Or just add 90º through code.
@JentlerYT
@JentlerYT 4 жыл бұрын
@@CodeMonkeyUnity Pls how do I add 90 degrees through code? (I am a complete beginner as you can see)
@Drizxander
@Drizxander 4 жыл бұрын
I am having a problem. At 8:30 the sprite is showing at some distance ahead from the gun. It doesn't matter where the MuzzleFlash gameobject is placed in scene. It always shows at that same position. Please help me with this issue.
@raidoksgames3510
@raidoksgames3510 4 жыл бұрын
I have same problème you fixed that?
@Drizxander
@Drizxander 4 жыл бұрын
@@raidoksgames3510 No I couldn't fix it
@Cenji
@Cenji 3 жыл бұрын
How do I import the Weapon Aim file into unity?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
It's a .unitypackage so just drag and drop on your unity project window
@mr_kwkd
@mr_kwkd 4 жыл бұрын
why does mine stretch and stuff? Its exactly the same code in the video, and it does point, but it stretches and warps. Does anyone have a fix?
@mr_kwkd
@mr_kwkd 4 жыл бұрын
And also im getting the error (The script is called Pivot): UnassignedReferenceException: The variable aimTransform of Pivot has not been assigned. You probably need to assign the aimTransform variable of the Pivot script in the inspector. UnityEngine.Transform.set_rotation (UnityEngine.Quaternion value) (at :0) UnityEngine.Transform.set_eulerAngles (UnityEngine.Vector3 value) (at :0) Pivot.Update () (at Assets/Scripts/Pivot.cs:15) Im not doing any of the animation/gun stuff just the pointing at mouse
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Check the scale of the parent of the object you're rotating, it should be 1,1,1
@boogiewg5141
@boogiewg5141 2 жыл бұрын
When I use this, the "gun" is shaking/vibrating/jittering when the player is moving in any direction. I have tried changed execution orders and many combination of update, lateupdate and fixed update, but to no avail.
@lukastomasek8038
@lukastomasek8038 4 жыл бұрын
I was looking for this topic , thanks for creating this tutorial !
@viktorvanchov3093
@viktorvanchov3093 4 жыл бұрын
Hey, can you make a 2d building system? I tried finding other turtorials but they were either outdated or a turtorial about 3D building. I started a project where bulding is esential and it would really help if you made a turtorial about it.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Yeah city builder tutorials is something I definitely want to cover but not sure when.
@Makaplakka
@Makaplakka Жыл бұрын
how do you make your player change directions without the pistol sprite also moving in that direction?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Depends on how you are handling the player changing directions, sounds like maybe you are inverting the localScale.x? If so one solution is to not make the weapon a child of the player, make it a separate object and just make it follow the player position
@dinukachathurangashow8571
@dinukachathurangashow8571 Жыл бұрын
Dear code monkey: i did everything but gun is not pointing exactly where the mouse is at(little down) when I drag if farther away from the player. when I get closer it rotates all the way to the back, not pointing at the mouse position correctly. additionally i have used the Cinemachine noise camera holding effect. how can i fix this? i wish if u could reply the fixing code as well if possible.
@SleepingDaemon
@SleepingDaemon 4 жыл бұрын
Hello, CodeMonkey, how would I go about adding other projectile weapons with this setup?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
I covered several methods of handling projectiles here kzbin.info/www/bejne/hJzIZn2Bhc6Eitk
@-Hex-
@-Hex- 4 жыл бұрын
very helpful thx
@random_precision_software
@random_precision_software 2 жыл бұрын
Ive got a grappling hook my player can use with the mouse ...SOUND NO Problemo ... BUT how do I use the right stick of the xbox controller as a mouse position in Unity old Input system? I have no problem getting th axis from the controller just working out the pos from the stick pos.. Dont Fancy doinf a tutorial ? hehe ?
@redsinc.9587
@redsinc.9587 10 ай бұрын
Hey I've been having trouble with the whole animating process(im new to unity). In the video you jumped to having the whole animation process done so I'm kind of stumped. I have already premade pixel animations and have the frames for the gun idle and gun shooting but not sure how to actually do what you did in the video. Some help would be appreciated or if you know of a video i can watch to help me back on track because a lot of the videos do it weird and don't do it how you had things set up. thanks!
@tobi_jazz
@tobi_jazz 2 жыл бұрын
i copied your code excactly, but i got the same error message(NullReferenceException: Object reference not set to an instance of an object) over and over again. Why am I getting these but not you
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Use Debug.Log to find what is null unitycodemonkey.com/video.php?v=5irv30-bTJw
@kuo-kun7915
@kuo-kun7915 3 жыл бұрын
Can I do the katana zero mouse attack thingy?
@hellodarkness4572
@hellodarkness4572 2 жыл бұрын
I dont know why but my animation doesnt play even when i have it all hooked up and the trigger is working, and the transitions work as well. please help.
@tiubert6140
@tiubert6140 6 ай бұрын
In general, the tutorial is cool, but... I would like to find the code that makes the camera move with the cursor. Perhaps I missed some part of the video or video from the playlist
@tiubert6140
@tiubert6140 6 ай бұрын
This is my work (script on camera, in Update) transform.position = (player.position + Camera.main.ScreenToWorldPoint(Input.mousePosition)) / 2
@CodeMonkeyUnity
@CodeMonkeyUnity 6 ай бұрын
Yup that's exactly it, just get the mouse world position and position the camera wherever you want, maybe on the mouse position itself, maybe always on the player, or in the halfway point between the player and the mouse
@semimode
@semimode 4 жыл бұрын
Hey, Can you make a video about how to make that kind of character that you use in almost every video?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
I covered how my animation system works here: kzbin.info/www/bejne/Z2LGaqWNh9SngJo Its all based on dynamically modifying meshes in runtime
@semimode
@semimode 4 жыл бұрын
@@CodeMonkeyUnity I watched it and honestly didn't understand anything😂
@semimode
@semimode 4 жыл бұрын
@@CodeMonkeyUnity And im not really professional at making games. But i wanted to make that kind of animated character into one of mine training game.
@Magma-uw7yo
@Magma-uw7yo 4 жыл бұрын
my weapon squeeze when I turn, it's not the same scale. How can I fix that ?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Make sure you're only rotating on the Z axis
@xeb0lax
@xeb0lax 4 жыл бұрын
When i click left mouse click the animaton not happens or i cannot see the muzzleflash
@raidoksgames3510
@raidoksgames3510 4 жыл бұрын
You fixed ?
@curialbellic
@curialbellic Жыл бұрын
How do you make the gun stand behind the character when aiming upwards?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Depends on how your character/animations are set up, you need to play with SortingOrder or move the mesh on the Z
@frostbeard9542
@frostbeard9542 4 жыл бұрын
Every minute of this tutorial and ever other tutorial takes averagely an hour to research and understand.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
You just described the normal learning process. This is not a race so dont worry about how much time it takes you to learn something, just focus on actually learning it.
@frostbeard9542
@frostbeard9542 4 жыл бұрын
@@CodeMonkeyUnity Thanks for the advice, I just started game development and learned how long it takes to fully understand a concept. I also didn't mean anything critical in my comment of your tutorials. Infact you've helped me learn a lot about how to code and use Unity's software in your them.
@CorpSlay
@CorpSlay 4 жыл бұрын
i have 1 question, i downloaded the codemoney utility. unity put this utility under the assets.. is that the exact folder? or i have to move to another section for having all setup and ready for this tutorial and other you made?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
You just need to double click the .unitypackage file and it gets added to your assets folder. Now open up a new script and you should be able to add "using CodeMonkey.Utils" and browse all the utilities source code.
@CorpSlay
@CorpSlay 4 жыл бұрын
Ok thanks a lot. Love your videos man, they are very accurate. Keep up the good work
@kum914
@kum914 3 жыл бұрын
for me when i have my mouse pointer on the side the gun points upwards and when the mouse pointer is pointing upwards the gun is pointing sideways why is that?
@LaserGadgets
@LaserGadgets 5 ай бұрын
After finishing with a D in the Jr Programmer path I am more demotivated than ever. 2D game but you are still writing vector3. No idea why. When I add this to my prototype my weapon is ordering around me. Instantiate(puke.prefab bla puke.prefab.rotation). And they say UE5 is even harder. Is it in chinese or something?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 ай бұрын
Because at its core Unity is a 3D engine, you can use Vector2s if you want, they will just automatically be converted into Vector3. It sounds like you should focus on the absolute basics first, check out my free beginner courses kzbin.info/www/bejne/d56qhHh-bLaWesk kzbin.info/www/bejne/poPIg2mQbtd-Y9E
@LaserGadgets
@LaserGadgets 4 ай бұрын
@@CodeMonkeyUnity I did the Jr Programmer thing and starting from zero, I have to admit not all of it made it onto my bio-harddrive, but still. its like learning chinese and I can barely from sentences and even if I do it right, there comes another more complicated but "better" way to do it. Like no matter how much you learn, its never enough. Frustrating.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 ай бұрын
Programming is a relatively complex skill so yes what you are describing is the perfectly natural learning process. It takes time to learn, that's normal. I've been programming for 25 years and I'm still learning new things
@LaserGadgets
@LaserGadgets 4 ай бұрын
@@CodeMonkeyUnity Yeah I guess it is. I am just asking myself how you actually learned all of this because all I am doing is watching people doing what I might need for my first attempt, and copy what they do....which is actually the way I did not wanna walk.
@sanspseudofix8814
@sanspseudofix8814 4 ай бұрын
Hello thanks for this tuto But i want to know how to do that this netcode I tried but the weapon dont follow the player
@CodeMonkeyUnity
@CodeMonkeyUnity 4 ай бұрын
You just need to synchronize a vector3 for where each player is aiming
@MarkJrLuat
@MarkJrLuat 3 жыл бұрын
Hey, sorry I keep asking so many questions but I followed ur projectile tutorial but when I did the first way of shooting projectile the bullet just spawns at the bottom right of my character.
@JentlerYT
@JentlerYT 4 жыл бұрын
ok, sorry for all this, I downloaded the Utils, but there's still a little problem... the weapon doesn't follow the mouse correctly... for example, when my mouse is on the top of the screen, the weapon is facing right...
@JentlerYT
@JentlerYT 4 жыл бұрын
in conclussion, the controls are inverted.
@cratuss2661
@cratuss2661 4 жыл бұрын
@@JentlerYT seems like you have done a mistake at one part of the video. check your code. for me it worked today.
@JentlerYT
@JentlerYT 4 жыл бұрын
I have checked the code so many times! What Unity version do you use?
@cratuss2661
@cratuss2661 4 жыл бұрын
@@JentlerYT the newest one.
@theovellenoweth1992
@theovellenoweth1992 3 жыл бұрын
when I tried to shoot the muzzle flash was nowhere near the gun. how do I fix this?
@РичардТриальд
@РичардТриальд 3 жыл бұрын
Strangely, but when i swapped x and y in math formule, for me all was completed Also, how do you connected muzzle flash to animation, cause animator set in pistol, and in script i didn't found any stroke of code that connecting that
@namangupta4033
@namangupta4033 4 жыл бұрын
Hey, I don't know for some reason there's a time lag between clicking the mouse button and the playing of the animation. Very small, like a 0.3 sec time lag, but it's there. What should I do about it ?
@tarkozkan6439
@tarkozkan6439 4 жыл бұрын
you can change sensivity for Inputs in project settings it should do the trick
@sundarakrishnann8242
@sundarakrishnann8242 4 жыл бұрын
I guess you have to uncheck has exit time in animation transition...
@lspremul
@lspremul 4 жыл бұрын
Hi Code Monkey, I was curious how you would apply the rotation of the character over a period of time? Would something like this work? aimTransform.eulerAngles = new Vector3(0, 0, angle) * rotationSpeed * Time.deltaTime?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Sure, that would rotate it continuously. If you want to rotate smoothly and stop you can look into Mathf.Lerp();
@lspremul
@lspremul 4 жыл бұрын
@@CodeMonkeyUnity Oh ok cool thanks for the tip!
@biimyflipps
@biimyflipps 2 жыл бұрын
if i have a working "aim" system can i skip the part or do i need this one for that the shooting shal work
@Xionic420
@Xionic420 Жыл бұрын
the weapon is following the camera not the mouse please help
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Are you in a 3D scene? The code for getting the mouse position is different in 2D and 3D unitycodemonkey.com/video.php?v=0jTPKz3ga4w
@CrCRccccc
@CrCRccccc 3 жыл бұрын
i have a problem with an animator, so when i add an animator component my pistol stop looking at mouse, what should i do?
@motttti
@motttti 3 жыл бұрын
I can't get the gun to flip when it goes past 90 degrees, anything wrong with this? private void Update() { Vector3 mousePosition = UtilsClass.GetMouseWorldPosition(); Vector3 aimDirection = (mousePosition - transform.position).normalized; float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg; AimPivot.eulerAngles = new Vector3(0, 0, angle); Vector3 aimlocalScale = Vector3.one; if (angle > 90 || angle < -90) ; { aimlocalScale.y = -1f; } else { aimlocalScale.y = +1f; } AimPivot.localScale = aimlocalScale; } }
@yahyashafqat7352
@yahyashafqat7352 3 жыл бұрын
yo dude iam not able to add player to playeraimweapon for some reason. i realy followed every move but still i am not able to add it
@hawtrawd69
@hawtrawd69 11 ай бұрын
hey man, i have a question, it gives me an "Object reference not set to an instance of an object" error in unity, at the line with aimTransform.eulerAngles = new Vector3(0, 0, angle); . Do you happen to know why?
@CodeMonkeyUnity
@CodeMonkeyUnity 11 ай бұрын
That means you have a null reference, meaning a reference that hasn't been assigned to anything In that line the only thing that can be null is aimTransform, so you probably didn't name the child exactly teh same name as you used in Find(); Whenever you have that error always use Debug.Log to find out what is null unitycodemonkey.com/video.php?v=5irv30-bTJw
@robotplays346
@robotplays346 4 жыл бұрын
Hi, how would I make it so you can only aim a little bit
@N1thium
@N1thium 4 жыл бұрын
Hi! Nice tutorial mate! Can you make an special about mobile controllers? Thanks!
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Like 3rd party gamepads? Those are very difficult since they're all different, you're probably better off using an asset that already has a lot of them implemented like Rewired.
@N1thium
@N1thium 4 жыл бұрын
@@CodeMonkeyUnity Oh no no i mean the ones we make with scripts and UI! Hehehe
@AustraliaM69
@AustraliaM69 5 ай бұрын
how did u get it to actually rotate around the player? it rotates in spot at 5:25 but rotates all around the player after you add hands? what am i missing
@CodeMonkeyUnity
@CodeMonkeyUnity 5 ай бұрын
It always rotates around the pivot, so you move the child objects to some offset which will cause it to rotate around the pivot unitycodemonkey.com/video.php?v=NsUJDqEY8tE unitycodemonkey.com/video.php?v=qa5WBatZ830
@cbaker1514
@cbaker1514 3 жыл бұрын
DId i miss something? It says for beginners but when creating the project im not sure whether to chose 3d or 2d... also theres already a background behind the character. He doesnt mention how to get that background
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
The topic of the video is 2D aiming so that's what it covers, if you want to learn the general basics of Unity watch this one kzbin.info/www/bejne/e2ekZYqsqKmYgqc
@caiquemoa
@caiquemoa 3 жыл бұрын
the part that i was most interested you just skiped as "quick polishing" did you teach it in other video or are you saving for your course? if yes which one
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
What part? The Screenshake? That depends on how you set up the camera, in this case I had a normal camera so I just move it randomly for a few frames, if you have Cinemachine you add some noise. The shoot flash? It just spawns a sprite for the flash and destroys it after 1 or 2 frames. The bullet trace? I covered it here kzbin.info/www/bejne/oHXMcq2uoL6tpqs And the bullet particles I covered that here kzbin.info/www/bejne/iZzOqaqpjdx4nsU
@caiquemoa
@caiquemoa 3 жыл бұрын
@@CodeMonkeyUnity the 13:30 part the sprite turn toward the mouse, i'm trying to make this to but mine just turn when he is in the center of the map, if the player is far from the center and the mouse is >> because the mouse is >> from the center kkk
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
@@caiquemoa Sounds like you're using the direction from the center of the screen to test where the player should look when instead you should calculate the direction from the player to the mouse position. Do (mousePosition - playerPosition)
@caiquemoa
@caiquemoa 3 жыл бұрын
@@CodeMonkeyUnity Ty was exactly that
@hengsopheata9147
@hengsopheata9147 Жыл бұрын
Hi CodeMonkey I having problems with the aiming, I did everything in the tutorial and it keep saying "NullReferenceException: Object reference not set to an instance of an object" in the console on the 21nd line "aimTransform.eulerAngles = new Vector3(0, 0, angle);" Please help.
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
In that line the only thing that could be null is the aimTransform, use Debug.Log to verify unitycodemonkey.com/video.php?v=5irv30-bTJw Remember that Find(); is case sensitive, the name must match the child name exactly
@julius6521
@julius6521 3 жыл бұрын
When I use your script or countless others i that I have tried, the gun just rotates to some direction (random depending on position) and then stays like that. When I move the mouse it only rotates insanely little. I'm in the Unity 2019 version and I have no idea what I'm doing wrong. Help?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Verify that you're grabbing the Mouse World Position and doing all the math correctly. Add a bunch of Debug.Log in various places kzbin.info/www/bejne/a5rVp2ZmYseHgNk
@roglSimpson
@roglSimpson 4 жыл бұрын
Have you had any issues with weapon rotation stuttering or being jerky as the mouse pointer gets closer to the character (it works just fine when mouse pointer is further away)? If so, what was the secret sauce to resolve it? Thanks in advance!
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
One way you can solve it is smooth out the rotation, perhaps with a maximum speed. Although that would also make your game less responsive so it really depends on your design
@roglSimpson
@roglSimpson 4 жыл бұрын
Thanks for the reply! In the end, it turned out I was using the wrong pivot point. I tried calculating the rotation using the weapon's fire point game object, instead of using the arm shoulder as a pivot point and offsetting by Y coordinate of the fire point.
@barkmober
@barkmober 2 жыл бұрын
@@CodeMonkeyUnity Can you continue spiderman
@jeremiahbennett3004
@jeremiahbennett3004 3 жыл бұрын
Wondering here, is there a way to add knockback to these projectiles? I want to add a way to use movement with knockback from a projectile, any good ways I can do that?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
It depends on how you're controlling your characters, could be with rigidbody.AddForce or just moving the transform.position
@byDiAbLo21
@byDiAbLo21 3 жыл бұрын
Hi, I've got a problem, all seems good but when my mouse is close/over the aim transform my gun starts flickering how could I solve this?
@anonymoussloth6687
@anonymoussloth6687 3 жыл бұрын
Add a gun rotation dead zone which means set a specific threshold for the mousePosition - player's position so that the gun doesn't rotate when the mouse is really close to the player.
@Daniel-do2mh
@Daniel-do2mh 4 жыл бұрын
Hmm, do you know how could I translate it for mobile? (android) Like, with one finger you move the weapon and the other one you aim/shoot. (You control the weapons, not a character)
@tadeopuga9888
@tadeopuga9888 Жыл бұрын
Hi, I followed your tutorial step by step, but when I ran the project it just hit me with a lot of NullReferenceExceptions. How can I fix that?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Use Debug.Log to find what is null unitycodemonkey.com/video.php?v=5irv30-bTJw
@ASMRcu
@ASMRcu 4 жыл бұрын
Awesome......
@chaojack3113
@chaojack3113 4 жыл бұрын
hey There is no character or background after I opened the file
@curialbellic
@curialbellic Жыл бұрын
Do you have a tutorial on how to make the camera move smoothly to where you point the cursor?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
I made a camera system here unitycodemonkey.com/video.php?v=pJQndtJ2rk0 And mouse position here unitycodemonkey.com/video.php?v=0jTPKz3ga4w Just play with the smoothing to get the speed you want
@TheHumanSystem
@TheHumanSystem 2 жыл бұрын
Hello and thank you for the information. It seems to flip the direction of the shooting when I move the player using the left arrow key. The issue does not happen when I hit the up, down, or right arrow keys. In fact, if I hit the left arrow key, I can 'fix' it by hitting the right arrow key and moving slightly to the right. So, if I am facing right, it works. I can move the mouse all around and shoot correctly, but if I move the player to the left, even slightly, the mouse direction is now opposite. Any ideas? Why does the left arrow key flip the direction of the mouse detector? I have run debug.logs and nothing seems to make sense, so I am guessing there is a code issue.
@TheHumanSystem
@TheHumanSystem 2 жыл бұрын
I just realized, 2 minutes after commenting, that I have a flip on the Sprite when they look the other way...I'm guessing this is the issue. Now I have to decide, do I want to player to look the other way or aim the other way? :)
@Mikael._
@Mikael._ 4 жыл бұрын
Thanks for the awesome content. I'll make a small shooter, nothing more than some squares shooting at each other to begin... I have a simple question: In my C# scripts i can't make one class "find" other classes or scripts. Let's say i am editing the script "MainScript", and then i add a line like this "SecondScript.Functionxxxxx", when you do that, your code has the autofills for the functions or events. Like in this tutorial, the "subscribe to the event" part, mine wont be highlighted in blue and wont have those autofills. That happens with the "EventHandler", "Vector3", "Input", etc. The thing is that they work fine, but it seems my Visual Studio don't know them. (I instaled it after Unity from MS website and did the "Create Unity games" instalation). Sorry for the ̶s̶i̶m̶p̶l̶e̶ big question. :P
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Does your visual studio recognize Unity classes? Like Debug and Vector3? Try manually installing the Unity Tools for VS docs.microsoft.com/en-us/visualstudio/cross-platform/visual-studio-tools-for-unity?view=vs-2019
@Mikael._
@Mikael._ 4 жыл бұрын
​@@CodeMonkeyUnity Thanks for the help, it was not recognizing "Vector3, Debug, [SerializeField], other classes (project ones or even UtilsClass.xxxx), Vector2, etc..." It recognizes only core C#, like the functions, Int, bool, float, etc. I looked into the link you sent and it was the instalation i did before. (i instaled Unity w/ VS, then instaled it from MS when i started snake tutorial), so if my instalation was right (into work loads it was Unity Development)... there was something in between VS and unity I saw in a forum answer the guy said to enable VS Comunity in Unity preferences as the default C# tool, mine was auto. It works now, just select VS Comunity in Edit>Preferences>External tools And check the "Editor Attaching" box. Then open your scripts from your Scripts folder in unity and now VS recognize everything. Save them all into a single project and all should work fine. I'll leave it here in case anyone have the same problem...
@robotplays346
@robotplays346 4 жыл бұрын
Mikael yes, I have that problem, are you on PC? If you are, that is the PC version, not a bug
@twigmytwig9614
@twigmytwig9614 4 жыл бұрын
For some reason, my aim is always around 90 degrees to the left of my mouse? no matter what i change the starting rotation to. Any help?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Add a Debug.Log to see what your aimDir vector is set to, make sure its pointing correctly. And for the weapon make it point to the right since that is the 0 angle
@joaquincastillo3912
@joaquincastillo3912 4 жыл бұрын
Good video :D This works in a 2D side looking shooter game?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Sure, it just points towards the mouse so you could use it in a SideScroller/Platformer
@masonbezzer5940
@masonbezzer5940 3 жыл бұрын
How are you meant to do the animation? Is there another video i can watch which explains it?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
It's a game object so you animate just like you would any other game object
@asdf-nn7mb
@asdf-nn7mb 4 жыл бұрын
how to flip the player in the direction of the mouse?
@yeevon3852
@yeevon3852 4 жыл бұрын
mouse position is able to apply on phone ?
@SupertigerDev
@SupertigerDev 2 жыл бұрын
How can I find free to use player textures that are holding the gun that will work like this?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
There's tons of free sprites on the Unity Asset store or OpenGameArt
@apexhydra6662
@apexhydra6662 3 жыл бұрын
Does anyone know how to add the animations to the gun without it just playing right over the character
@bonesvalley7230
@bonesvalley7230 4 жыл бұрын
Everything's pink when I open the game scene, anybody can help, please?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Make sure you have the Lightweight RP Package installed
@nickvullings8728
@nickvullings8728 3 жыл бұрын
For some reason my shooting animation happens about 6 units to the right of where my gun is? any idea why this may be
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
You probably offset the child object, make sure that on the button next to the tools you have "Pivot" and not "Center"
@alexiosv7954
@alexiosv7954 3 жыл бұрын
I am having an issue activating my CodeMonkey account.... the confirmation link is not working....I need to download the project files of this project so i can learn a few things..
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
You didn't receive it? Check your spam folder
@alexiosv7954
@alexiosv7954 3 жыл бұрын
@@CodeMonkeyUnity Receiving it but the link is not working says too long to respond and doesn't activate anything
@goldenx-dragon2327
@goldenx-dragon2327 2 жыл бұрын
CodeMonkey i got a problem with this script. It says NullReferenceException: Object reference not set to an instance of an object PlayerAimWeapon.Update (). Could you help me?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Use Debug.Log to find what is null unitycodemonkey.com/video.php?v=5irv30-bTJw
@Tiparium_NMF
@Tiparium_NMF 3 жыл бұрын
How would you incorporate twin stick shooter style controls into this? This behavior is perfect for KBM, but I want KBM and controller to behave effectively the same.
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
You would just make a virtual cursor for the mouse position that you would move with a joystick
@teebs2721
@teebs2721 4 жыл бұрын
How would I make a player flip left / right depending on what side of he screen the mouse it on?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Test for the angle, if > 90 or < -90 set the localScale.y to -1
@nazrynn3550
@nazrynn3550 4 жыл бұрын
Hi Code Monkey, I added a shield that I can rotate around the player using this method, however the collider I added to the shield doesn’t seem to rotate with it, if you have any idea why this might be, it would be a huge help. Thank you so much for making these videos!
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
Where did you add the collider? If you add it to the image object it should work, just don't place it on the parent.
6 Years of Learning Game Development
17:20
Cobra Code
Рет қаралды 125 М.
The Trick I Used to Make Combat Fun! | Devlog
8:12
Game Endeavor
Рет қаралды 1,6 МЛН
哈莉奎因怎么变骷髅了#小丑 #shorts
00:19
好人小丑
Рет қаралды 48 МЛН
Как подписать? 😂 #shorts
00:10
Денис Кукояка
Рет қаралды 6 МЛН
HAH Chaos in the Bathroom 🚽✨ Smart Tools for the Throne 😜
00:49
123 GO! Kevin
Рет қаралды 13 МЛН
3 Ways to Shoot Projectiles in Unity!
23:07
Code Monkey
Рет қаралды 243 М.
Unity 2D Aim and Shoot at mouse position Tutorial
14:39
MoreBBlakeyyy
Рет қаралды 80 М.
2D Shooting in Unity (Tutorial)
21:12
Brackeys
Рет қаралды 1,2 МЛН
Creating SMART enemies from scratch! | Devlog
5:40
Challacade
Рет қаралды 326 М.
Ability Bar in Unity (Swap Weapons/Items)
29:07
Code Monkey
Рет қаралды 32 М.
What are Events? (C# Basics)
15:05
Code Monkey
Рет қаралды 391 М.
TOP DOWN MOVEMENT in Unity!
22:30
Brackeys
Рет қаралды 1,2 МЛН
I Made the Same Game in 8 Engines
12:34
Emeral
Рет қаралды 4,1 МЛН
An Aseprite Crash Course In 30 Minutes
31:47
AdamCYounis
Рет қаралды 1,1 МЛН
How I Would Start Game Development (If I Started Over)
16:59
Thomas Brush
Рет қаралды 113 М.
哈莉奎因怎么变骷髅了#小丑 #shorts
00:19
好人小丑
Рет қаралды 48 МЛН