Search Unity

How to cancel/stop/interfere specific animation in animator?!?

Discussion in 'Scripting' started by Dziulijanas, Apr 9, 2019.

  1. Dziulijanas

    Dziulijanas

    Joined:
    Sep 27, 2018
    Posts:
    4
    Hello devs! I have an issue and I can't find any solution. Using Third Person Animator and related scripts. What I'm trying to achieve? While [Input.GetButtonDown("Fire1")] it plays spell casting animation, which is about 3 seconds and I want to cancel that animation by moving to any direction or jumping. Right now I can't interfere/stop animation while is not finished.

    Code (CSharp):
    1.  private CharacterControl m_Character; // A reference to the ThirdPersonCharacter on the object
    2.     private Transform m_Cam;                  // A reference to the main camera in the scenes transform
    3.     private Vector3 m_CamForward;             // The current forward direction of the camera
    4.     private Vector3 m_Move;
    5.     private bool m_Jump;                      // the world-relative desired move direction, calculated from the camForward and user input.
    6.  
    7.     private void Start()
    8.     {
    9.         //get the transform of the main camera
    10.         if (Camera.main != null)
    11.         {
    12.             m_Cam = Camera.main.transform;
    13.         }
    14.         else
    15.         {
    16.             Debug.LogWarning(
    17.                 "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
    18.             // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
    19.         }
    20.         // get the third person character ( this should never be null due to require component )
    21.         m_Character = GetComponent<CharacterControl>();
    22.     }
    23.  
    24.  
    25.     private void Update()
    26.     {
    27.         if (!m_Jump)
    28.         {
    29.             m_Jump = Input.GetButtonDown("Jump");
    30.         }
    31.     }
    32.  
    33.     // Fixed update is called in sync with physics
    34.     private void FixedUpdate()
    35.     {
    36.         // read inputs
    37.         float h = Input.GetAxis("Horizontal");
    38.         float v = Input.GetAxis("Vertical");
    39.  
    40.         // calculate move direction to pass to character
    41.         if (m_Cam != null)
    42.         {
    43.             m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; // 1, 0, 1
    44.             m_Move = v * m_CamForward + h * m_Cam.right;
    45.         }
    46.         else
    47.         {
    48.             // we use world-relative directions in the case of no main camera
    49.             m_Move = v * Vector3.forward + h * Vector3.right;
    50.         }
    51.  
    52.         //if (Input.GetKey(KeyCode.Mouse0))
    53.         //{
    54.         //    m_Character.Shooting(true);
    55.         //}
    56.         //else
    57.         //{
    58.         //    m_Character.Shooting(false);
    59.         //}
    60.  
    61.         if (Input.GetButtonDown("Fire1"))
    62.         {
    63.             m_Character.Shooting(true);
    64.         }
    65.         else
    66.         {
    67.             m_Character.Shooting(false);
    68.         }
    69.         //if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 2f;
    70.         // pass all parameters to the character control script
    71.         m_Character.Move(m_Move, m_Jump);
    72.         m_Jump = false;
    73.     }
    From Controller extra bit:
    Code (CSharp):
    1.     public void Shooting(bool action)
    2.     {
    3.         if (m_IsGrounded)
    4.         {
    5.             m_Animator.SetBool("Shooting", action);
    6.         }
    7.     }
    Other things:
    In animator every transition (Has Exit Time) is not ticked except 1 transition to Exit, because it freezes the character completely after the spell casting animation finishes. (Image attached).

    I've tried to change interruption sources but no luck.

    If I use [Input.GetKey(KeyCode.Mouse0)] instead I can release the mouse and animation stops at any time but if I hold till the end of animation the character freezes of the last frame. This could be an alternative option but I could not manage to restart the animation or change the state into e.g. Grounded.

    Any possible solution?

    Thanks!
    Cast.JPG
     
    Last edited: Apr 9, 2019