Search Unity

Trigger animation running delayed, why? [Solved]

Discussion in 'Animation' started by PJRM, Jul 15, 2015.

  1. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303

    Hi.
    I set up this to trigger the Player_Fire animation when I click.
    the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Animator))]
    5. public class PlayerFire : MonoBehaviour {
    6.   #region variables
    7.   private PlayerStats player_stats;
    8.   private Animator player_anim;
    9.   #endregion
    10.   #region methods
    11.   void Start () {
    12.     player_anim = GetComponent<Animator>();
    13.     player_stats = GetComponent<PlayerStats>();
    14.     if (!player_stats)
    15.       Debug.LogError("Player Stats Script not assigned!!!");
    16.     }
    17.    
    18.     void Update () {
    19.     if (player_stats) {
    20.       if (Input.GetMouseButtonDown(0)) {
    21.         // roda a animação do tiro
    22.         if (player_anim) {
    23.           player_anim.SetTrigger("Firing");
    24.         }
    25.  
    26.         GameObject bullet = Instantiate(
    27.           player_stats.Fire_Weapon_Bullet_Prefab,
    28.           player_stats.Fire_Weapon_Slot.position,
    29.           player_stats.Fire_Weapon_Slot.rotation) as GameObject;
    30.         Bullet_Laser bl = bullet.GetComponent<Bullet_Laser>();
    31.         bl.Owner = gameObject;
    32.       }
    33.     }
    34.   }
    35.   #endregion
    36. }
    37.  
    The problem:
    I really want to this animation run at the moment I trigger it! but it's delaying or waitting the Blend tree, no matter what transition scale/time I set.

    May someone help me figure this out?
     
    grafiboys and zafranullah7 like this.
  2. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    I figure out a way to do it. but I really don't know if this is the best way to do it.
    1. Removed the trigger parameter
    2. Set time of the animation to 3x to looks more faster and realistic.
    3. I change this:
    Code (CSharp):
    1. player_anim.SetTrigger("Firing");
    To this:
    Code (CSharp):
    1. player_anim.CrossFade("Player_Fire", 0.05f, -1, 2f);
    Guys, I'm open for suggestions! I duplicated my prefab for test-making in case you post anything for me! The topic is watched so i'll be notified.

    Hope this helps others.
    Best regards,
    PJRM
     
    grafiboys likes this.
  3. the_motionblur

    the_motionblur

    Joined:
    Mar 4, 2008
    Posts:
    1,774
    You need to make sure that in your transition "has exit time" is set to false otherwise the current animation may run until it has reached the crossfade point.
    Also if you want absolute immediate change from one animation to another make sure that the transition duration is set to zero. This is usually the case for 2D sprite animation. 3D should have at least a little transition time.
     
    cym3ra, artcad, grafiboys and 4 others like this.
  4. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Thank you!!! I probably mess with this checkbox... it worked exactly as you said!
    Thank you so much.
     
    the_motionblur and grafiboys like this.