Trigger animation running delayed, why? [Solved]


Hi.
I set up this to trigger the Player_Fire animation when I click.
the script:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Animator))]
public class PlayerFire : MonoBehaviour {
  #region variables
  private PlayerStats player_stats;
  private Animator player_anim;
  #endregion
  #region methods
  void Start () {
    player_anim = GetComponent<Animator>();
    player_stats = GetComponent<PlayerStats>();
    if (!player_stats)
      Debug.LogError("Player Stats Script not assigned!!!");
    }
   
    void Update () {
    if (player_stats) {
      if (Input.GetMouseButtonDown(0)) {
        // roda a animação do tiro
        if (player_anim) {
          player_anim.SetTrigger("Firing");
        }

        GameObject bullet = Instantiate(
          player_stats.Fire_Weapon_Bullet_Prefab,
          player_stats.Fire_Weapon_Slot.position,
          player_stats.Fire_Weapon_Slot.rotation) as GameObject;
        Bullet_Laser bl = bullet.GetComponent<Bullet_Laser>();
        bl.Owner = gameObject;
      }
    }
  }
  #endregion
}

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?

2 Likes

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:
    player_anim.SetTrigger("Firing");To this: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

1 Like

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.

7 Likes

Thank you!!! I probably mess with this checkbox… it worked exactly as you said!
Thank you so much.

2 Likes