2D Bullet / Projectiles in Unity / 2023

In this video we cover 2D projectiles and collisions for Unity and C#
This video is part of our "In 5 minutes" series where we show you exactly what you want to know.
SUPPORT GREAT CONTENT
----------------------------------------------------------------------------------------------------
/ distortedpixelstudios
$3 = Coffee with Perks (You can even get 1-on-1 unity support here)
CHAT WITH US
----------------------------------------------------------------------------------------------------
Discord = / discord
Twitter = / dpstudiosdev
USED UNITY ASSETS
----------------------------------------------------------------------------------------------------
Environment: assetstore.unity.com/packages...
Characters: assetstore.unity.com/packages...
Projectile: opengameart.org/content/magic...
Music by Matthew Pablo
www.matthewpablo.com
Track : Stepping Stones

Пікірлер: 115

  • @FLStudioTutorialz
    @FLStudioTutorialz3 жыл бұрын

    These 5 Minute tutorials are amazing. Thank you for that. I hope it's not over yet because the last upload is over 8 month behind. Your on point every single time. Keep it up.

  • @lorenzo5275
    @lorenzo5275 Жыл бұрын

    Wow bro... this is the first tutorial of my life that is clear, fast, efficient and even better than unity tutorial.... good job, pls continue to post video, we love it

  • @autokratija
    @autokratija3 жыл бұрын

    Hello guys, thanks for wonderful tutorial series. It's been very helpful ! Please, keep continue !!!!

  • @spo0ds152
    @spo0ds1523 жыл бұрын

    i love how there is this relaxing music in a video about a really hard part of programming

  • @LookFren

    @LookFren

    3 жыл бұрын

    its pretty simple actually

  • @shortwatch4
    @shortwatch44 жыл бұрын

    The quality of this tutorials it's amazing for your number of subscribers, keep workin on it men!

  • @shortwatch4

    @shortwatch4

    4 жыл бұрын

    @@DistortedPixelStudios you could do a short tutorial series of a genre, like 5 videos for a simple game

  • @alexonian9543
    @alexonian9543 Жыл бұрын

    old but gold, making a turret game and this got me started

  • @rishikeshkarkhanis
    @rishikeshkarkhanis3 жыл бұрын

    Really helpful tutorial, keep it up man! 👍

  • @bakihanma1680
    @bakihanma16802 жыл бұрын

    easy to follow !! thanks alot

  • @zer0._.
    @zer0._.3 жыл бұрын

    A huge thank you...😁😄💚💚💚💚

  • @BigGermanShip
    @BigGermanShip3 жыл бұрын

    Thanks for this amazing Tutorial

  • @4SeasonProducer
    @4SeasonProducer3 жыл бұрын

    "wow that huge" very sadistic

  • @aroctagames4175
    @aroctagames41753 жыл бұрын

    for those who are having the issue where it only fires one direction, I figured out that it happens because of how you turn the player. if you follow the brackeys tutorials he has a guide for a 2d character controller but in a later video he changes how it is done for this exact reason. Most everything here still works but in the charactercontroller2D you need to change how you flip the player to "transform.Rotate(0f, 180f, 0f);" without the "quotes" and back in the projectile behavior script all you need is to change it to this " transform.position += transform.right * Time.deltaTime * Speed;" again without the "quotes" what is causing the problem is with scaling the sprite to change where you are facing the fix mentioned above is to make the sprite rotate rather than scale differently. because with scaling the lauchoffset stays looking to the right and will always think you are facing that direction and fire one way, but if you rotate the player parent object (change above) the lauchoffset will rotate with it, hope that helps

  • @DistortedPixelStudios

    @DistortedPixelStudios

    3 жыл бұрын

    You do know that I show how to do this later in the video right?

  • @aroctagames4175

    @aroctagames4175

    3 жыл бұрын

    @@DistortedPixelStudios no I just found this video today and had the problem and then found a way to fix it. Most videos I've found dont have any sort of follow up with these problems so since I didnt see any comment about it I didnt even consider it

  • @bane5807

    @bane5807

    3 жыл бұрын

    @@DistortedPixelStudios at the end of the video? where?

  • @chinest3386

    @chinest3386

    2 жыл бұрын

    Hey don’t know if you still have that movement code but I don’t understand what I was supposed to do for the first fix also when I flip my character it goes invisible

  • @chinest3386

    @chinest3386

    2 жыл бұрын

    @@aroctagames4175 for now I’m gonna make my game with it only shooting right

  • @christopherdoiron4294
    @christopherdoiron42943 жыл бұрын

    Not bad. Now lets get it following the mouse for directional firing!

  • @RazgyMicYt
    @RazgyMicYt4 жыл бұрын

    Thanks man this really helped me couse i am really into it and i'm working to a game and your tutorials really helped me. I really want to ask something. Can you please make a tutorial like how to pick up items like score u now how it is in Mario you pick up a coin and it gives you that coin if you can do that it will be really helpful for 2d btw couse from all the tutorials i watched yours were the most helpful and good couse you are going straight to the thing that you are showing in the video and usually almost no KZread does that

  • @gamoe
    @gamoe Жыл бұрын

    thanks you

  • @bobsanders5232
    @bobsanders52322 жыл бұрын

    First off this is an absolutely great video, and I'm keeping it for later, but in case there's anyone else who had the problem where the bullet spawned but didn't move forward at all the solution is in ProjectileBehavior replace the lines 6 to 10 with; public RigidBody2D _rigidBody //drag your the Projectile RigidBody2D into the _rigidBody box that'll be created under the script in the Projectile prefab inspector void Start() { _rigidBody.velocity = transform.right * Speed } I don't know why it didn't work for me and I don't see anyone else in the comments with this problem but... you can never be too sure, Cheers folks!

  • @MiguelRodriguez-ff9wl

    @MiguelRodriguez-ff9wl

    2 жыл бұрын

    my issue was the bullet travels faster when the player is moving, but the bullet travels slower when the player is idle, however your solution was better for me anyways so thanks for that however you want to include Time.deltaTIme somewhere in there,

  • @theday7110
    @theday71103 ай бұрын

    thank you ...

  • @unitynocode
    @unitynocode2 жыл бұрын

    Thank you ! I tried more than 10 examples from Google. But only your code working.

  • @ruizon6899
    @ruizon68992 жыл бұрын

    For some reason whenever i shoot on the right i cant see it in the game and scene view but when i played it in 3d whenever i shoot to the right the projectile spawns behind the camera thats why i couldnt see it so i just moved the camera back so i can see it Why does it spawn behind the camera?

  • @Lilyvex
    @Lilyvex Жыл бұрын

    Great tutorial! For some reason the projectile only moves right, regardless of which way the player is facing. Would love some pointers to this problem!

  • @Huy-G-Le
    @Huy-G-Le Жыл бұрын

    I have this problem, where: 1# I press the left mouse one, and three projectile spawn. 2# The projectile stay in the same spot, they won't fly any further. 3# The projectile only appears at one side, the right side and not the left side. How do I fix this? Also I'm using a different Player Movement Script, doesn't seem to raise any confliction though.

  • @Kackenswert
    @Kackenswert3 жыл бұрын

    When I did the codeding the LaunchOffset and ProjecttitlePrefab is not by the others like Jump Force or Movement Speed

  • @egemen5404
    @egemen54043 жыл бұрын

    OnCollisionEnter2D not working properly. When i was type "private void OnCollisionEnter2D" it looks like as a simple method. Does anyone know the solution?

  • @dragonballz3686
    @dragonballz36862 жыл бұрын

    Bro, I am making a 2d top down space shooter. There is a problem in shooting system, that is , when enemy is shooting and I shoot, then the player's bullet is colliding with enemy bullet and the player's bullet is destroyed but the enemies bullet is not destroyed. Can you tell me what to do to make the both bullets passing away without any collision.

  • @DieDona
    @DieDona2 жыл бұрын

    One question, does the projectile really need a rigid body, since you are moving it with the transform?

  • @Fwsr.
    @Fwsr. Жыл бұрын

    to what button is the fire button assigned? it gives an error that i havent did that yet

  • @Uncrypt
    @Uncrypt3 жыл бұрын

    Can somebody paste me the ProjectileBehaviour Script im getting the same error over and over and its the simplist one i just dk how to debug it.

  • @clementparrot4001
    @clementparrot400110 ай бұрын

    If your bullet only moves in one direction, you're probably using localScale for flipping your player whereas in this video transform.rotate(0,180,0) is used for flipping. So, on your Flip method, just add at the end: LaunchOffset.transform.Rotate(0f, 180f, 0f); And when you instantiate your bullet, write : Instantiate(projectilePrefab, LaunchOffset.position, LaunchOffset.transform.rotation) All should work after that ;)

  • @seb56137
    @seb56137 Жыл бұрын

    I'm getting an error where it says the namespace ProjectileBehaviour couldn't be found. I've made sure to spell it right and everything

  • @dantheace8538
    @dantheace8538 Жыл бұрын

    I wrote my script for the enemies projectiles and it worked flawlessly, but when I copy paste it to my player's the projectile just fly right through the enemy ?? The script for the enemies can kill my player but my player's script can't, even tho it's a copy paste from the enemies ?

  • @emrecanyarici5408
    @emrecanyarici54083 жыл бұрын

    2:52 When I type the word, it does not offer me an article and does not accept my article, what could be the reason for this?

  • @rendericu92
    @rendericu923 жыл бұрын

    I have a strange issue here. When facing right, it shoots right but when facing left, the projectiles still go right and collide with the player

  • @aroctagames4175

    @aroctagames4175

    3 жыл бұрын

    i posted a fix here its a lot to retype out but just look at that comment if you still need help

  • @haraldseventyrogspill1380
    @haraldseventyrogspill1380 Жыл бұрын

    hey i used your code and my projectile donst vanish when it hits somthing and dosnt aim where im lookign, may i get help pls

  • @SuperGamePlayOfficial
    @SuperGamePlayOfficial3 жыл бұрын

    Nice but how can i add range to it

  • @MiguelRodriguez-ff9wl
    @MiguelRodriguez-ff9wl2 жыл бұрын

    This worked for me however the bullet travels way faster when the player is moving and shooting at the same time and the bullet travels slower when the player shoots while standing idle. To fix that, I set the rigidbody2d body type of the projectile prefab to kinematic and now it fires much better from the players move speed but now it totally ignores my collisions detections. Update: Just realized there a checkbox for use kinematic/static collisions when you have kinematic body type selected, I think this body type works best for this tutorial

  • @MyMike004

    @MyMike004

    Жыл бұрын

    nice update and question did you run into the issue that OnCollisionEnter2D for the projectile is not firing off?

  • @ciprianpasca7738
    @ciprianpasca77383 жыл бұрын

    So i have this problem. When i jump and shoot, the projectile doesn't collide with anything. Also they dont destroy on impact.

  • @letsgamingde420

    @letsgamingde420

    2 жыл бұрын

    Have the same weird problem, it does work with the grenades, but with these it does not work

  • @nintenerd7680
    @nintenerd76802 жыл бұрын

    hey been following your tutorial step by step. but after i programmed the prefabs then ran the game. the console would recognise the button click but it doesn't show the circle bullet image. do you have any idea why this could be?? EDIT: i got it shooting, however the objects are not destroying, could anyone in the comments or yourself can bring this to attention please??

  • @MyMike004

    @MyMike004

    Жыл бұрын

    same here

  • @bluups8477
    @bluups84773 жыл бұрын

    Okay so in the script, i there's no Projectile prefab and launchoffset option, i dont know why cause i follow the tutorial exactly the same. Any ideas?

  • @RiaanWalters

    @RiaanWalters

    3 жыл бұрын

    See this timestamp 03:28

  • @qidu585
    @qidu5853 жыл бұрын

    How can I make a delay so that you can’t constantly click to spam them

  • @erwanan5971
    @erwanan59713 ай бұрын

    Im having trouble with manipulating the empty objects with my mouse, I dont know but it seems like Unity doesnt let me drag it

  • @deepsolace92
    @deepsolace92 Жыл бұрын

    the projectile only moves to the left, what is wrong?

  • @wisochiso877
    @wisochiso8773 жыл бұрын

    How can I get rid of the clones that go for ever?

  • @tylerdigiacomo1541
    @tylerdigiacomo15413 жыл бұрын

    my bolts keep going at a 45degree angle anyone know how to fix???!

  • @hilkevih9086
    @hilkevih9086 Жыл бұрын

    Что мне сделать, если влево пули летят нормально, а вправо они невидимые, но наносят урон. Все перепробовал уже

  • @zeanelhamas5392
    @zeanelhamas53923 жыл бұрын

    why my projectile can't get out and why my character if i cilck left mouse button doing backward

  • @alliocha0591

    @alliocha0591

    3 жыл бұрын

    It's because the projectile collide with your player . I change "transform.position += -transform.right * Time.deltaTime * Speed;" to "transform.position += transform.right * Time.deltaTime * Speed;" and it worked. But the projectile is shoot in my back ... I think the code to set sprite rotation is a bit messed up but i didn't find solution

  • @nolesquad5162
    @nolesquad51622 жыл бұрын

    The projectiles for me fire way back and don't fire at the correct line. Please help

  • @dimitrisb5654
    @dimitrisb56544 жыл бұрын

    can you make a vid about how to shoot on cursor? (bad english srr)

  • @SirgaNL
    @SirgaNL3 жыл бұрын

    Hi, nice tutorial! I have one problem, my character is only shooting to the right side. When i try to fire to the left, the projectile won't come out. Do you know what i did wrong?

  • @eloi1209

    @eloi1209

    3 жыл бұрын

    i dunno i have the same error did you fin any solutions since?

  • @zufar9194
    @zufar91944 жыл бұрын

    Pliss make collectible item tutorial🙏🙏

  • @Ziegelbomber
    @Ziegelbomber4 жыл бұрын

    Hey i maybye need some Help ^^ so i did everything as it is shown in the video but this if (!Mathf.Approximately(0, movement)) transform.rotation = movement > 0 ? Quaternion.Euler(0, 180, 0) : Quaternion.indentity; wasnt there last video. When i put it in it says compile error if its out the projectile only shoots in one direction to the left and if i press d it doenst shoot to the right

  • @appleduck1497

    @appleduck1497

    3 жыл бұрын

    i have the same problem have you found any solutions?

  • @s4l7h

    @s4l7h

    3 жыл бұрын

    @@appleduck1497 me to :c

  • @ruscyber9765
    @ruscyber97653 жыл бұрын

    It doesn't fire when the character turned to another side. (Every line of the code is written in a correct order)

  • @tigranvardanyan5530

    @tigranvardanyan5530

    3 жыл бұрын

    It fire but colision kill the bullet try 2:32 transform.position += transform.right instead of transform.position += -transform.right the minus makes bullet fly to character

  • @Draunzler

    @Draunzler

    3 жыл бұрын

    @@tigranvardanyan5530 instructions unclear, now every bullet hit me

  • @tigranvardanyan5530

    @tigranvardanyan5530

    3 жыл бұрын

    @@Draunzler Try this void Update() { transform.position += transform.right * Time.deltaTime * Speed; Destroy(gameObject,3f); }

  • @16bitgamers17
    @16bitgamers172 жыл бұрын

    i am getting this error: Assets\Player.cs(6,12): error CS0246: The type or namespace name 'ProjectileBehaviour' could not be found (are you missing a using directive or an assembly reference?

  • @dubbelthee8877

    @dubbelthee8877

    2 жыл бұрын

    I have the same no idea how to fix it tho

  • @16bitgamers17

    @16bitgamers17

    2 жыл бұрын

    @@dubbelthee8877 I changed it from ProjectileBehaviour to GameObject

  • @E-Man471

    @E-Man471

    2 жыл бұрын

    @@16bitgamers17 thank you

  • @base13music
    @base13music7 ай бұрын

    For some reason my projectiles don't destroy whenever they hit other colliders, they just stop moving an stay there.

  • @a_sesar
    @a_sesar Жыл бұрын

    on what do i shoot?

  • @Cxrsed_Selenity
    @Cxrsed_Selenity3 жыл бұрын

    i dis the movement and jump tutorial but idk what button to press to jump

  • @mzascool6139
    @mzascool61393 жыл бұрын

    Can anyone help when my projectiles hit the obstacles they don't destroy. There's no problem in my programming too

  • @zakr2084

    @zakr2084

    3 жыл бұрын

    hey did you firgure it out???

  • @alientizim1050

    @alientizim1050

    2 жыл бұрын

    Stuck as well

  • @HQmtb
    @HQmtb2 жыл бұрын

    hi my bullets go fro bricks what do i do

  • @sevpoolay
    @sevpoolay Жыл бұрын

    Ho my characters :V enjoy broz

  • @goddessyukii
    @goddessyukii Жыл бұрын

    My projectiles dont get destroyed when they hit something qwq

  • @Supribo
    @Supribo3 жыл бұрын

    thank you for the help but my projectiles are spawning on the back of the character and launches at him

  • @zakr2084

    @zakr2084

    3 жыл бұрын

    do you still need help??

  • @Supribo

    @Supribo

    3 жыл бұрын

    @@zakr2084 yes pls

  • @zakr2084

    @zakr2084

    3 жыл бұрын

    @@Supribo so you see the game object called launch off set move it foward and if its still shooting towards you rotate z 180

  • @Supribo

    @Supribo

    3 жыл бұрын

    @@zakr2084 ok im trying it RN

  • @Supribo

    @Supribo

    3 жыл бұрын

    @@zakr2084 didnt work at all both of them

  • @tredutore
    @tredutore Жыл бұрын

    how about animated projectile

  • @renno2679
    @renno2679 Жыл бұрын

    "Type 'ProjectileBehaviour' already defines a member called 'Update' with the same parameter types." No idea what that implies...

  • @katofffel4387
    @katofffel43872 жыл бұрын

    when i try to fire the characer just moves a bit and nothing happens

  • @Alkhalidi44

    @Alkhalidi44

    2 жыл бұрын

    same

  • @letsgamingde420

    @letsgamingde420

    2 жыл бұрын

    Your Projectile collides with the player and gets destroyed. There are 2 ways of fixing that: Easy-Way: Move ur LaunchOffset Better-Way: in "OnCollisionEnter2D" add this: Character2DController charControl = collision.collider.GetComponent(); Character2DController player = charControl; if (!player) { Destroy(gameObject); ]

  • @loneloa2049
    @loneloa20493 жыл бұрын

    why it only works for 1 time?????? if i die in the game and start it again, non of your codes will work

  • @inject742
    @inject7424 жыл бұрын

    it shoots only one way, to the left, but when i look right and fire, the bullets go in the same direction..

  • @CoreDreamStudios

    @CoreDreamStudios

    3 жыл бұрын

    add a - to transform.right

  • @kanchit3727

    @kanchit3727

    3 жыл бұрын

    @@CoreDreamStudios How to?

  • @zakr2084
    @zakr20843 жыл бұрын

    the block doesnt clear the bullets for me??

  • @aroctagames4175

    @aroctagames4175

    3 жыл бұрын

    does the block have a Collider2D on it? if not the "bullet" will just go through it

  • @misspeachyxo
    @misspeachyxo3 жыл бұрын

    for me it shoots at the right but it wont shoot at the left aswell

  • @aroctagames4175

    @aroctagames4175

    3 жыл бұрын

    I figured out why if you still need help with this I posted what fixed it for me on my own comment (to much to retype)

  • @afeefansari9370
    @afeefansari93703 жыл бұрын

    Can you please put the script? It keeps giving me errors.

  • @CoreDreamStudios

    @CoreDreamStudios

    3 жыл бұрын

    Which script? I can assist.

  • @foomi9363
    @foomi93634 жыл бұрын

    what button is suppose to be the fire button?

  • @DistortedPixelStudios

    @DistortedPixelStudios

    4 жыл бұрын

    Left Mouse Button (but it depends on your settings in Unity > Edit > Project Settings > Input Manager)

  • @user-eg7sq5vt7e
    @user-eg7sq5vt7e2 жыл бұрын

    I am this close to crying I did everything the same but it's not working at all >.

  • @alientizim1050

    @alientizim1050

    2 жыл бұрын

    Same

  • @ponsan7212
    @ponsan72122 жыл бұрын

    Dust

  • @Heyszki
    @Heyszki3 жыл бұрын

    This is a bad tuto