More shooting options

So, from tutorials on the internet, I have this shooting code:

#pragma strict

var theBullet : Rigidbody;
var Speed = 20;

function Update () {
if (Input.GetMouseButtonDown(0))
{
var clone = Instantiate(theBullet, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));

	Destroy (clone.gameObject, 3);
}

}

What can I do to add
-Mag size and reloading. (But no ammo, as for the type of game I am making, unlimited ammo is best)
-Different impacts and bullet holes, blood for player, dirt for walls and such
-Option for automatic fire
-Accuracy/ cone variable
-Multiple shots at once for shotgunny goodness

While I do not you to be too cryptic or anything, my goal is to learn as well as have it work, so explanation (this works because… blah blah blah) would be greatly appreciated, instead of just HERE YA GO! CODE!

Quickly put this together.

Pretty much, all I did was add in the “size of the clip” and how many bullets are currently in it. With that, you can reload the gun easily, by simply checking if the bullet count is greater than 0. If not, than reload and set the amount of bullets to the “clip size”.

As for auto fire, what I did was add a variable to see whether the gun is firing or not. How it checks is simply if the mouse has been pressed, than the gun is firing. If the mouse has just been un-pressed, than it has stopped firing.

var theBullet : Rigidbody; 
var Speed = 20;
//The max amount of ammo
var ClipSize : int = 24;
//How many bullets are currently in the gun.
var RoundsInClip : int = 24;
//How long it takes to reload
var ReloadTime : float = 1;
//A stopper to make sure you can't reload while already reloading
private var CanReload = true;

//Can it auto fire.
var AutomaticFire = false;
//Is the gun currently auto firing
var Firing = false;
// The wait inbetween shots
var FirePauseTime : float = 0.1;
//Making sure it cant fire super fast.
private var CanFire = true;

function Update () {
/// If r is pressed, and you are not already reloading, the gun will be reloaded
if(Input.GetKeyDown("r") && CanReload == true){
CanReload = false;
ReloadGun();
}
  
//making sure the gun fires when the mouse is clicked
if (Input.GetButtonDown("Fire1") && CanFire == true ) {
if(AutomaticFire==true){
Firing=true;}else{
ShootOneRound();
}
}

//Making sure the gun stops firing when the mouse is not being clicked.
if (Input.GetButtonUp("Fire1")){
Firing = false;
}

if(Firing == true && CanFire ==true){
ShootOneRound();
}
 
}

function ShootOneRound(){
//Making sure the gun doesnt fire super fast.
CanFire = false;

var clone = Instantiate(theBullet, transform.position, transform.rotation);
 clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
     //Removes 1 bullet
     RoundsInClip--;
     
     Destroy (clone.gameObject, 3);
yield WaitForSeconds(FirePauseTime);
CanFire=true;
}

function ReloadGun(){
//Add any reload animations in here or audioclips.
yield WaitForSeconds(ReloadTime);
CanReload = true;
RoundsInClip=ClipSize;
}

In the case of you wanting to add clips, it is very simple. All you would have to do is add in a variable like “ClipCount” and remove one every time you reload.

This is a really good start for you, I have included auto fire, mag size, amount of bullets, reloading, and some more. as for the rest, I suggest you watch youtube videos on how to make an adavanced gun script.

-Player blood can simply just be instantiated when damage is received (Either as a particle effect, or as a screen effect)

-Impact effects (a bullet hitting dirt) could be done with raycasting. Raycast the path the bullet will go, and instantiate a dirt particle effect there.

-Accuracy could be done by everytime before a shot, you rotate the spawn of the bullet a little bit. Thus, having inaccuracy.

-Multiple bullets is trickier, and I suggest watching videos on it “How to make a shotgun script”.

Hope this helps!