- Home /
Lasers Fire over and over
I have a program that randomly chooses a number between 1 and 3, and therefore chooses an enemy to fire a laser. But sometimes when an enemy is picked, nobody else shoots but him. randomNumber doesn't change. Please Help! I have no idea and I've tired over this for days! P.S nextShot and period is my way of creating a pause in the update function. It might be the problem but once again, I don't know! P.P.S in another script are the commands on where the laser moves. @aldonaletto
function Update ()
{
if(evilEntity1Fire == true)
{
if(Time.time > nextShot)
{
nextShot = Time.time + period;
var laserPrefab1 : GameObject;
laserPrefab1 = Instantiate(laser, evilEntity1.transform.position, evilEntity1.transform.rotation);
evilEntity1Fire = false;
}
}
if(evilEntity2Fire == true)
{
if(Time.time > nextShot)
{
nextShot = Time.time + period;
var laserPrefab2 : GameObject;
laserPrefab2 = Instantiate(laser, evilEntity2.transform.position, evilEntity2.transform.rotation);
evilEntity2Fire = false;
}
}
if(evilEntity3Fire == true)
{
if(Time.time > nextShot)
{
nextShot = Time.time + period;
var laserPrefab3 : GameObject;
laserPrefab1 = Instantiate(laser, evilEntity3.transform.position, evilEntity3.transform.rotation);
evilEntity3Fire = false;
}
}
if(randomNumber == 1)
{
Robot1();
}
if(randomNumber == 2)
{
Robot2();
}
if(randomNumber == 3)
{
Robot3();
}
}
function Robot1 ()
{
yield WaitForSeconds (difficultyWait);
evilEntity1Fire = true;
randomNumber = Random.Range(1,3);
}
function Robot2 ()
{
yield WaitForSeconds (difficultyWait);
evilEntity2Fire = true;
randomNumber = Random.Range(1,3);
}
function Robot3 ()
{
yield WaitForSeconds (difficultyWait);
evilEntity3Fire = true;
randomNumber = Random.Range(1,3);
}
Answer by Tohaveaname · May 18, 2016 at 02:10 PM
I don't use JavaScript so I apologise for any potential syntax errors on my part but try this:
function Update ()
{
if(Time.time > nextShot) // Stopped checking for this multiple times as it was unecessary.
// Not entirely sure what it's doing but you said you need it for
// pause functionality so I left it alone.
{
nextShot = Time.time + period; // Again not entirely sure what this is doing
// but you said you needed it.
if(evilEntity1Fire == true) // Changed to an else if block as it seems two robots
// can't shoot at the same time.
{
var laserPrefab1 : GameObject;
laserPrefab1 = Instantiate(laser, evilEntity1.transform.position, evilEntity1.transform.rotation);
evilEntity1Fire = false;
}
else if(evilEntity2Fire == true)
{
nextShot = Time.time + period;
var laserPrefab2 : GameObject;
laserPrefab2 = Instantiate(laser, evilEntity2.transform.position, evilEntity2.transform.rotation);
evilEntity2Fire = false;
}
else if(evilEntity3Fire == true)
{
var laserPrefab3 : GameObject;
laserPrefab1 = Instantiate(laser, evilEntity3.transform.position, evilEntity3.transform.rotation);
evilEntity3Fire = false;
}
}
Invoke("Robot", difficultyWait);
}
function Robot ()
{
randomNumber = Random.Range(1,4); // The documentation states "The returned value will
// never be max" so 3 wouldn't have ever occurred.
// I also suspect this does not need to be a class
// variable just a local.
if(randomNumber == 1)
{
evilEntity1Fire = true;
}
else if(randomNumber == 2)
{
evilEntity2Fire = true;
}
else if(randomNumber == 3)
{
evilEntity3Fire = true;
}
}
I have tried to tidy up your code as well. I hoped this helped.
Thanks for trying, Tohaveaname, but the problem is still there. Ive discovered that when I have the nextShot and Time.time stuff in the script, it does what I want in speed terms but after a while it eventually ends up with just the first evilEntity firing forever. If i get rid of the nextShot stuff, every evilEntity fires forever at a rapid pace. Thankyou for trying though
Answer by $$anonymous$$ · May 23, 2016 at 09:02 PM
EDIT : I fixed it! Thanks Tohaveaname, your code was awesome. The problem that we both ovelooked was evilEntityfFire(x) not turning off. I just moved all the statements saying evilEntityFire(x) = False outside the if statements and just inside the Update() Function. Thanks so much for your help though Tohaveaname, that was the only thing I changed. Wooh! :D
P.S Thankyou
function Update ()
{
if(Time.time > nextShot)
{
nextShot = Time.time + period;
if(evilEntity1Fire == true)
{
var laserPrefab1 : GameObject;
laserPrefab1 = Instantiate(laser, evilEntity1.transform.position, evilEntity1.transform.rotation);
}
else if(evilEntity2Fire == true)
{
nextShot = Time.time + period;
var laserPrefab2 : GameObject;
laserPrefab2 = Instantiate(laser, evilEntity2.transform.position, evilEntity2.transform.rotation);
}
else if(evilEntity3Fire == true)
{
var laserPrefab3 : GameObject;
laserPrefab1 = Instantiate(laser, evilEntity3.transform.position, evilEntity3.transform.rotation);
}
}
evilEntity1Fire = false;
evilEntity2Fire = false;
evilEntity3Fire = false;
Invoke("Robot", difficultyWait);
}
function Robot ()
{
randomNumber = Random.Range(1,4);
if(randomNumber == 1)
{
evilEntity1Fire = true;
}
else if(randomNumber == 2)
{
evilEntity2Fire = true;
}
else if(randomNumber == 3)
{
evilEntity3Fire = true;
}
}
Your answer