- Home /
Question by
allfader · Feb 07, 2013 at 09:04 PM ·
javascriptgunfire
Rapid fire to Instantiated prefab
I have a very basic gun script shooting 10 projectiles, but the problem is that it fires them all at once. I need some kind of rapid fire effect, so there is a short pause between each shot.
How do I do this.
//The script so far
var BulletPrefab:Transform;
var reloadTime = 0.5;
var ammoCount = 20;
private var lastShot = -10.0;
function Update()
{
if(Input.GetButtonDown("Flare"))
if (Time.time > reloadTime + lastShot && ammoCount > 0) {
for (var i : int = 0;i < 10; i++)
{
var bullet = Instantiate(BulletPrefab,
GameObject.Find("FlareSpawnPoint").transform.position,
GameObject.Find("FlareSpawnPoint").transform.rotation);
lastShot = Time.time;
ammoCount--;
}}}
Comment
Best Answer
Answer by paulaceccon · Feb 07, 2013 at 09:19 PM
Try this:
var BulletPrefab:Transform;
var reloadTime = 0.5;
var ammoCount = 20;
function Update()
{
if(Input.GetButtonDown("Flare"))
{
StartCoroutine("Shot");
}
}
IEnumerator Shot()
{
for (var i : int = 0; i < 10; i++) {
var bullet = Instantiate(BulletPrefab, GameObject.Find("FlareSpawnPoint").transform.position, GameObject.Find("FlareSpawnPoint").transform.rotation);
ammoCount--;
yield WaitForSeconds(reloadTime);
}
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
How do I prevent audio from restarting if it's being asked to play again? 2 Answers
"WARNING: Boolean expression will always have the same value." 0 Answers
how to create a gun in unity3d 2 Answers
Is there a script out there that actully works for shooting bullets? 2 Answers