Machine Gun Script :)

I need help i have a gun script does any one know how to make it like a machine gun to where you can just hold the mouse button to fire here’s what i got so far thanks

var bullitPrefab:Transform;

function Update()

{

if(Input.GetMouseButtonDown(0))
{
var bullit = Instantiate(bullitPrefab, GameObject.Find("spawn").transform.position,Quaternion.identity);

bullit.rigidbody.AddForce(transform.forward * 50000);
}

}

1 Answer

1

Here is an extension to your code that will shoot as a machine gun. I made a few other changes as well. GameObject.Find() is slow and to be avoided when you can. It can be done once in start. The perShotDelay is the approximate delay between each allowed shot.

#pragma strict
 
var bullitPrefab:Transform;
var perShotDelay = 0.15;

private var timestamp = 0.0;
private var spawn : Transform;

function Start() {
	spawn = GameObject.Find("spawn").transform;
}

function Update() {
	if(Input.GetMouseButton(0) && Time.time > timestamp) {
		timestamp = Time.time + perShotDelay;
		var bullit = Instantiate(bullitPrefab, spawn.position,Quaternion.identity);
		bullit.rigidbody.AddForce(transform.forward * 50000);
	}
}

Thank you I appreciate it a lot this will really help my game