Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Trigger animation running delayed, why? [Solved]

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

  1. PJRM


    Mar 4, 2013

    I set up this to trigger the Player_Fire animation when I click.
    the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    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.     }
    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.         }
    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. }
    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


    Mar 4, 2013
    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,
    grafiboys likes this.
  3. the_motionblur


    Mar 4, 2008
    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.
  4. PJRM


    Mar 4, 2013
    Thank you!!! I probably mess with this checkbox... it worked exactly as you said!
    Thank you so much.
    grafiboys likes this.