#shorts #unity #shooting
How to shoot a bullet in Unity
So you want to start shooting in Unity. First you're going to need a gun. The gun needs a script, if it's your character use that.
Next, we need a bullet. Make a prefab with the bullet mesh. The bullet needs a script, a Collider, and a Rigidbody. Open that script and let's code.
For the bullet, let's add a speed, and a reference to our components. In Update, we want to tell the rigidbody to go forward, so we multiply forward by speed and deltaTime.
Let's make it do something when it hits. For now, it'll get rid of itself.
Alright, moving on to the gun script. We need a reference to the bullet and to where we want to shoot from. Jumping back to Unity, let's drag our bullet prefab into it's field, and make an empty for the spawn point, just in front of the muzzle. Make sure there's enough space the bullet will hit the gun!
In the gun's update, let's check if the player is clicking, and, if so, let's fire. Instantiate the bullet by referencing the bullet, the point we want to spawn from, and then a quaternion saying where we are pointing.
And, we are done!