Machine Gun Automatic Fire in Unity3d

Sharing is caring:
In  this Xenosmash tutorial we’ll show you how to increase the rate of fire to mimic a machine gun rate of fire. We’ll be using “GetButton” so that when we hold down the left click button we’ll have automatic fire based off  the rate of fire set in our variable “fireRate”. We will also edit the “eject” script so that our bullets being ejected match the rate of fire of our weapon. We’ll also add in an audio clip so that once we fire we can hear a gunshot. The link to the audio clip is below. The updated scripts for the automatic fire function are found below. Copy and paste the and apply it to your empty game objects.

Audio Clips

http://soundbible.com/1804-M4A1-Single.html

Javascript Files

 

bulletHoles.js (firing of our weapon)


var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes

var power : int = 10; //provides power to our raycast that can affect our rigidbody components

var fireRate : float = 0.5;

private var nextFire : float = 0.0;

var gunShot : AudioClip;

function Update () {

Fire ();

}

function Fire () {

if(Input.GetButton("Fire1") && Time.time > nextFire) {

nextFire =  Time.time + fireRate;

AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);

ForceFire();

}

}

function ForceFire () {

var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction

var hit : RaycastHit;

Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters

if(Input.GetButton("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something

Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust

// the position and rotation of textures to match the object being hit

if (hit.rigidbody !=null)

hit.rigidbody.AddForceAtPosition(fwd * power, hit.point);  //applies a force to a rigidbody when we click on it. Multiples our forward raycast times our power variable at the position we click

}

}

eject.js (ejecting of our bullet shells)


var bulletCasing : Rigidbody;

var ejectSpeed : int = 10;

var fireRate : float = 0.5;

private var nextFire : float = 0.0;

function Update () {

if (Input.GetButton("Fire1") && Time.time > nextFire) {

nextFire =  Time.time + fireRate;

var clone : Rigidbody;

clone = Instantiate(bulletCasing, transform.position, transform.rotation);

clone.velocity = transform.TransformDirection(Vector3.left * ejectSpeed);

}

}

 

Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:
TwitterYouTube

Comments

  1. EliteSlayer says:

    The script works fine for me except for the raycast. Its shooting out of the back of my gun and I cant see the bullet holes or shoot in the right direction, please let me know how to fix this.

    • Jonathan Gonzalez says:

      It’s working off the Z axis of whatever the script is attached to. Ideally you’d want to use an empty game object and place it directly in front of the barrel of your gun. Make sure the Z axis of the empty game object is pointing in the direction that you’d be shooting. If you don’t see bullet holes it could be a number of things, but more than likely your raycast isn’t detecting any colliders. First fix the axis issue and see if it clear it up. If you were firing “backward” then that’s where the bullets would show up too.

  2. Brenden Reeds says:

    Is there a way to have it emit particles instead of the bullet hole images?

    • Yes there is. I’m actually working on a tutorial, but basically replace the bullet hole texture portion of the code with a particle system. Make sure the particle system you use is fairly short. Good luck!

  3. anonymous says:

    This does not work at all for me. I used only the BulletHoles.JS script, and assigned it to my gun, but IT WILL NOT WORK!! Please help me and let me know what to do.

    • It’s not applied to the gun, it’s applied to an empty gameObject directly in front of the barrel of your weapon. Same thing with the ejection script, which is applied to an empty gameObject placed near the ejection slot of your weapon. Make sure both of these empty gameObjects have the Z direction pointing in the correct position as that is the direction being fired.

Speak Your Mind

*