Search Unity

I Need Help with an FPS Firing Animation

Discussion in 'Animation' started by PrizeBuckle, Jan 19, 2021.

  1. PrizeBuckle

    PrizeBuckle

    Joined:
    Jan 19, 2021
    Posts:
    1
    I'm new to the forums so pardon me if this isn't the place to post this. I've been working on an FPS project and I animated an MP5K to bring into my project. This MP5K has 3 firing animations that are played at random every time the "Fire" function of my script is called. It works, but not consistently. In full auto at higher fire rates, the animations don't always interrupt each other.

    Here is the source code for the video below.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class WEAP_FIRING : MonoBehaviour {
    7.  
    8.     Animator animObj;
    9.  
    10.     bool canFire, canAim;
    11.  
    12.     float nextFire = 0.0f;
    13.  
    14.     public Text ammoCount;
    15.  
    16.     public int roundsInMag;
    17.     public int maxRoundsInMag;
    18.  
    19.     public float fireRate;
    20.     public float reloadTime;
    21.  
    22.     public AnimationClip[] firingAnim;
    23.     public AnimationClip idleAnim, movingAnim, aimAnim;
    24.    
    25.  
    26.     void Start() {
    27.  
    28.         animObj = GetComponent<Animator>();
    29.  
    30.         roundsInMag = maxRoundsInMag;
    31.  
    32.     }
    33.  
    34.     void Update() {
    35.  
    36.         ammoCount.text = "" + roundsInMag;
    37.  
    38.         if (Input.GetButton("Fire1") && roundsInMag > 0 && Time.time > nextFire) {
    39.  
    40.             nextFire = Time.time + fireRate;
    41.             Fire();
    42.  
    43.         }
    44.  
    45.         if (Input.GetButton("Fire2") && canAim) {
    46.  
    47.  
    48.  
    49.         }
    50.        
    51.     }
    52.  
    53.     void Fire () {
    54.  
    55.         roundsInMag--;
    56.         animObj.Play(firingAnim[Random.Range(0, firingAnim.Length)].name);
    57.  
    58.     }
    59. }
    Below is the animation list.



    and here is the script in action. I created a UI ammo counter at the top left to demonstrate the fire rate. As you can see the animations are sometimes in sync with the fire rate, and other times the animations don't play.



    Any help would be appreciated.
     
unityunity