In enemy movement, I used my player as my waypoint to where the enemies are going to. One of my problems with this is that when enemies are approaching near the player, they slow down their movement. I can’t figure out what’s the problem with this yet. Hope anyone could suggest if this has been solved already.
Another thing is my bullet script. In the Proof of Concept tutorial, when the bullet hits the enemy, it will automatically destroy the enemy. What I want to do is have an enemyLives variable with integer value of 3 then destroy the enemy when it’s been hit 3 times. I have done simple codes but it has a problem with the logic. :? Well, I guess I have to post my script now for you to understand my problem well. Here’s the code I attached to the bullet, which I instantiate in the Update function of my player.
var explosion : Transform;
static var enemyLives : int = 3;
function OnTriggerEnter(col : Collider)
{
if (col.gameObject.tag == "Enemy")
{
enemy = col.gameObject;
if(enemy)
{
enemyLives--;
Debug.Log(enemyLives);
}
var tempExplosion: Transform;
Destroy(gameObject);
if (enemyLives <= 0)
{
tempExplosion = Instantiate(explosion, enemy.transform.position, enemy.transform.rotation);
Destroy(enemy);
Destroy(gameObject);
SpikeyScript.playerScore += 1;
enemyLives = 3;
}
}
}
The problem in this code is that, when bullet hits any enemy in the game field, enemyLives–; is being performed. And the last enemy hitted is being destroyed when enemyLives is already 0 even if it has been hit only once. I’m thinking of using array, but I can’t figure out it how. :? I hope any one could help me here…
What happens if you do this is that target will get shorter as the enemy approaches the player. Because the distance traveled by your enemies is length(target) * deltaTime, your enemies slow down as they get near.
This is because you have only one enemyLives counter for all of your enemies. You should probably rethink how your game is setup. Here’s what I would do:
Write a Enemy.js script that handles enemy behavior. This script would have an OnCollisionEnter() that deals with bullets hitting the enemy. It would also have you enemyLives variable!
Attach the Enemy.js script to every enemy in the game (or better: make a prefab for your enemies)
The crux is in that second step you create a unique instance of the Enemy class for every enemy, meaning that each has it’s own enemyLives value that is independent of the other enemies. Problem solved!
private var enemyLives : int = 3;
var explosion : GameObject; //explosion prefab
function OnTriggerEnter(col : Collider)
{
if (col.gameObject.tag == "bullet")
{
bullet = col.gameObject;
enemyLives--;
Destroy(bullet);
}
if (enemyLives <= 0)
{
tempExplosion = Instantiate(explosion, gameObject.transform.position, gameObject.transform.rotation);
Destroy(gameObject);
SpikeyScript.playerScore += 1;
enemyLives = 3;
}
}
Just a thought: At first, I used static var for enemyLives so the same thing happened as my previous code in bullet behaviour. :roll: So I used private var and it worked.