If you've seen my previous answer, this is sort of a followup on a different subject. I have a character (enemy) made in Blender and exported each individual mesh into a separate .obj file. after doing this, the enemy no longer follows the player. Here is my script:
using UnityEngine;
using System.Collections;
public class ExperimentalEnemy : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public float noChaseRange = 50f;
public int HitBoxRadius;
public ExperimentalDeath Damage;
public float Health = 10f;
private Transform myTransform;
public GameObject Char;
public Animator EnemyAnim;
public GameObject Poof;
public bool isDead;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
isDead = false;
Char = GameObject.FindGameObjectWithTag("Player");
HitBoxRadius = 4;
//Reference to DamageChar from the ExperimentalDeath.cs script
Component Damage = Char.GetComponent();
}
// Update is called once per frame
void Update () {
if (Health < 0.1) {
isDead = true;
EnemyDie ();
}
Debug.DrawLine(target.position, myTransform.position, Color.blue);
if (isDead == true){
Health = 0F;
Destroy (collider);
if(Random.Range(0f, 20f)< 1){
Instantiate (Poof, transform.position, transform.rotation);
}
}
if (isDead != true){
//Look at target
transform.LookAt(target.position);
if(Vector3.Distance(target.position, myTransform.position) > noChaseRange) {
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
if(Vector3.Distance(target.position, myTransform.position) < HitBoxRadius) {
Damage.DamageChar(Random.Range(0.01F, 0.2F));
Debug.Log ("Is Damaging " + Char);
}
}
}
public void EnemyDamage (float amount) {
Health -= amount;
}
public void EnemyDie () {
//if(Random.Range(0f, 10f)< 1){
//}
Destroy (gameObject, 2F);
EnemyAnim.SetBool ("Die", true);
}
void OnTriggerEnter (Collider other){
if (other.gameObject == GameObject.FindGameObjectWithTag ("Bullet")){
Destroy (other, 0.1F);
EnemyDamage(Random.Range(0.1F, 0.2F));
}
}
}
↧