Тёмный

Unity - Point and Shoot Tutorial 

Press Start
Подписаться 24 тыс.
Просмотров 82 тыс.
50% 1

In this tutorial we discuss how to point and shoot a turret towards the direction of your mouse.
SUBSCRIBE: bit.ly/2Js78lE
============
SUPPORT: / pressstartclub
In this video, we cover:
+0:50 - Creating a C# Script
+1:40 - Calculating our target position
+2:04 - Crosshairs following mouse movement
+2:40 - Hiding our mouse cursor
+3:15 - Rotating our gun to point towards the mouse
+4:25 - Creating an anchor point for our turret
+5:00 - Firing a projectile on click
+8:40 - Showing off the finished product
IF YOU FOUND ANY VALUE FROM THIS VIDEO CONSIDER JOINING OUR PATREON CLUB:
/ pressstartclub
COPY & PASTE CODE FROM THIS TUTORIAL:
pressstart.vip/tutorials/2019...
MORE TUTORIALS:
pressstart.vip/unity-tutorials

Опубликовано:

 

10 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 210   
@lznika2732
@lznika2732 4 года назад
In the last 2 nights i watched at least 7 video tutorials and a bunch od sites and you were only that actually helped me. Got my sub!
@MrValiantrobe
@MrValiantrobe 2 года назад
I spent 2 days on this by myself and scrapped my entire Projectile system and then this video helped me quite a bit. Thanks!
@maciekantosik6781
@maciekantosik6781 3 года назад
Thanks a lot. This tutorial helped me with my first Game Jam.
@mucahiterenozkur1124
@mucahiterenozkur1124 2 года назад
This video is still helpful. It made my job really easier. Thank you for your effort man, I appreciate it :)
@mohidtahir5340
@mohidtahir5340 3 года назад
Thanks this helped me alot, I wasted alot of time finding solution and your video was the one which helped.
@toterbui3628
@toterbui3628 4 года назад
Very awesone no one ever upload like this video thank you..
@pressstart6864
@pressstart6864 4 года назад
Thank you. I try to be different. Appreciate your feedback!
@codycanipe4415
@codycanipe4415 2 года назад
Wow, that was clean!
@pushpahebbar6540
@pushpahebbar6540 3 года назад
Thank you very much you helped me a lot.
@mattheww1072
@mattheww1072 4 года назад
Your tutorials are so great! Everything is so simple and easy to follow. do you think you could do a tutorial on object pooling? I'd love a tutorial from you about this.
@edsonbcz2973
@edsonbcz2973 4 года назад
You help me some much! Thanks!
@Gurseerat_Singh
@Gurseerat_Singh 3 года назад
Omg bro i thank you soooooooooooooooooooooooooooo frickin much bro i love you i was mailing a game and i was not able to solve a bug in which my bullets would not go in the correct way but you my bestie you solved the problem which big youtubers like code monkey,blackthornprod were not able to do for me i was getting a headache man trying to fix the dammn bug but i couldnt but you did it man i thank you sooo much
@MariWeiss28
@MariWeiss28 2 года назад
Thanks so much!
@amezing4478
@amezing4478 5 лет назад
Good sir, thank for video
@ReasonableRadio
@ReasonableRadio 4 года назад
the pronunciation of Quaterion.Euler() killed me lmao
@pressstart6864
@pressstart6864 4 года назад
Haha ;)
@victorzhang6859
@victorzhang6859 4 года назад
@@pressstart6864 qua-ter-neeeeeeeeeeeeeeeeeeeeeeeeee-on
@dominikkin2588
@dominikkin2588 5 лет назад
Thank you!
@RyusukeSky95
@RyusukeSky95 4 года назад
Really thank you, very usefull
@GTop-ss2lg
@GTop-ss2lg 4 года назад
thanks very much bro!
@personific.huntore7436
@personific.huntore7436 4 года назад
ty, very useful tutorial
@pressstart6864
@pressstart6864 4 года назад
Awesome!
@dariuszcegieka3494
@dariuszcegieka3494 5 лет назад
Epic video thank you very much!
@pressstart6864
@pressstart6864 5 лет назад
Thank you for your support!
@dariuszcegieka3494
@dariuszcegieka3494 5 лет назад
With your films you have really helped me with making my game, your tutorials are really good keep doing good work :)
@NOISCALE
@NOISCALE 3 года назад
Perfect !! Thx!
@mahmoud-quran
@mahmoud-quran 4 года назад
You are awesome!
@arkadqta6987
@arkadqta6987 5 лет назад
thanks man
@daichi_devs
@daichi_devs 4 года назад
You haven't uploaded in a while but I wanted to let you know that all your videos are really well made and easy to follow. So thank you and I hope you come back!
@HB-wk6zj
@HB-wk6zj 4 года назад
THANK YOU MAN THANK YOU THANK YOU !!!!!
@pressstart6864
@pressstart6864 4 года назад
You’re welcome bud!
@Alicia0Cramel
@Alicia0Cramel 4 года назад
thank you!!
@ChaluNetwork
@ChaluNetwork 4 года назад
Thanks a lot mahn
@pressstart6864
@pressstart6864 4 года назад
Thank you!
@user-ei3kg3du8v
@user-ei3kg3du8v Год назад
Great
@danieljayne8623
@danieljayne8623 3 года назад
I'm a bit confused about the fireBullet method. Surely as we are setting velocity on a RigidBody2D we would need to call that code within the FixedUpdate method?
@benn709
@benn709 4 года назад
thanks
@LilPie
@LilPie Год назад
tysmm
@funnyteddy3472
@funnyteddy3472 3 года назад
thank you for this. but i have a question, how can i limit the rotationZ what I mean is i want to stop the rotation of the player when the rotation Z is 12 ?
@benogden1409
@benogden1409 2 года назад
Does anyone else have the issue where the cross hair seems to move in the inverted direction? If I change the camera to be on the other side of the scene then it fixes the problem but now that means that I have to worry about flipping everything else(like player movement etc). How do I fix the cursor so that It aims correctly no matter where the camera is?
@gamalalejandroabdulsalam904
@gamalalejandroabdulsalam904 5 лет назад
god bless you
@idontdogood6870
@idontdogood6870 5 лет назад
Whenever I’m stuck you always seem to have a tutorial for what I need
@pressstart6864
@pressstart6864 5 лет назад
Awesome, glad I could help
@caioianoliveira9488
@caioianoliveira9488 3 года назад
Muito bom conteúdo ótimo
@nguyenhoangdung3823
@nguyenhoangdung3823 5 лет назад
nice
@osamaanwar1277
@osamaanwar1277 4 года назад
Can you show how to the same thing in 3D with a fixed camera on the back of a character holding a gun and shoot in world space according to a crosshair in screen space
@victorzhang6859
@victorzhang6859 4 года назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-_QajrabyTJc.html Brackeys' video explains it all.
@Oscar-xt8hm
@Oscar-xt8hm 4 года назад
@@victorzhang6859 they doesnt really explain the same thing :/
@tornanblade665
@tornanblade665 3 года назад
@@Oscar-xt8hm Osama & Oscar the two O's of Oscorp
@audacityfilmsproductions5334
@audacityfilmsproductions5334 3 года назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-6eIVxyxoimc.html Here ya go.
@victorzhang6859
@victorzhang6859 3 года назад
i guess u would have to use raycasts
@bishopjackson2264
@bishopjackson2264 Год назад
How would you do the offset? Like imagine if you had a guy (top down) holding a gun in his right hand (slighty off from middle of turret)?
@sadiulakhanji6271
@sadiulakhanji6271 4 года назад
everything works except as I move the cursor more, the bullets shoot further away from the cursor. also for me, my player object starts the game with its rotation not facing the cursor. its rotated 90 degrees away from the cursor but still shoots in the relative direction of it. any help, please?
@byterexe4325
@byterexe4325 3 года назад
the tutorial is really great! but because im using all this on a pistol, i need to finf a way thet when cursor comes to the left side, character goes to the left side instead of gun being in weird direction
@jakanater7763
@jakanater7763 3 года назад
you might have to subract or add by 90 btw
@3shego
@3shego 4 года назад
how would I make a delay in between shots?
@walney2008
@walney2008 3 года назад
hello, in the world of submarines game, how to do the movement mechanics? with constant speeds, and the system that marks the ship to fire the torpedo?
@Wolfzoon
@Wolfzoon 3 года назад
So you first have a vector3 target, pointing to the same z as transform position, of which you substract player.transform.position, so difference would have a z value of 0. Then you divide what is essentially a 2d vector by it's magnitude, aka normalizing it, and then you normalize it again? Why?
@ilovedatfruitybooty9546
@ilovedatfruitybooty9546 3 года назад
7:27 Just saving!
@eduardomarin6836
@eduardomarin6836 3 года назад
How could I do it if I want to make a bullet shoot from the player to the direction where I touch the screen (on mobile phones)
@PotatoTrader
@PotatoTrader 4 года назад
Does anyone know why I keep getting the error *camera does not not contain a definition for screentoworldpoint* pls someone tell me I’m really confused
@mehdib1781
@mehdib1781 3 года назад
Please HELP, i want to do an archer player that can rotate only in some angles and not doing 360 degres rotation, please how can i do that?
@jacksaunders8527
@jacksaunders8527 4 года назад
My ball rotates around the centre of the screen once I move it, how do I set the position of bullet fire on a moving player?
@jacksaunders8527
@jacksaunders8527 4 года назад
valid
@stuubo132
@stuubo132 4 года назад
when I try to puy the point and shoot file on the camra I get a notification saying "Cant add script behaveiour AssemblyInfo.cs The script needs to derive from MonoBehaviour!" plz help
@Alicia0Cramel
@Alicia0Cramel 4 года назад
pleas ehelp, when i try to do the code the moving player is pushed back for every shoot and it's annoying
@khankhalik2188
@khankhalik2188 5 лет назад
I am trying to make a player control same as in (Smashing Four game), any help will be appreciated.
@johku433
@johku433 4 года назад
How can i do this without turret rotation?
@tornanblade665
@tornanblade665 3 года назад
please do a video on restarting levels when a sprite hit a tile
@SatishSatish-vp6ck
@SatishSatish-vp6ck 3 года назад
How to add collider to target and bullets will destroy when it touch's the target Can u guys help me with this
@NGamerify
@NGamerify 4 года назад
Great tutorial, how can I do this but instead of the mouse button, to the position of a GameObject in screen?
@fongaming101
@fongaming101 3 года назад
Public gameobjectonscreen; Instantiate( projectile, gameobjectonscreen.position, quaternion.identity)
@hammerheadforme1585
@hammerheadforme1585 4 года назад
Can someone help me my player isn’t pointing exactly towards my mouse but my bullets are
@christopherdoiron4294
@christopherdoiron4294 2 года назад
I have a character who runs around, rather than stationary turret like you. When I shoot, it seems like the angle is off as I move around myself. On the right hand side, bullet hits target. on the top and bottom, the bullet is a few degrees off. Doesn't matter how I center/reset positions of the objects and parents. It seems like its the math behind finding the angle, and how it doesn't match exactly with the position because of the screen dimensions? Does this make sense?
@UglyDouglyss
@UglyDouglyss Год назад
Did you make the gun in the player spot
@David-gh5es
@David-gh5es 4 года назад
im working on an other type of game and i want to make the player's rotation unvisible. is there any way to do that?
@blwho8881
@blwho8881 4 года назад
Create another object without a sprite and use it as the player and u can add it to the actual player if you want
@kuma4an
@kuma4an 4 года назад
Спасибо!
@pressstart6864
@pressstart6864 4 года назад
Добро пожаловать
@kuma4an
@kuma4an 4 года назад
@@pressstart6864 Thank you!
@cosminandrei4346
@cosminandrei4346 3 года назад
7:40 why did you divide difference by distance?This will be the same direction
@unversedunavailable793
@unversedunavailable793 3 года назад
Great tutorial! One question though. Is it possible to make it so when the player holds down the button it keeps firing until they let go?
@sweetys321
@sweetys321 3 года назад
Yes it is! In the part where you do the input for when you shoot just change it to getmousebutton and your done you might want to add a cool down to it though.
@unversedunavailable793
@unversedunavailable793 3 года назад
@@sweetys321 thank you :)
@2Asspie
@2Asspie 4 года назад
Really Thanks just a script i need it!! can you make different weapon swtich with this scripts?
@jasontravail8224
@jasontravail8224 2 года назад
are you using 3 d or 2d unity engine ?
@fuckdenneherkanal431
@fuckdenneherkanal431 4 года назад
I tried so many videoes, but none work. Every bullet is locked to positive x axis. Bullet doesn't start at bulletStart.
@vaggelispeppas1332
@vaggelispeppas1332 5 лет назад
7:38 isn't the value already normalized because you divide the difference with the length why do you normalize it again??
@zeroiq4737
@zeroiq4737 4 года назад
How long did it take you to learn? Im 1month into game development and still a noob
@ReasonableRadio
@ReasonableRadio 4 года назад
Isn't there a built in function that wraps the arctan math as a "pointAt" function or something?
@pressstart6864
@pressstart6864 4 года назад
There's LookAt() but it didn't work well for 2D
@hasanyasin2752
@hasanyasin2752 4 года назад
Thats cool, but could you teach us how to do this on mobile too? Nice tutorial btw!!!
@sirloinsteak4094
@sirloinsteak4094 4 года назад
can someone help me i used his code and after i when i fire the first shot it's visible but all other shots after are not visible.
@l4k8
@l4k8 3 года назад
having the same problem
@fongaming101
@fongaming101 3 года назад
Check sprite render sorting layer or projectiles z to 0. Test by switching to 3d mode.. maybe hit apply overrides on prefab
@AnonymousKhaos
@AnonymousKhaos 4 года назад
I want this except i want the crosshair to kinda sway around the mouse any ideas?
@christianstein9242
@christianstein9242 3 года назад
create a random number generator, with the number created being a reference, and add it to the mouse position calculation
@cadenouttakes3983
@cadenouttakes3983 3 года назад
Can this work when making a 2D game in unity 3D? My crosshair is going to the opposite side of the screen from my mouse.
@lslightningstrike284
@lslightningstrike284 3 года назад
what ive found if you put the camera on the other side it works normaly
@FightingSportsMedia
@FightingSportsMedia 3 года назад
Should use fixed update
@brucescott7156
@brucescott7156 4 года назад
Currently trying to figure out how to use the rotation for Y rotation so I can do this on a 3d Game
@sarahcruz2103
@sarahcruz2103 4 года назад
did you get it?
@tayyarmoh
@tayyarmoh 3 года назад
sir im doing this on a gun but i want the gun to stop on 90 degree and -90 can you help me?
@tayyarmoh
@tayyarmoh 3 года назад
ok i got a solution
@lukasmerta8244
@lukasmerta8244 4 года назад
Can I use this in my mobile game ?
@hassanmaswedish2290
@hassanmaswedish2290 2 года назад
Press start
@ZakRahman
@ZakRahman 5 лет назад
Nice tutorial! I encountered the following issues: I couldn't drag a script onto the camera. "Can't add script behaviour AssemblyInfo". I had to add it as a component within the camera itself. Not a problem. Secondly, my mouse directions for the cross hair were inverted, and any fix I tried to make just made the entire thing disappear.
@shivambawa
@shivambawa 5 лет назад
you must have named your script different than the class name just make both the class name and the script name same
@x-blocks8092
@x-blocks8092 4 года назад
just write -target
@pressstart6864
@pressstart6864 4 года назад
If the cross hairs are inverted it’s because you’re using a different camera than I presented in the script. Like said above just invert the target values like so: crosshairs.transform.position = new Vector2(-target.x, -target.y);
@MickMackMikkel
@MickMackMikkel 4 года назад
How do you deallocate the projectiles after they have been generated in the scene, they just keep clogging up the game?
@pressstart6864
@pressstart6864 4 года назад
Not sure what you mean? You mean like remove them from the scene or object pooling?
@MickMackMikkel
@MickMackMikkel 4 года назад
@@pressstart6864 Like around the 8:00 mark, when you're shooting the projectiles, you can see that they are "kept alive" in your game. Sure it's no problem for a small tutorial, but for a bigger game these would have to be deleted in order for the game to run smoothly. I digged around and found out that you can destroy the projectiles using Destroy(name, time), so for instance you could write destroy(projectile, 1) to have them destroyed after 1 second, or you can destroy them when they leave the view...
@pressstart6864
@pressstart6864 4 года назад
Yes, I usually write a script to detect when objects leave camera view. I cover it in this video: Spawning Objects in Unity [Using Instantiate] ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-E7gmylDS1C4.html
@aventura1266
@aventura1266 3 года назад
My projectile is 90 degrees off in angle, I tried to edit it in "open prefab". How can I angle it correctly as it shoots?
@folkhero2046
@folkhero2046 3 года назад
Same here. I think I might be 270 degrees off, the projectile is lagging 90 degrees. Did you ever figure it out @Martin Ronning?
@aventura1266
@aventura1266 3 года назад
@@folkhero2046 Never did find it out. I guess a possible solution is to edit the prefab correctly in Photoshop before you put it in as a prefab, but I got some transparency issues while trying this.
@folkhero2046
@folkhero2046 3 года назад
@@aventura1266 I did get it to work by rotating the prefab 90 degrees, not a very elegant solution, but it does work. Thanks man
@JxD01
@JxD01 5 лет назад
Hi Sir I Finally Successfully Completed my Project Cuz of You Thanks alot but from 2 days i m Turning my Project to Android Platform and I can,t Rotate my Cannon , or Gun with Touch controls pls explain how can i control my Aim or Rotations with Joystick in Android I will be Very Thankful Sir!! "i just want to Switch Controls from Mouse to Touch joystick"
@pressstart6864
@pressstart6864 5 лет назад
This might help you: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-98dQBWUyy9M.html
@pressstart6864
@pressstart6864 5 лет назад
Actually this might even be better. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-xHQqtddMc9U.html
@kooltyme
@kooltyme 4 года назад
how do i set the magnitude of my velocity, my code wasnt exactly the same as yours, and idrk how to set my magnitude
@kooltyme
@kooltyme 4 года назад
i want to do this since the bullet goes at diff speeds depending on how far away the cursor is.
@kooltyme
@kooltyme 4 года назад
nvm i didnt need one, i just did direction*(bulletSpeed/(Mathf.Abs(direction.x)+Mathf.Abs(direction.y))
@wonsiastywons3436
@wonsiastywons3436 3 года назад
If anyone (like me) had a problem to get rotation of a player working, I found other solution (for rotation): Vector2 positionOnScreen = transform.position; //Get the Screen position of the mouse Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition); crosshair.transform.position = new Vector2(mouseOnScreen.x, mouseOnScreen.y); //Get the angle between the points float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen); //Ta Daaa transform.parent.gameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
@walney2008
@walney2008 5 лет назад
good video, tnhks, how AI enemy bomb in tower ?
@istwythevil1659
@istwythevil1659 3 года назад
yes
@drpaingaming3901
@drpaingaming3901 4 года назад
everybody badass until 3:20
@thatboredinternetwanderer140
@thatboredinternetwanderer140 3 года назад
uh oh
@HasbiHB
@HasbiHB 3 года назад
my crosshairs position inverse with my mouse position help please
@HasbiHB
@HasbiHB 3 года назад
never mind im just change the z mouse input into minus then its work
@lostdeeper
@lostdeeper 4 года назад
you now made a singleplayer diep.io
@nanthigapuvirajan8704
@nanthigapuvirajan8704 4 года назад
eyyo ma bullets arent showing upp on the game screen but are in the project screen
@muhammetkaraman9949
@muhammetkaraman9949 3 года назад
I wonder how we're going to kill the enemy.
@haliskara2879
@haliskara2879 4 года назад
this script doesnt work if the bullet has rigidbody please ı need help
@sinthebay
@sinthebay 4 года назад
use rigidbody2D
@DecrareOld
@DecrareOld 4 года назад
How can I delete the bullet after some time?
@lancaerf.3924
@lancaerf.3924 4 года назад
Destroy(gameObject, float time);
@fancybaby404
@fancybaby404 4 года назад
help when i move my mouse at the game my camera rotates like crazy
@jacksaunders8527
@jacksaunders8527 4 года назад
I'm the same, did you fix this? any chance you can help me? same problem.
@koferperk
@koferperk 4 года назад
2:54, wheres the link man?
@folkhero2046
@folkhero2046 3 года назад
Yes would really appreciate to know where this math came from. I need to correct my projectile lagging 90 degrees behind my cursor
@sushant.neupane
@sushant.neupane 5 лет назад
Can you make tutorial on score boards?
@pressstart6864
@pressstart6864 5 лет назад
Yes, sometime in the future I will :)
@zakwanmasud2406
@zakwanmasud2406 5 лет назад
@@pressstart6864 Yes
@sausageswiper2397
@sausageswiper2397 4 года назад
Says cannot find script
@solitarygrounds
@solitarygrounds 4 года назад
why is my mouse mirroring the damn thing when i copied exactly what you have ugh!
@iii-tf6rc
@iii-tf6rc 3 года назад
you should avoid using getComponent in update for optimization
@ikryxxdev4756
@ikryxxdev4756 4 года назад
Some problems i will fix for you: 1. The player is following the mouse, but the rest is not working The script must have the name PointAndShoot 2. My player follows the mouse in the wrong angle Go to player.transform.rotation and go after Quaternion.Euler instead of leaving (0.0f, 0.0f, rotationZ) experiment with bunch of degrees at the Z position for example, when u have a triangle (0.0f, 0.0f, rotationZ - 90.0f) Hope i could help:)
@adrigl3371
@adrigl3371 4 года назад
Thanks man
@jmthecreator7556
@jmthecreator7556 4 года назад
thanks, this solved my issue
@brave-coeurobiasnara1194
@brave-coeurobiasnara1194 4 года назад
When my bullet create, she automaticly goes to a z pos of -393, and I dont know why...
@David-gh5es
@David-gh5es 4 года назад
Vector3 pos = transform.position; pos.z = 0; transform.position = pos; add this code to the update
@brave-coeurobiasnara1194
@brave-coeurobiasnara1194 4 года назад
@@David-gh5es I fixed this on my own thx for answer
@ayyo_akshita
@ayyo_akshita 4 года назад
I have a problem the bullet just appears for 1 second then it just disappears
@kooltyme
@kooltyme 4 года назад
the speed is probably really high
Далее
Mobile Joystick Movement + Shoot in Unity
11:09
Просмотров 35 тыс.
Stray Kids <ATE> Mashup Video
02:17
Просмотров 1,5 млн
Unity 2D Aim and Shoot at mouse position Tutorial
14:39
Better Coding in Unity With Just a Few Lines of Code
15:27
3 Ways to Shoot Projectiles in Unity!
23:07
Просмотров 240 тыс.
Unity Movement [RigidBody vs Translate]
9:00
Просмотров 282 тыс.
TOP DOWN MOVEMENT in Unity!
22:30
Просмотров 1,2 млн
How to jump in Unity (with or without physics)
16:09
Просмотров 41 тыс.
2D Melee in Unity Tutorial
19:46
Просмотров 15 тыс.
10 Unity Tips You (Probably) Didn't Know About
6:47
Просмотров 36 тыс.