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.
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
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.
Just set the 20 to 4 and the 5 to 2 and that will be every two seconds or so 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
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?