3
votes

first of all, forgive me if my question is too trivial, that's because I am new in Unity, and I have some difficulty getting documentation. What I want is to move a character from one position to another running animation walking. The animations are launched through an controller animation "Animator". the problem is that When I launch a trigger to start the animation, the character does not stop the previous animation to perform the animation I ask, that results in a desynchronization: The character starts to move with the animation it has at the time. I've tried many things, but I can not find the problem. this is may Animator: enter image description here

and this is de code i have:

enum AnimTriger {IdleTrigger, SpawnTrigger, RunTrigger, AttackTrigger, DeadTrigger, VictoryTrigger };
public class CharacterController : MonoBehaviour {
    private GameObject target, origin;
    private bool isWalking = false;
    private bool isMele = false;
    private GameObject instigator;



    public void goAndAttack(GameObject instigator, GameObject target,GameObject origin, bool mele)
    {
        this.target = target;
        this.origin = origin;        
        this.instigator = instigator;
        isMele = mele;
        StartCoroutine(AttackAction());

    }

    private IEnumerator AttackAction()
    {
        Animator anim = instigator.GetComponent<Animator>();

        yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length);        
        if (isMele)
        {
            anim.SetTrigger(AnimTriger.RunTrigger.ToString());
            yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length);
            isWalking = true;
            while (isWalking)
            {
                yield return new WaitForFixedUpdate();
            }
            yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length);

        }

        anim.SetTrigger(AnimTriger.AttackTrigger.ToString());
        if (isMele)
        {
            anim.SetTrigger(AnimTriger.RunTrigger.ToString());
            target = origin;
            yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length);
            isWalking = true;
            while (isWalking)
            {
                yield return new WaitForFixedUpdate();
            }
            yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length);
        }
        anim.SetTrigger(AnimTriger.IdleTrigger.ToString());

    }

    void Update () {
        if (isWalking && isMele)
        {
            instigator.transform.position = Vector3.MoveTowards(instigator.transform.position, target.transform.position, 0.1f);
            isWalking = instigator.transform.position != target.transform.position;
        }


    }


}

Is it possible to force the "Animator" to end any animation and execute any animation I want?
Thanks in advance for any help you can give me on this problem

2

2 Answers

2
votes

The answer is simple, just uncheck the "atomic" check box and you can break animations enter image description here

0
votes

bi the time y finish posting the question i found a fixed. i add to may UpDate function an other condition:

    void Update () {
    if (isWalking && isMele && instigator.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Basic Layer.Run"))
    {
        instigator.transform.position = Vector3.MoveTowards(instigator.transform.position, target.transform.position, 0.1f);
        isWalking = instigator.transform.position != target.transform.position;
    }

But this is not what i want.