- Home /
whats wrong with my enemy respawn script????
when the enemy respawns the scripts attached to it get unchecked and dont work heres my script on the enemy #pragma strict
var health = 100;
var respawn : Transform;
var enemy : Transform;
function TakeDamage (damage : float)
{
if (health <= 0)
{
Destroy(gameObject);
return;
}
health -= damage;
if (health <= 0)
{
Destroy(gameObject);
health += 100;
var respawnedEnemy : Transform = Instantiate( respawn, enemy.transform.position, enemy.transform.rotation );
//respawnedEnemy.velocity = transform.TransformDirection(Random.Range(-2, 2) - 3.0f, Random.Range(-1, 2) + 3.0f, -Random.Range(-2, 2) + 1.0f);
Physics.IgnoreCollision( respawnedEnemy. collider, transform.root.collider );
}
}
and my script on the bullet #pragma strict
var damage : int = 25;
function Start () {
Destroy (gameObject, 4);
}
function OnCollisionStay (theObject : Collision)
{
if(theObject.gameObject.tag=="Enemy")
{
theObject.gameObject.GetComponent(EnemyHealth).TakeDamage(damage);
}
}
Answer by Skalde · Jun 11, 2013 at 11:23 PM
Instead of using the prefab as the "Enemy : Transform" i would suggest that you create a new enemy Away from your scene your terrain, and uses that because, Prefabs doesnt check the variables when its created.. its a wierd system but thats how i did it ^^
did this make sense??
the problem is it isnt a prefab i updated the question though with a picture of whats happening
Strange.. when i did this, i made the enemy instatiate an "emty gameobject" i called "Respawner" and put a script on it that yield for 300 Seconds and then instatiated the enemy. then i put a "Enemy doll" and a "Respawner" in the scene far away from the Terrain and instatiated from them..
try that?
never$$anonymous$$d i got it working all i had to do was add my other scripts on the enemy into one big script then make that script enabled when the enemy is respawned #pragma strict
var moveSpeed : int;
var turnSpeed : int;
var health : int = 200;
var respawn : Transform;
var target : Transform;
var projectile : EnemyHealth;
function TakeDamage (damage : int)
{
health -= damage;
if (health <= 0)
{
Destroy(gameObject);
return;
}
health -= damage;
if (health <= 0)
{
Destroy(gameObject);
health += 200;
RespawnEnemy();
}
}
function RespawnEnemy()
{
var respawnedEnemy : EnemyHealth;
respawnedEnemy = Instantiate(projectile, respawn.transform.position, respawn.transform.rotation);
respawnedEnemy.turnSpeed = 10;
respawnedEnemy.moveSpeed = 10;
respawnedEnemy.enabled = true;
var other : EnemyHealth = gameObject.GetComponent(EnemyHealth);
//respawnedEnemy.velocity = transform.TransformDirection(Random.Range(-2, 2) - 3.0f, Random.Range(-1, 2) + 3.0f, -Random.Range(-2, 2) + 1.0f);
Physics.IgnoreCollision( respawnedEnemy. collider, transform.root.collider );
}
function OnGUI()
{
GUI.Box(Rect(Screen.width - 300, 40, 100, 80), "");
GUI.Label(Rect(Screen.width - 300, 70, 90, 20), "health:" + health);
}
// follow our player
function Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update()
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), turnSpeed * Time.deltaTime);
if(Vector3.Distance(transform.position, target.position) > 10)
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
How do I make my player re-spawn in its original place on collision of enemy? 1 Answer
I need help creating a ZombieAI for my Walking Dead Game 0 Answers
How kill an enemy and respawn after die 2 Answers
How to get your player to respawn when in void (C#) 2 Answers
FPS simultaneous animation..... newbie. 0 Answers