- Home /
Animation sometimes doesn't play until the end, sometimes it doesn't play at all and sometimes it plays more than once.
using UnityEngine; using System.Collections;
public class Enemy : MonoBehaviour {
public Animator animator;
private void Start()
{
animator = gameObject.GetComponent<Animator>();
}
public float health = 100f;
IEnumerator Die()
{
animator.ResetTrigger("isWalking");
animator.ResetTrigger("isAttacking");
animator.SetTrigger("isDead");
yield return new WaitForSeconds(seconds: 0.80f);
animator.ResetTrigger("isDead");
Destroy(gameObject);
}
public void TakeDamage(float damage)
{
health -= damage;
if (health <= 0f)
{
StartCoroutine(Die());
}
}
}
using System.Collections; using UnityEngine; using UnityEngine.AI;
public class EnemyController : MonoBehaviour {
public float lookRadius = 10f;
private bool isWalking = false;
public Animator animator;
// public Animation walkAnimation;
Transform target;
NavMeshAgent agent;
// Use this for initialization
void Start () {
target = PlayerManager.instance.Player.transform;
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
animator.SetBool("isWalking", true);
animator.SetBool("isAttacking", false);
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
FaceTarget();
animator.SetBool("isWalking", false);
animator.SetBool("isAttacking", true);
StartCoroutine(WaitBeforeAttack());
}
}
}
IEnumerator WaitBeforeAttack()
{
yield return new WaitForSeconds(1);
TakeDamage();
}
void TakeDamage()
{
GameObject.Find("Player").GetComponent<PlayerHealth>().playerHealth -= 10;
}
void FaceTarget ()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 7f);
}
}
no offence but 1) you haven't posted a full code example so we can see what you might be missing (where is TakeDamage() called from?) and 2) your code example needs to be formatted correctly (the top 2 lines are just plain text) which takes like... 10 seconds to fix? putting more effort into your question and how your format it will most likely result in people being more willing to help.
Answer by winterfluxstudio · May 22, 2018 at 06:01 PM
Try adding debug logs and see when/if they trigger to get a better idea what might be causing it.
It could potentially be an issue with your animation controller parameters. What are your animation state trasitions like? For example, one particular settings forces the AC to wait for the animation to "finish" before moving to the next animation. You can "Break" this to quickly switch between animations.
using System.Collections;
using UnityEngine;
using UnityEngine.AI;
public class EnemyController : MonoBehaviour {
public float lookRadius = 10f;
private bool isWalking = false;
public Animator animator;
// public Animation walkAnimation;
Transform target;
NavMeshAgent agent;
void Start ()
{
target = PlayerManager.instance.Player.transform;
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
animator.SetBool("isWalking", true);
Debug.Log("isWalking = true");
animator.SetBool("isAttacking", false);
Debug.Log("isAttacking= false");
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
Debug.Log("call FaceTarget()");
FaceTarget();
animator.SetBool("isWalking", false);
Debug.Log("isWalking = false");
animator.SetBool("isAttacking", true);
Debug.Log("isAttacking = true");
StartCoroutine(WaitBeforeAttack());
}
}
}
IEnumerator WaitBeforeAttack()
{
yield return new WaitForSeconds(1);
TakeDamage();
}
void TakeDamage()
{
GameObject.Find("Player").GetComponent<PlayerHealth>().playerHealth -= 10;
}
void FaceTarget ()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 7f);
}
}
updated (with debug logs). a common issue with animation is the state machine and how it works. For example, your issue could potentially be due to the "exit time" of an animation. This means the system will wait for that animation to finish playing before moving to the next animation state. This can cause some user input or transitions to not register. (try this). click on the animation state connection (the white line) between two animations and deselect "has exit time". Re-test with Debug logs to see if problem is resolved.
I'll try that! Edit: The animations work PERFECTLY now, thanks to you. God Bless you.
Your answer
Follow this Question
Related Questions
How do i add an AnimationEvent to an animation of a single instance with script? 0 Answers
How to enable an animation then disable after it is done playing no matter what fps you are running? 1 Answer
CORoutine does not completely stop 1 Answer
Animation created via script does not play 1 Answer
animator.GetCurrentAnimatorStateInfo(0).IsName not working 1 Answer