I’m using a script for autofire that I found in unity’s script reference. I works great but only as an autofire! If I press the button again it doesn’t fire till the time for the next fire pass. So, I put also a GetButtonDown which does the job only tha instead of 1 bullet, instantiates 2 (expected to happen!). I tried to avoid this thing by adding a firstshot variable set to true or false depending on the key being down or not, but it didn’t work. Here’s the code:
// Instantiates a projectile every 0.5 seconds,
// if the Fire1 button (default is Ctrl) is pressed.
var missile : GameObject;
var fireRate = 0.5;
private var nextFire = 0.0;
/*var firstshot = true;*/
function Update () {
if (Input.GetButton ("Fire1") Time.time > nextFire /* firstshot == false*/) {
nextFire = Time.time + fireRate;
Instantiate (missile, transform.position, transform.rotation);
}
if (Input.GetButtonDown ("Fire1")) {
/*firstshot = true;*/
Instantiate (missile, transform.position, transform.rotation);
}
/*if (Input.GetButtonUp ("Fire1")) {
firstshot = false;
}*/
}
It must be simple but I couldn’t work it out. Any ideas??
// Instantiates a projectile every 0.5 seconds,
// if the Fire1 button (default is Ctrl) is pressed.
var missile : GameObject;
var fireRate = 0.0;
private var nextFire = 0.0;
/*var firstshot = true;*/
function Update () {
if (Input.GetButton ("Fire1") Time.time > nextFire ) {
Instantiate (missile, transform.position, transform.rotation);
}
}
This doesn’t work though… it just instantiates many many many missiles while i keep the fire button down.
I am trying to make a script that has the autofire in a normal pace (every half second) but also instantiate a missile every time the button is tapped, if the player chooses not to use autofire.
You need to set nextFire every frame, otherwise a missile will be instantiated every frame no matter what:
// Instantiates a projectile every 0.1 seconds,
// if the Fire1 button (default is Ctrl) is pressed.
var missile : GameObject;
var fireRate = 0.1;
private var nextFire = 0.0;
function Update () {
if (Input.GetButton ("Fire1") Time.time > nextFire ) {
Instantiate (missile, transform.position, transform.rotation);
nextFire = Time.time + fireRate;
}
}
b) fire repeatedly in a fixed time interval, if the player holds the firebutton down.
As it is now it seems fine to the naked eye. It’s not so obvious that it creates two missiles. But when I add a missile sound effect (with playatclipPoint or PlayOneShot), you can notice easy that the sound does not reflect to what is going on.
The script I posted will fire if the player taps the button, and continue to fire at the given rate if the player holds the button down. Do you want something more complicated than this?
This script of yours works only with 0.1 fire rate and that’s because most of the times you cannot tap the button quicker than 0.1 seconds. I want to have around 0.5 - 0.7 secs fire rate for the auto-fire (while the button is pressed) and at the same time to fire as I like by tapping the fire button.
I was thinking having two functions, one for tapping and one for holding down and switch between them whenever the key is up. But it doesn’t work.
The other thought is to start the autofire function if the key is held down for a certain amount of time, like 0.7 or 1 second. Because now the GetButtonDown and the GetButton start working immedietly when the button is pressed. So, the script will have a GetButtonDown for tapping the button and fire, and for the auto-fire a GetButton with an if (button is being held for more than a second) { start the autofire in a fixed given rate}. something like that.
Is it possible to calculate the time the button is being held???
Not sure if you’ve received your answer yet, but I noticed the reason you get a double fire is because the first time the button is pressed, both Input.GetButton(“Fire1”) and Input.GetButtonDown(“Fire1”) are TRUE. Granted you check to make sure that Time.time > nextFire, however, nextFire is set to 0.0 during the creation of the script, so that will always be true the very first time the Fire1 button is pressed. My solution was to change the nextFire during the initial press of the Fire1 button. Here is my code:
var fireRate = 0.5;
private var nextFire = 0.0;
private var i = 1;
function Update() {
if(Input.GetButtonDown("Fire1")) {
Debug.Log("Fire1nce: " + i);
nextFire = Time.time + fireRate;
i++;
}
if(Input.GetButton("Fire1")) (Time.time > nextFire)) {
Debug.Log("Fire2ice: " + i);
nextFire = Time.time + fireRate;
i++;
}
}
It took me a while to figure out what you needed, but I believe this is what you are asking for.
It was a good problem for me to solve as this is something that I might need to know in the near future.
Yes, I had realized that the very first moment I wrote the script and tried to fix it by setting this firstshot variable to change true or false when the key was up. Unforunately with no results.
But you gave the solution.
It works just great!!
Thanx very much for your effort!! First post for you!!
!HUG!
And sorry everyone for not being so clear of what was happening and what I was trying to achieve. English is not my first language but I’m trying to get better, like in Unity!!