How to Make the Player Shoot (Unity Tutorial | 2D Top Down Shooter)

Поделиться
HTML-код
  • Опубликовано: 9 янв 2025

Комментарии • 47

  • @knightmarerip711
    @knightmarerip711 Год назад +1

    Great series! One thing I did different on this is when the player presses the space bar multiple times. Instead of adding the SingleFire check, I just added code on the OnFire to set the LastFireTime to Time.Time -1 when the spacebar is released. This gives a rapid fire effect.
    It's weird that you have 19k subscribers and there's less than 200 likes on this series. There should be way more than that given how great this is!

  • @oliviaculleton6614
    @oliviaculleton6614 Год назад

    you just saved my life. sincerely, a freshman game design student who's first assignment is a top down shooter

  • @erikstagg6347
    @erikstagg6347 Год назад

    Loving this tutorial series! I haven't made games since my 2D game class(with flash!) and 3D level design class with unreal engine in high school many years ago! Can't wait for more videos!

    • @KetraGames
      @KetraGames  Год назад

      Great, thanks for your support 😊

  • @wouterraemaekers6136
    @wouterraemaekers6136 Год назад +2

    This is great, this helps me so much. However you need to add a default Box Collider 2D to the enemy or it won't detect collisions, hope it helps anyone.

  • @WorstDeveloper
    @WorstDeveloper Год назад +1

    At 12:07, where did you get "gameObject" from? It's not defined anywhere as far as I can see.

    • @logeshstvs
      @logeshstvs Год назад

      Inside the OnTriggerEnter2D function, we can reference the Game Object of the collider it returns using collision.gameObject. So it gives you the game object with which we can disable it or destroy it as per our needs, here we are destroying it.

  • @kngt9655
    @kngt9655 Год назад +4

    My bullet only shoots straight up, is there any way to fix this?

    • @ix3669
      @ix3669 2 месяца назад

      hello. did u by chance fix this issue?

  • @nogrilodoguede7515
    @nogrilodoguede7515 2 года назад +1

    NEW VIDEO!!! I'M SO HAPPY!!!

  • @mostly4real
    @mostly4real Год назад +1

    Why does my _fireContinuously bool start as false and immediately trigger to true at the start of the game? (bullet fires continuously without interaction, then after first OnFire acts as it should turning on and off)

  • @Verevyta
    @Verevyta Год назад

    I really enjoy your tutorials. For this one, it would have been nice to explain how you made the input action file. Hope to see a video about shooting in 3D space.

  • @seandobson2682
    @seandobson2682 Год назад

    Great video, it helped me fix a bug in my code. Legends!

  • @GodofGamesss
    @GodofGamesss 2 года назад +1

    Any chance there will be a follow up on the: "Stand Back Up! How to Reverse Ragdoll Physics"? Having characters being able to stand up after being knocked down is a cool effect when done with the Ragdoll physics, rather then just normal animations.

    • @KetraGames
      @KetraGames  2 года назад

      Hi, we're planning to do a follow up soon showing how to cover the scenario where the Zombie is face down

    • @GodofGamesss
      @GodofGamesss Год назад +1

      @@KetraGames Cool looking forward to it.

  • @SILENTFromYT
    @SILENTFromYT 10 месяцев назад

    whenever i spress spacebar the bullets kepp going to a different directions how do I fix that?

  • @ix3669
    @ix3669 2 месяца назад

    Hello, my issue is that the bullets only fires upwards all the time and doesnt moves where the character is facing, can u please help me with this, I tried using gpt but It does not work at all.

    • @KetraGames
      @KetraGames  Месяц назад

      Hi, have you tried comparing your script with the one here - dotnetfiddle.net/8aQNri
      It could also be the gun offset transform. Make sure that this is a child of your character and rotates when your character rotates. Hope that helps 😊

  • @JiaguoLiu
    @JiaguoLiu 25 дней назад

    Thanks for your course!! It helps a lot. There is a problem and I wish you can help me solve it. The problem is I cannot use the function of "OnFire(InputValue inputValue)" or "OnMove(InputValue inputValue)", I wish to know Is this a special function that comes with unity? Or do you need to install some kind of plugin or something? Before that, I used to update the movement and shooting directly in the Update. Later, I found that this could not solve the problem mentioned at the end of the video, that is, clicking the shooting again before the shooting interval was over could delay the corresponding action. Can up help answer it? Thank you very much!🥲

  • @topherevans2564
    @topherevans2564 12 дней назад

    Followed this guide the the T. And didn't work. Space bar shoots a stream of bullets then stops, enemies don't collode with bullets, and the code shows lots of errors and broken references in visual studio.

  • @JakeMakesGames
    @JakeMakesGames Год назад

    followed the tut line for line, but my fire continuously bool isnt setting to true, any idea how to debug this?

    • @KetraGames
      @KetraGames  Год назад

      Hi. it may be something to do with the setup of the input. Do you have a binding called Fire in your input settings? This needs an interaction adding for 'Press and Release'. Then in the script the method name needs to match exactly so should be OnFire. Hope that helps 😊

  • @FznAmn
    @FznAmn Год назад

    why when the player fires bullets at the enemy, the bullets only penetrate the enemy and the enemy is not destroyed/disappeared

  • @tristanbluu
    @tristanbluu Год назад

    Hey, had a question for you. I set up the shooting script to the player, but every time i shoot the gun the character flies to the right. Anyway to fix this?

    • @KetraGames
      @KetraGames  Год назад

      Hi, is it possible you're setting the velocity of the player rather than the bullet when you shoot?

    • @tristanbluu
      @tristanbluu Год назад

      @@KetraGames Just as an update, I never clicked the "Is Trigger" button on the collider 2d component in the inspector tab. Thank you for your help, as well as your video. I appreciate it!

  • @JakeSterlingFan
    @JakeSterlingFan 7 месяцев назад

    Please help when I hold spacebar the bullets dont shoot continuously. They shoot and stay infront of the player
    Ah, nvm I didn't set up rigid body on the right one 😅 I love this series

  • @glebiank
    @glebiank Год назад +1

    Why can't we just use a tag for an enemy? All these 'instantiate, getcomponent, findobject, etc' methods are bad for perfomance, as far as I know. So we must avoid these unless it is impossible to do so, mustn't we?
    And why not use a coroutine for 'cooldowns' between shots?

    • @KetraGames
      @KetraGames  Год назад +1

      Hi, thanks for the great questions.
      You're right that using a tag for the enemy would be more performant than GetComponent. The check is only happening when an enemy is hit and not every frame though so the difference will be negligible. In this case I'd say it comes down to personal preference. I tend to avoid tags as I prefer the type safety provided by GetComponent over having to make sure I type the Tags exactly right every time 😊
      Regularly instantiating and deleting objects is something that can cause performance issues. This is usually resolved by a technique called Object Pooling. We'll be looking at how to do this later in the series.
      FindObjectOfType can be quite intensive so you definitely want to use it sparingly. I would advise only ever using this in the Awake method so that it is only called once for the object. We're currently using FindObjectOfType in the Enemy scripts because the enemies need to find various other Objects when they are spawned. Because the enemies don't exist in the Scene at the start we can't assign these components to the Enemies in the designer. The impact of this will be reduced when we use Object Pooling as we'll only ever Instantiate a set number of Enemies and then keep reusing them, meaning the Awake methods won't get called again. If this was causing a problem with performance we could improve this further by assigning the required components to the Spawners in the designer. Then we could pass through these objects to the Enemy scripts after they've been spawned.
      When it comes to optimising your game there is often a trade-off in code readability and maintainability, so my view is making your game as performant as possible isn't always the best way to go. If your game is already running comfortably at 60FPS on your target hardware then you wouldn't want to reduce maintainability for performance gains. Having said that you want to avoid common issues, like you've raised here, that can cause your game to fall below 60FPS. So to answer your question 'So we must avoid these unless it is impossible to do so, mustn't we?' my view would be that you must avoid them unless you feel it makes the code more maintainable and you're confident it won't have a noticeable impact on your game. I hope that helps 😊
      And yes, you could use a Coroutine for the cooldown if you prefer.
      Thanks again for the great questions and the support you're giving others in the comments 😊

  • @astout6494
    @astout6494 Год назад

    so can someone tell me why my bullet wont shoots any help pls

    • @usamntoday
      @usamntoday 11 месяцев назад

      have you solved it? I got stuck when we did the bullet prefab

    • @astout6494
      @astout6494 11 месяцев назад

      Yes I did it’s the layering change the number from zero to like two in the bullets should show up

  • @xaviers5781
    @xaviers5781 2 года назад +1

    Any way to update more frequently?

    • @KetraGames
      @KetraGames  Год назад +3

      Hi, the aim is to release a video once a week, although it may not always be on this series. Sometimes life gets in the way though and it's not always possible to achieve this. We'll do our best 😊

  • @unston
    @unston Год назад +1

    my bullet just goes up

    • @ix3669
      @ix3669 2 месяца назад

      hello, did u by any chance fixed this issue?

    • @unston
      @unston 2 месяца назад

      I don't remember, sorry, it was a while ago and ever though I probably did solve the issue, I don't even know in which project I used the tutorial
      If you have any issues I suggest asking AIs like chat GPT, gemini, etc, as it often knows how to solve the issue. You can even paste the link of the video toelaborate on your problem

  • @Sonsuz-Oyun
    @Sonsuz-Oyun Год назад

    When the bullets touch the enemy, the bullet passes through them and the enemy does not disappear. Can you help me please?
    Fire shoot code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;
    public class PlayerShoot : MonoBehaviour
    {
    [SerializeField]
    private GameObject _bulletPrefab;
    [SerializeField]
    private float _bulletSpeed;
    [SerializeField]
    private float _timeBetweenShots;
    private bool _fireContinuously;
    private bool _fireSingle;
    private float _lastFireTime;
    // Update is called once per frame
    void Update()
    {
    if (_fireContinuously || _fireSingle)
    {
    float timeSinceLastFire = Time.time - _lastFireTime;
    if (timeSinceLastFire >= _timeBetweenShots)
    {
    FireBullet();
    _lastFireTime = Time.time;
    _fireSingle = false;
    }
    }
    }
    private void FireBullet()
    {
    GameObject bullet = Instantiate(_bulletPrefab, transform.position, transform.rotation);
    Rigidbody2D rigidbody = bullet.GetComponent();
    rigidbody.velocity = _bulletSpeed * transform.up;
    }
    private void OnFire(InputValue inputValue)
    {
    _fireContinuously = inputValue.isPressed;
    if (inputValue.isPressed)
    {
    _fireSingle = true;
    }
    }
    }
    Bullet code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Bullet : MonoBehaviour
    {
    private void OnTriggerEnter2D(Collider2D collision)
    {
    if (collision.GetComponent())
    {
    Destroy(collision.gameObject);
    Destroy(gameObject);
    }
    }
    }

    • @notquiteretro3120
      @notquiteretro3120 Год назад +2

      I had the same problem, in my case it was because I hadn't associated Bullet.cs with the Bullet prefab (I missed the step at 2:36). Because I had already made the bullet prefab (and therefore it wasn't in my Hierarchy), I had to select the bullet prefab in the Project pane, then Add Component and select the Bullet script. Hope this helps!

    • @KetraGames
      @KetraGames  Год назад

      @notquiteretro3120 Thanks for sharing 😊

    • @Lazy_NPC
      @Lazy_NPC Месяц назад

      @@notquiteretro3120 ya ever save a college student's life the day before their project is due? now you have, thanks! :]]

  • @elsheshar999
    @elsheshar999 Год назад

    I have a problem whit the bullet, because when I press the button in my code gets me the next message:
    Severity Code Description Project File Line Suppression State
    Message IDE0051 Private member 'PlayerShoot.OnFire' is unused Assembly-CSharp D:\Unity\Unity Hub\gugueon 2.0\Assets\Scipts\Player\PlayerShoot.cs 54 Active
    Can you help me plis?
    pd: Your tutorials are very good

    • @KetraGames
      @KetraGames  Год назад +1

      Hi, I think this is down to the code analysis rules you have enabled in Visual Studio. You can read more about this rule here - learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0051
      To fix this you can either supress the warning or if you don't want to do that you can make the method public.
      Hope that helps 😊