Search Unity

Animator transition not working properly

Discussion in 'Animation' started by ksmachado, Oct 1, 2021.

  1. ksmachado

    ksmachado

    Joined:
    May 11, 2021
    Posts:
    1
    Hi folks,

    I am trying to perform a 3d atimation transition the cicle of animations are not working properly.

    in first point my code have a "BasicAttack" method that starts the animation cicle:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class KnightManager : HeroManager
    5. {
    6.      float approachSpeed = 20;
    7. bool isApproaching;
    8.      public GameObject sword;
    9.      // Start is called before the first frame update
    10.      void Start()
    11.      {
    12.          selectorManager = GetComponent<SelectorManager>();
    13.          animator = GetComponent<Animator>();
    14.        
    15.      }
    16.      // Update is called once per frame
    17.      void Update()
    18.      {
    19.         // UpdateState();
    20.          Approaching();      
    21.      }
    22.      public override void BasicAttack()
    23.      {
    24.          base.BasicAttack();
    25.          enemy = GetTarget();
    26.          SetDirection(transform.position, enemy.transform.position, this.gameObject);
    27.          animator.applyRootMotion = false;
    28.          animator.SetBool("Approach_bool", true);
    29.          isApproaching = true;
    30.        
    31.      }
    32.      void Approaching()
    33.      {
    34.          if(isApproaching)
    35.          {
    36.            
    37.              transform.Translate(Vector3.forward * Time.deltaTime * approachSpeed);
    38.          }
    39.    
    40.      }
    41.      void OnTriggerStay(Collider collider)
    42.      {
    43.          if(collider.gameObject == enemy)
    44.          {
    45.              isApproaching = false;
    46.              animator.applyRootMotion = true;
    47.              animator.SetTrigger("Slash_trigger");
    48.              animator.SetBool("Approach_bool", false);
    49.          }
    50.      }
    51.      public void KillEnemy()
    52.     {
    53.          enemy.GetComponent<EnemyManager>().KillCharacter();
    54.      }
    55. }
    Below the animator controller:



    Whenever I trigger BasicAttack for the first time the animation works properly. The character approaches the enemy and when collides with him enters in Slash state.

    However, when I trigger BasicAttack again, the animation skips the approach state and goes straight to the slash state.