controlled firing?

I am trying to create a harpoon gun for an underwater game and I don’t want it to be able to fire quickly. I would like a delay of about 2 seconds between the time when players can shoot. If at all possible, I would like the time to be defined as a variable so I can adjust it in the engine GUI and not have to dive into the code. Here is my code so far:

var throwSound : AudioClip;
var harpoonObject : Rigidbody; 
var throwForce : float; 

private var canShoot : boolean = true;

function Update () {
	
	if(Input.GetButtonUp("Fire1")){
		//audio.PlayOneShot();
		var newHarpoon : Rigidbody = Instantiate(harpoonObject, transform.position, transform.rotation); 
		newHarpoon.name = "harpoon"; 
		newHarpoon.rigidbody.velocity = transform.TransformDirection(Vector3(0,throwForce,0)); 
		Physics.IgnoreCollision(transform.root.collider, newHarpoon.collider, true);
	}
	
}	

	@script RequireComponent(AudioSource)

Ignore the audio things…those are commented out so I can put audio in later. I just want this thing up and running now.

There are many ways to do this. I like to set up a float and control it with deltaTime and have it reset its self in a short time like

var throwForce : float;
var timeTrigger : float;
var throwSound : AudioClip; 
var harpoonObject : Rigidbody; 
private var canShoot : boolean = true;
//==============//
function Update() {
timeTrigger += 1.0*Time.deltaTime;  
if(timeTrigger >= 20) {
  timeTrigger = 0.0;
  }
if((Input.GetButtonDown("Fire"))(timeTrigger >=0.0)(timeTrigger <=5)){ 
//audio.PlayOneShot(); 
var newHarpoon : Rigidbody = Instantiate(harpoonObject, transform.position, transform.rotation); 
newHarpoon.name = "harpoon"; 
newHarpoon.rigidbody.velocity = transform.TransformDirection(Vector3(0,throwForce,0)); 
Physics.IgnoreCollision(transform.root.collider, newHarpoon.collider, true); 
   } 
}    
@script RequireComponent(AudioSource)

Its also fun to insert vars for the condition to adjust the window that the the gun can be fired :smile:

Thanks but I’m somewhat new to coding and don’t really know where all that would go in what I already have…can you help me out?

I Updated it and put it together. I cant stand trying to write examples inside a little topic box, so i hope i got it right-lol but if not i think its close :smile:

I added it and it seems to kind of work…but isn’t quite giving me what I want. The time you seem to have put in is 20 seconds…way too long. Whenever I try to adjust the numbers in your code I can either fire infinitely or not fire at all.

I just want it to fire at a steady pace of 1 shot every 2 or 3 seconds so the player can’t just keep hitting the mouse button to fire quickly.

This should work quickly put it together also doesnt have to update everyframe.

        float timeLastFired = Time.time;
        float timeDelayPerShot = 2;
        if (timeLastFired >= timeLastFired + timeDelayPerShot)
        {
            print("shoot and reset");
            timeLastFired = Time.time;
        }

Again…I’m sorry but I don’t really know where to put that in my code…I really am new to all of this…

Just set the 20 to 4 and the 5 to 2 and that will be every two seconds or so :smile: Unless you use a CoRoutine and Invoke the shot every two seconds. What ever style you go with when creating a delay it wont fire every time you hit the L-click or that would’nt be a delay. Besides a harpoon/spear gun does take a short time to reload-lol :smile:

Okay I think I know what is going on with your code mantis. You are indeed giving me a 2 second delay, however, the other two seconds where it can fire allows me to fire unlimitedly. How can I make it so it’s 1 shot every 2 seconds? How do I run coroutines?

Put this with your other variables:

private var startTime;
var endTime;

You want to start a timer when it is shot. Put this after the fire button is pressed (in its braces):

canShoot = false;
startTime = Time.time;

Put this outside of the “Fire1” if statement braces:

if((Time.time - startTime) > endTime))
canShoot = true;

Finally change your "if(Input.GetButtonUp(“Fire1”)) to:

if(Input.GetButtonDown(“Fire1”) canShoot))

In the editor change endTime to how much you want to delay the shot.

Okay so I added everything and that fixed it…THANKS!

Figured I’d just hijack this thread instead of new one since it’s relative.

I’m using this (basic fire script with Zer033x’s code dropped in.

The issue is it only fires once, and Zer033x says to change the endTime in editor. But I don’t see anywhere to change it.

//fire bullets prefab
var newObject : Transform;

private var startTime;
var endTime;
private var canShoot : boolean = true;


function Update () {
	//fire bullets works
	if(Input.GetButtonDown("Fire1") canShoot) 
	{
		Instantiate(newObject, transform.position, transform.rotation);
		Debug.Log("Cube created");
		
		canShoot = false;
		startTime = Time.time;

	}
//timed shot
	if((Time.time - startTime)> endTime)
		canShoot = true;

}

You don’t have to change it in the editor, just change it in the script.

see “var endTime”

change that to var endTime = x;

In order for it to show up in the editor I think you have to do var endTime : float;