Uploaded October 2020 | Updated September 2026, 2 weeks ago
#AmongUs #UnityFire #UnityWeapon
Episode 3. Fire Pistol And Shotgun Switching Them Back And Forth.
I play games here - twitch.tv/zzotoff
You are very welcome :-)
Previous video
Give Among Us A Weapon Pistol & Shotgun | Game Clone Made In Unity Software | Weapon Switch Feature youtu.be/nDAZIKNx0wg
Pre-previous video
Among Us Idle And Run Animation Unity Software Tutorial | Top Down Movement In 2D Unity Game youtu.be/W3-EncfpBZM
Object pooling system video youtu.be/Mq2zYk5tW_E
Bullet sprite here drive.google.com/file/d/1zdSVbN8ImWjn6JERyRHbC1sigUX9QUTQ/view?usp=sharing
Hi!
In a previous video I gave this spaceman a pistol and a shotgun. Now I’ll give him some bullets and he will be able to shoot them from his guns.
Here is my project with a new bullet sprite and a couple of new scripts. Let’s put it all together. First of all I need to rename my pistol and shotgun prefabs adding this underscore symbol to their names. I need it to determine which weapon player is holding at the moment and which fire pattern will be used. Ok. next step is to add a barrel game object to the pistol and shotgun prefabs. This barrel gameobject will be used as a spawning point for bullets. I bring a pistol and shotgun to the scene and give both of them a child gameobject named a barrel. Barrel will be positioned at the tip of the gun. And it should be rotated by -90 degrees in the Z axis because bullets will be sent along the green axis. Same for shotgun. Once a prefab is edited I should apply the changes. Pistol and shotgun will use fire script. I D&D it into them. Here is this script.
Variables.
In the start method I get the name of a weapon which is used at this moment. In Update if the fire button is pressed weapon fire method is invoked which gets this weapon name as a parameter. And in this method if a pistol is used then one bullet is spawned and is sent along the green axis. If the current weapon is a shotgun then three bullets are spawned where each bullet gets its own direction. First bullet goes a bit downwards, the second bullet goes straight forward and the third bullet goes a bit upwards. So I have a kind of shotgun spread here. That’s the script.
So the script is attached and I have to D&D bullet prefab and barrel gameobject to their slots of this script. Bullet should go here. But I don’t have a bullet yet. Lets create it. I D&D bullet sprite to the scene. Here it is. Bullet needs a collider to detect collisions later. It should be a trigger. Also the bullet needs a rigidbody 2d. Gravity scale is zero. And collision detection is continuous. I D&D bullet into the prefabs folder creating a prefab of it. Bullet form hierarchy can be deleted. Now I can fill fire script slots with gameobjects. Select pistol. Bullet goes here. And the pistol barrel goes here. Same for shotgun. Bullets go here. Shotguns barrel goes here. Apply changes to prefabs. Pistol and shotgun are ready to go. Delete them from hierarchy. One little script for bullets. This script just destroys a bullet after 3 seconds. Of course this is not a good approach to instantiate and destroy bullets. You should use an object pooling system for that. I have a video about it. A link will be in the description. Almost done. I hit play to test it and once I press the Q button to select a weapon I get an error. It happens because I forgot to add an underscore symbol to the pistol and shotgun in weapon control script. Here. What a stupid mistake. Done. Now it should work just fine. Great! Hope you like this tutorial. Thank you for watching and see you next time.
#AmongUs #UnityFire #UnityWeapon
Episode 3. Fire Pistol And Shotgun Switching Them Back And Forth.
I play games here - twitch.tv/zzotoff
You are very welcome :-)
Previous video
Give Among Us A Weapon Pistol & Shotgun | Game Clone Made In Unity Software | Weapon Switch Feature youtu.be/nDAZIKNx0wg
Pre-previous video
Among Us Idle And Run Animation Unity Software Tutorial | Top Down Movement In 2D Unity Game youtu.be/W3-EncfpBZM
Object pooling system video youtu.be/Mq2zYk5tW_E
Bullet sprite here drive.google.com/file/d/1zdSVbN8ImWjn6JERyRHbC1sigUX9QUTQ/view?usp=sharing
Hi!
In a previous video I gave this spaceman a pistol and a shotgun. Now I’ll give him some bullets and he will be able to shoot them from his guns.
Here is my project with a new bullet sprite and a couple of new scripts. Let’s put it all together. First of all I need to rename my pistol and shotgun prefabs adding this underscore symbol to their names. I need it to determine which weapon player is holding at the moment and which fire pattern will be used. Ok. next step is to add a barrel game object to the pistol and shotgun prefabs. This barrel gameobject will be used as a spawning point for bullets. I bring a pistol and shotgun to the scene and give both of them a child gameobject named a barrel. Barrel will be positioned at the tip of the gun. And it should be rotated by -90 degrees in the Z axis because bullets will be sent along the green axis. Same for shotgun. Once a prefab is edited I should apply the changes. Pistol and shotgun will use fire script. I D&D it into them. Here is this script.
Variables.
In the start method I get the name of a weapon which is used at this moment. In Update if the fire button is pressed weapon fire method is invoked which gets this weapon name as a parameter. And in this method if a pistol is used then one bullet is spawned and is sent along the green axis. If the current weapon is a shotgun then three bullets are spawned where each bullet gets its own direction. First bullet goes a bit downwards, the second bullet goes straight forward and the third bullet goes a bit upwards. So I have a kind of shotgun spread here. That’s the script.
So the script is attached and I have to D&D bullet prefab and barrel gameobject to their slots of this script. Bullet should go here. But I don’t have a bullet yet. Lets create it. I D&D bullet sprite to the scene. Here it is. Bullet needs a collider to detect collisions later. It should be a trigger. Also the bullet needs a rigidbody 2d. Gravity scale is zero. And collision detection is continuous. I D&D bullet into the prefabs folder creating a prefab of it. Bullet form hierarchy can be deleted. Now I can fill fire script slots with gameobjects. Select pistol. Bullet goes here. And the pistol barrel goes here. Same for shotgun. Bullets go here. Shotguns barrel goes here. Apply changes to prefabs. Pistol and shotgun are ready to go. Delete them from hierarchy. One little script for bullets. This script just destroys a bullet after 3 seconds. Of course this is not a good approach to instantiate and destroy bullets. You should use an object pooling system for that. I have a video about it. A link will be in the description. Almost done. I hit play to test it and once I press the Q button to select a weapon I get an error. It happens because I forgot to add an underscore symbol to the pistol and shotgun in weapon control script. Here. What a stupid mistake. Done. Now it should work just fine. Great! Hope you like this tutorial. Thank you for watching and see you next time.










