- Home /
Enemy behaviour in SHMUP
I have a behaviour script for an enemy, however it seems to ignore my if statement conditional in the update function. The enemy starts with enemyAlive = false. If it enters the world bounds, its Alive becomes true. In Update I ask are you alive if so activate your movement and firing patterns. The enemy ignores this and activates its movement and firing patterns while its Alive = false.
pragma strict
private var enDir : Vector2;
private var xSpeed : Number = 1;
private var ySpeed : Number = 10;
private var placeSpeed : Number = 1;
private var enemyAlive : boolean = false;
public var preEnemyBullet : GameObject;
function Update ()
{
//check if enemy is within the screen
if (enemyAlive == true)
{
//check if the enemy has touched its min or max height
if (this.transform.position.y <= -16 || this.transform.position.y >= 16)
{
//reverse the diection of the enemy and begin shooting
CancelInvoke("Shoot");
enDir.y *= -1;
InvokeRepeating("Shoot", 1.0 , 2.0);
}
//move the enemy while on screen
this.transform.Translate(enDir * ySpeed * Time.deltaTime);
this.transform.Translate(Vector2.one * xSpeed * Time.deltaTime);
}
else
{
//move the enemy while off screen
this.transform.Translate(Vector2.one * placeSpeed * Time.deltaTime);
}
}
function Shoot ()
{
//Instantiate the enemy's bullet
Instantiate(preEnemyBullet , this.transform.position , Quaternion.identity);
}
function OnTriggerEnter ( col : Collider )
{
if (col.tag == "WorldBounds")
{
//active the enemy when it enters the screen
enemyAlive = true;
}
}
Your answer
Follow this Question
Related Questions
How to: Do a camera that is top Down/Isometric 5 Answers
Enemy fire script 1 Answer
Standart Enemy Melee 3 Answers
how to check enemy presence(or part of its body) on player's screen? 1 Answer
How to make an enmy and an AI 2 Answers