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

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ธ.ค. 2024

ความคิดเห็น • 46

  • @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

    • @KetraGames
      @KetraGames  ปีที่แล้ว +1

      Great to hear 👍😊

  • @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 😊

  • @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.

  • @wouterraemaekers6136
    @wouterraemaekers6136 ปีที่แล้ว +1

    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.

  • @kngt9655
    @kngt9655 ปีที่แล้ว +3

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

    • @ix3669
      @ix3669 หลายเดือนก่อน

      hello. did u by chance fix this issue?

  • @nogrilodoguede7515
    @nogrilodoguede7515 ปีที่แล้ว +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)

  • @GodofGamesss
    @GodofGamesss ปีที่แล้ว +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  ปีที่แล้ว

      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 9 หลายเดือนก่อน

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

  • @ix3669
    @ix3669 หลายเดือนก่อน

    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 😊

  • @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.

  • @FznAmn
    @FznAmn ปีที่แล้ว

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

  • @JiaguoLiu
    @JiaguoLiu 2 วันที่ผ่านมา

    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!🥲

  • @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 😊

  • @seandobson2682
    @seandobson2682 ปีที่แล้ว

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

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Great to hear, thanks 😊

  • @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 6 หลายเดือนก่อน

    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 11 หลายเดือนก่อน +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  11 หลายเดือนก่อน +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 10 หลายเดือนก่อน

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

    • @astout6494
      @astout6494 10 หลายเดือนก่อน

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

  • @xaviers5781
    @xaviers5781 ปีที่แล้ว +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 หลายเดือนก่อน

      hello, did u by any chance fixed this issue?

    • @unston
      @unston หลายเดือนก่อน

      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 11 หลายเดือนก่อน

    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 11 หลายเดือนก่อน +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  11 หลายเดือนก่อน

      @notquiteretro3120 Thanks for sharing 😊

    • @Lazy_NPC
      @Lazy_NPC 9 วันที่ผ่านมา

      @@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 😊