Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Is it possible to not execute a function until the end of an animation clip?

Discussion in 'Animation' started by unity_hbuE5OoLAkpBCQ, Aug 25, 2023.

  1. unity_hbuE5OoLAkpBCQ

    unity_hbuE5OoLAkpBCQ

    Joined:
    Feb 8, 2022
    Posts:
    1
    Hi, I'm trying to make a simple game with basic FPS mechanics. I've made a script called "Revolver" that handles what a simple gun should do. Just focus on the Fire() and Reload() functions:

    Code (CSharp):
    1. public class Revolver : MonoBehaviour
    2. {
    3.     [SerializeField] Player player;
    4.     [SerializeField] Animator animator;
    5.     [SerializeField] AudioSource audioSource;
    6.     [SerializeField] float fireRate;
    7.     [SerializeField] float maxDistance;
    8.     [SerializeField] float pushForce;
    9.     [SerializeField] int damage;
    10.     [SerializeField] int maxAmmo, totalAmmo;
    11.     private int ammo;
    12.     private float lastShot = 0;
    13.  
    14.     public delegate void ConsumingAmmo(int ammo, int totalAmmo);
    15.     public event ConsumingAmmo OnAmmo;
    16.  
    17.     void Start()
    18.     {
    19.         player.playerActions.InGame.Attack.performed += _ => Fire();
    20.         player.playerActions.InGame.Reload.performed += _ => Reload();
    21.         ammo = maxAmmo;
    22.         OnAmmo?.Invoke(ammo, totalAmmo);
    23.     }
    24.  
    25.     private void OnDisable()
    26.     {
    27.         player.playerActions.InGame.Attack.performed -= _ => Fire();
    28.         player.playerActions.InGame.Reload.performed -= _ => Reload();
    29.     }
    30.  
    31.     void Fire()
    32.     {
    33.         if(ammo > 0 && Time.time - lastShot >= fireRate && !isReloading)
    34.         {
    35.             animator.SetTrigger("Fire");
    36.             audioSource.time = 0.1f;
    37.             audioSource.Play();
    38.             ammo--;
    39.             OnAmmo?.Invoke(ammo, totalAmmo);
    40.             lastShot = Time.time;
    41.         }
    42.     }
    43.  
    44.     void DealDamage()
    45.     {
    46.         RaycastHit hit;
    47.         if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, maxDistance))
    48.         {
    49.             if (hit.collider.gameObject.TryGetComponent(out IDamagable target))
    50.                 target.GetDamage(damage);
    51.  
    52.             if(hit.collider.gameObject.layer == LayerMask.NameToLayer("Prop"))
    53.                 hit.rigidbody.AddForce(-(Camera.main.transform.position - hit.transform.position).normalized * pushForce, ForceMode.Impulse);
    54.         }
    55.     }
    56.  
    57.     void Reload()
    58.     {
    59.         if(ammo < maxAmmo && totalAmmo > 0)
    60.         {
    61.             animator.SetTrigger("Reload");
    62.             int reload = (totalAmmo - (maxAmmo-ammo) > 0) ? maxAmmo - ammo : totalAmmo;
    63.             totalAmmo -= reload;
    64.             ammo += reload;
    65.             OnAmmo?.Invoke(ammo, totalAmmo);
    66.         }
    67.     }
    68.  
    69.     void OnDrawGizmos()
    70.     {
    71.         Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * maxDistance, Color.green);
    72.     }
    73. }
    Now the problem is: whenever I perform the Reload() function and play its own animation, I can still execute what's in the Fire() function (except obviously the Fire animation).
    So I was wandering if there's something i can put in the if() statement of the Fire() function that prevents to execute all the instructions like:

    Code (CSharp):
    1. if(conditions... && WaitEndAnimationClip)
    2. {
    3.     //instructions...
    4. }