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!
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!
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.
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.
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)
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.
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.
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 😊
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.
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!🥲
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 😊
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 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!
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
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?
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 😊
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 😊
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
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); } } }
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!
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
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 😊
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!
you just saved my life. sincerely, a freshman game design student who's first assignment is a top down shooter
Great to hear 👍😊
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!
Great, thanks for your support 😊
At 12:07, where did you get "gameObject" from? It's not defined anywhere as far as I can see.
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.
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.
My bullet only shoots straight up, is there any way to fix this?
hello. did u by chance fix this issue?
NEW VIDEO!!! I'M SO HAPPY!!!
👍😊
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)
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.
Hi, we're planning to do a follow up soon showing how to cover the scenario where the Zombie is face down
@@KetraGames Cool looking forward to it.
whenever i spress spacebar the bullets kepp going to a different directions how do I fix that?
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.
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 😊
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.
why when the player fires bullets at the enemy, the bullets only penetrate the enemy and the enemy is not destroyed/disappeared
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!🥲
followed the tut line for line, but my fire continuously bool isnt setting to true, any idea how to debug this?
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 😊
Great video, it helped me fix a bug in my code. Legends!
Great to hear, thanks 😊
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?
Hi, is it possible you're setting the velocity of the player rather than the bullet when you shoot?
@@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!
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
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?
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 😊
so can someone tell me why my bullet wont shoots any help pls
have you solved it? I got stuck when we did the bullet prefab
Yes I did it’s the layering change the number from zero to like two in the bullets should show up
Any way to update more frequently?
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 😊
my bullet just goes up
hello, did u by any chance fixed this issue?
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
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);
}
}
}
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!
@notquiteretro3120 Thanks for sharing 😊
@@notquiteretro3120 ya ever save a college student's life the day before their project is due? now you have, thanks! :]]
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
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 😊