- Home /
Enemy not following player.
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<ExperimentalDeath>();
}
// 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));
}
}
}
What do you mean by "exported each individual mesh"? How many meshes does your enemy have? Is there animation involved? Are there any error messages? Have your tried inserting Debug.Log lines to check where exactly the script is not working as intended?
there is a mesh for each body part (head, chest, legs x2 and arms x2) as a pose to exporting the whole thing as a blender file. my enemy follows the player and when near enough, goes into the player's script and calls for damageChar which subtracts health from the player
I noticed that the problem is the animation. when i disable the animator the character moves fine (but the poof gameObject doesn't appear for some reason). i tried deleting all of the frame keys but it still didn't move. i (obviously) need the animation for the game sooooo..
Answer by dvora · Aug 17, 2014 at 10:32 PM
you can create a single parent for all the meshes and use the: transform.position=Vector3.MoveTowards(transform.position,target.position, speed);
Answer by TheShadyColombian · Aug 17, 2014 at 10:03 PM
i Fixed it! (yayyy!!!!) to fix it, i duplicated the empty that all the meshes are parented to and has all the components, and i parented it to the duplicate. for the one parented to the duplicate, remove every component but the animator, and the root one, keep everything but the animator. i still have one problem; the blootSplatter object and Poof object don't appear. (can i post this as an answer, BTW?)
Your answer
Follow this Question
Related Questions
Enemy Targetting Help 1 Answer
Enemy Follow Issue 3 Answers
Enemy Script 2 Answers
make enemy attack 0 Answers
Script to make an enemy follow the player when they trigger a door? 0 Answers