- Home /
Question by
DeathbyDomination · Nov 11, 2017 at 03:51 PM ·
instantiateanimatornot working
Animator not playing proper animations when Instantiating multiples of the same Prefab
When I instantiate multiples of the same prefab the animator gets screwy and won't play the right animation under the right circumstances. For Example when their health reaches zero they continue the "isWalking" bool and after awhile they will start playing the "isDying " bool before any damage is dealt.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
public float startinghealth = 100f;
private float currentHealth = 100f;
public int fundsAdded;
public GameObject player;
public GameObject wall;
public GameObject target;
private float range = 5f;
public float moveSpeed = 5f;
public float rotationSpeed = 1f;
public float minDistAtk = 0f;
private float distToPlayer = 0f;
private float distToWall = 0f;
static Animator anim;
void Start()
{
currentHealth = startinghealth;
player = GameObject.FindGameObjectWithTag("Player");
wall = GameObject.FindGameObjectWithTag("Wall");
target = GameObject.FindGameObjectWithTag("Target");
anim = GetComponent<Animator>();
}
void Update()
{
distToPlayer = Vector3.Distance(player.transform.position, this.transform.position);
distToWall = Vector3.Distance(wall.transform.position, this.transform.position);
if (currentHealth > 0)
{
if (distToPlayer <= minDistAtk && distToPlayer > 2.5f)
{
ApproachPlayer();
}
if (distToPlayer <= 2.5f)
{
Attack();
}
if (distToWall <= 5f)
{
Attack();
}
if (distToPlayer > minDistAtk && distToWall > 5f)
{
ApproachTarget();
}
}
if (currentHealth <= 0f)
{
Die();
}
}
public void ApplyDamage(float damage)
{
currentHealth -= damage;
}
void ApproachPlayer()
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(player.transform.position - transform.position), rotationSpeed * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, moveSpeed * 2f * Time.deltaTime);
anim.SetBool("isRunning", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
void Attack()
{
RaycastHit hit;
if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, range))
{
Debug.Log("Hit");
}
anim.SetBool("isRunning", false);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", true);
}
void ApproachTarget()
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.transform.position - transform.position), rotationSpeed * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, moveSpeed * Time.deltaTime);
anim.SetBool("isRunning", false);
anim.SetBool("isWalking", true);
anim.SetBool("isAttacking", false);
}
void Die()
{
anim.SetBool("isRunning", false);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
anim.SetBool("isDying", true);
gameObject.tag = "Untagged";
playerStat.money += fundsAdded;
Destroy(gameObject, 10);
}
}
Comment