- Home /
clones dying at the same time. why?
i dont know why but all of my clones die at the same time and same thing is happening with the animations; all clones are playing animation at the same time. I have made a prototype project in which i have 3 scripts "DestroyEnemy" where player tries to destroy the enemy with a raycast, "EnemyDeath" in which enemy dies and "MasterSpawnScript" for cloning. i am sharing all 3 scripts with you guys, please tell me where i am doing wrong.
//DestroyEnemy.js
#pragma strict
private var anim : Animation;
private var cameraTransform : Transform;
private var cam : Camera;
static var rayHit = false;
static var enemyName;
var maxHealth = 1000.0;
function Start()
{
cameraTransform = GameObject.Find("FirstPersonCharacter").transform;
cam = cameraTransform.GetComponent.<Camera>();
}
function Update()
{
rayHit = false; //bullet/ray is false after every frame
var hit : RaycastHit;
if (Input.GetButton("Fire1"))
{
var ray : Ray = cam.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction *10, Color.yellow);
if(Physics.Raycast(ray, hit) == true)
{
//Destroy(hit.collider.gameObject);
rayHit = true; //bullet/ray is true when hits enemy
Debug.Log(hit.transform.gameObject.name);
}
else
{
rayHit = false; //bullet/ray is false when hits any other object
}
}
}
//EnemyDeath.js
#pragma strict
var isHitting : boolean;
var startingHealth = 100.0;
private var enemyHealth : int;
//public var ragdoll : GameObject;
function Start ()
{
enemyHealth = startingHealth;
}
function Update ()
{
isHitting = DestroyEnemy.rayHit;
if(isHitting == true)
{
this.enemyHealth -=5;
Debug.Log(this.enemyHealth);
if(this.enemyHealth <=0)
{
Destroy(gameObject);
//Instantiate(ragdoll, gameObject.transform.position, gameObject.transform.rotation);
Debug.Log("working");
}
}
}
//MasterSpawnScript.js
#pragma strict
var spawnPoints : Transform[];
var enemyPrefabs : GameObject[];
var yieldTimeMin = 2;
var yieldTimeMax = 5;
static var enemyCounter = 0;
var spawnXOffSetMin = 0;
var spawnXOffSetMax = 0;
var spawnZOffSetMin = 0;
var spawnZOffSetMax = 0;
var defaultSpawnNumber = 5;
var waveNumber = 1;
var isSpawning = false;
function SpawnEnemies(wave : int)
{
var spawnNumber = (defaultSpawnNumber + 5 * (wave - 1));
isSpawning = true;
for(var i=0; i< spawnNumber; i++)
{
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
var object : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
var position : Transform = spawnPoints[Random.Range(0, spawnPoints.Length)];
var clone = Instantiate(object, position.position +
Vector3(Random.Range(spawnXOffSetMin, spawnXOffSetMax), 0,
Random.Range(spawnZOffSetMin, spawnZOffSetMax)), position.rotation);
clone.name = 'clone'+ (i+1);
enemyCounter++;
}
isSpawning = false;
}
function UpdateWave()
{
waveNumber++;
SpawnEnemies(waveNumber);
}
function Start ()
{
SpawnEnemies(waveNumber);
}
function Update ()
{
if(enemyCounter == 0 && !isSpawning)
{
UpdateWave();
}
}
Are all of the enemies losing health at the same rate? Do they still lose health even if you miss them?
Answer by Eno-Khaon · Feb 08, 2016 at 07:47 PM
The problem lies in your usage of static variable "rayHit"
The variable is static, so there are no instances of it. Every frame, your check for whether an enemy takes damage is based on them checking the state of it:
isHitting = DestroyEnemy.rayHit;
if(isHitting == true)
{
this.enemyHealth -=5;
by making enemy health reduction the first response to this variable, there is no verification of which enemy is actually being hit.
You're testing to ensure you're hitting anything at all, rather than hitting a specific target.
A common convention for handling this is for the attacker (in this case, the player) to fire the Raycast and, if it hits an applicable target, to then pull up the enemy's script and run a function along the lines of:
// Unity JS
// Finding the script and
// Calling the function:
var enemyHealth: EnemyHealthScript;
enemyHealth = hit.transform.gameObject.GetComponent(EnemyHealthScript);
enemyHealthScript.DealDamage(5);
// In your "EnemyHealthScript"
// The damage is actually dealt here.
function DealDamage(amount: int)
{
health -= amount;
}
I've genericized some of the class names in this case, but this should be usable as a starting point, in this case. A common approach is to only pull up information when it's needed (such as waiting to tell an enemy to take damage until you've hit it). Polling every frame with the enemies so they can ask "Have I been hurt?" seems a little silly when, instead, you can simply tell them when you've hurt them.
Your answer
Follow this Question
Related Questions
Enemy not generating... 0 Answers
Enemy randomly disappears on startup 0 Answers
how to kill an emeny? 1 Answer
Enemy not giving damage 2 Answers
Clone Independient Enemies 1 Answer