I have a problem that occurs when ever an enemy dies. what happens is the enemy and all the parts of it decide to go underneath the map, and seemingly disappear. this is annoying, because i want my enemy to fall apart. i do this by using this script, which i attach to all the parts of the enemy:
var snowman : Transform;
var sphere : boolean;
function Update () {
if (snowman.GetComponent(EnemyHealth).dead == true) {
gameObject.AddComponent("Rigidbody");
transform.parent = null;
if (sphere == true) {
gameObject.AddComponent("Sphere Collider");
}
else if (sphere == false) {
gameObject.AddComponent("Capsule Collider");
}
}
}
and the enemy AI code:
var target : Transform;
var damp = 5.0;
var speed = 3.0;
function Start () {
// Auto setup player as target through tags
if (target == null GameObject.FindWithTag("Player"))
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
if (target Vector3.Distance(transform.position, target.position) < 30) {
var rotate = Quaternion.LookRotation(Vector3(target.position.x, transform.position.y, target.position.z) - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var controller : CharacterController = GetComponent(CharacterController);
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * speed);
Debug.Log("I'm Attacking!!!");
}
else if (target Vector3.Distance(transform.position, target.position) > 30) {
Debug.Log("I'm not Attacking!!!");
}
}
