- Home /
Enemies not shooting laser on certain level
So in my enemy script, i have where i want the enemy to shoot lasers on certain levels. I got it where it can shoot on level 2, but when it hits like level 3, 4,5,6 and etc. they stop shooting after they get to that level. Here is my script
var level: int;
var nextShot: float = 0;
function Update () {
transform.Translate(Vector3(0,-1* speed * Time.deltaTime ,0));
if ( level == 2 && 3 && 5 && 6 && Time.time > nextShot){
Shoot();
nextShot = Time.time + fireRate;
}
}
If yu can help thank you! this has to be so easy and im really confuse why it was not shooting.
Answer by MrFijiWiji · Jun 14, 2014 at 01:34 AM
From what you're saying, it looks as though you want to begin allowing your enemies to shoot lasers after level 2. If this is indeed the case, the solution is simple.
In your code, where you have:
if ( level == 2 && 3 && 5 && 6 && Time.time > nextShot)
you should replace the above with:
if (level >= 2 && Time.time > nextShot)
This way, if the level int is greater or equal to 2, your enemies will shoot lasers.
However, I realise now that you want to have optional levels in which your enemies can shoot lasers. In this case, the simplest method in which i can think to achieve said behavior would be to have a bool that you can set on a per level basis if you want lasers active. Something like this:
var level: int;
var nextShot: float = 0;
var lasersActive: boolean = false;
function Update () {
transform.Translate(Vector3(0,-1* speed * Time.deltaTime ,0));
if (lasersActive && Time.time > nextShot){
Shoot();
nextShot = Time.time + fireRate;
}
}
Simply set lasersActive to true on the levels in which you would like to allow lasers and that's it.
I don't really understand your 2nd part cause i did try to turn it on true and nothing shot here is my script
if (laserActive && level == 2 && Time.time > nextShot){
laserActive = true;
Shoot();
nextShot = Time.time + fireRate;
}
What you have there won't work because your if statement is checking to see if lasersActive is true before you're setting it to true. If you are loading different scenes for each level, then you can simply set whether or not the lasers are active in the inspector for that particular level by ticking the check box for lasersActive, you wouldn't need the level == 2 section as you're enabling lasers on a per level basis.
If you aren't loading separate scenes for each level, then another method should be used that better deals with an "endless" type of game, but we'll come to that if you're making it in such a way.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Script help please 1 Answer
Trap Door Question 1 Answer
Enemy Spawn script not continuing level 2 Answers
reset level on collision 3 Answers