- Home /
constant fire (transform)
my script has a way of firing prefabs (source: TornadoTwins) does anyone know how 2 change the following script so that it fires constantly, thank you:
if(Input.GetButtonDown("Fire1"))
{
var bullit = Instantiate(bullitPrefab, GameObject.Find("spawn").transform.position, Quaternion.identity);
bullit.tag = "Smaugus3000laser";
bullit.rigidbody.AddForce(transform.forward * 40000);
}
Answer by FLASHDENMARK · Dec 10, 2010 at 08:02 PM
You could also use invokerepeating:
var rateOfFire = 2.0; var Bullet : Transform;
function Update () { //Fire bullets!!
var rateOfFire = .1;
if (Input.GetButtonDown("Fire1"))
InvokeRepeating("Shoot", .001, rateOfFire);
if (Input.GetButtonUp("Fire1"))
CancelInvoke("Shoot");}
function Shoot ()
{
var Bullet = Instantiate(Bullet,transform.Find("BulletSpawnPoint").transform.position, Quaternion.identity);
}
Make sure that where there stands "BulletSpawnPoint" type your own location of where you want the object to spawn.It needs the exact same name
would this go on its own script or amongst the moving script like the other???
I, my self use it in the same script, but you can also put it in a seperate script. :)
Answer by The_r0nin · Dec 10, 2010 at 07:04 PM
Change GetButtonDown to GetButton. Get Button returns true while the button is held down, GetButtonDown only when it is depressed (you have to release the button to call it again).
Yeah this works, But the problem is that it WILL only update every frame... and that is pretty fast so you got no way of really controlling your "rate of fire", but it works :)
Adding a time check is relatively trivial ( if ((Time.time-savedTime < .5), etc. I was just letting him know that the GetButtonDown would have to be changed before he would get a continuous fire rate from a held key...
i am still new with scripting, i'm getting better though :).... slowly