Hold down mouse 0 and every 5 seconds instantiate & fire?

I’ve looked at several scripts but they just wont work. I ofcourse tried my own and changed a bit in the scripts, still no luck on this. It’s probably really simple, but I cant seem to figure out what to do.

I’m looking for something, if you hold down mouse 0, every 5 seconds you hold it down it will fire a shot, and if you release mouse 0 it resets the timer. Here is what I’ve tried to do so far.

#pragma strict

var startTime:float = 0.0;
var ShotSpawn : Transform;
var ChiShot : Transform;

function Update () {
if(Input.GetButtonDown("Fire1"))
{
    startTime = Time.time;
}
if(Input.GetButton("Fire1"))
{
    if (Time.time - startTime >= 5)
    {
    Shot();
         //Instantiate(ChiShot, ShotSpawn.position, ChiShot.transform.rotation);
         startTime = 0;
    }
}
if(Input.GetButtonUp("Fire1"))
{
    startTime = 0;
}

}
function Shot() {
    var pel = Instantiate(ChiShot, ShotSpawn.position, ShotSpawn.rotation);
    pel.rigidbody.AddForce(transform.forward * 8000);
 
 
}

function OnGUI () {
GUI.Label(Rect(10,10,50,25), "" + startTime); 

}

You can do this with what I like to call a ‘time stamp’. That is, you create a time in the future for an event, and you reset that time as appropriate to the situation. Here is a bit of example code. Put it on an empty game object in a new scene and play. Once you’ve verified it works and understand the logic, integrate the logic into your code.

#pragma strict

var waitTime = 5.0;
private var timeStamp = Mathf.Infinity;

function Update() {
	if (Input.GetMouseButtonDown(0)) {
		timeStamp = Time.time + waitTime;
	}
	if (Input.GetMouseButtonUp(0)) {
		timeStamp = Mathf.Infinity;
	}
	
	if (Time.time >= timeStamp) {
		Debug.Log("Firing");
		timeStamp = Time.time + waitTime;
	}
}

Note after ‘firing’, I reset the time stamp to wait. This allows a firing every ‘waitTime’ seconds while the button is held down. If you want to force them to lift and then press the button again, set ‘timeStamp’ to ‘Mathf.Infinity’ instead.

Why do you want it to reset the timer when you release? You want someone to be able to fire faster by clicking rapidly than by clicking and holding? I’m just wondering if that is the intended behaviour because that is what will happen.

Either way according to what you said, this code is how you would do it (Note that if you don’t want to determine how often it shoots by “shots per second” and instead you just want to say “shoot every X seconds”, then remove private from the shotTimer declaration and just set that directly from the Editor and remove every part that says shotsPerSecond. I just find in general defining firing time by “shots per second” is easier :)):

var bullet : GameObject;
var shotsPerSecond : float;
private var shotTimer : float;
private var shotTimerElapsed : float;

function Start ()
{
    shotTimer = 1 / shotsPerSecond;
    shotTimerElapsed = 0;
}
 
function Update ()
{
    shotTimerElapsed = shotTimerElapsed + Time.deltaTime;
    if (Input.GetMouseButtonDown(0) && shotTimerElapsed >= shotTimer)
    {
        shotTimerElapsed = 0;
        // create bullet with whatever position and rotation
        Instantiate(bullet, transform.position, transform.rotation);
    }
    if (Input.GetMouseButtonUp(0))
    {
        shotTimerElapsed = shotTimer;
    }
}

Edit: Nevermind I think I misunderstood your question so this answer doesn’t apply. It’s still a good shoot script though :slight_smile: