Search Unity

Weapon Shooting Animation not playing

Discussion in 'Editor & General Support' started by NotARealPerson1, Aug 2, 2021.

  1. NotARealPerson1

    NotARealPerson1

    Joined:
    Oct 23, 2019
    Posts:
    4
    Here is the code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GunScript : MonoBehaviour
    6. {
    7.    
    8.     [SerializeField] float Damage = 10f;
    9.     [SerializeField] float range = 100f;
    10.     [SerializeField] Camera Cam;
    11.     [SerializeField] Animator WeaponAnimator;
    12.     [SerializeField] AudioSource ShootSource;
    13.     [SerializeField] AudioClip ShootSound;
    14.     [SerializeField] ParticleSystem MuzzleFlash;
    15.     [SerializeField] bool IsAutomatic;
    16.     bool isfiring = false;
    17.     [SerializeField] string ShootAnimation;
    18.     [SerializeField] string IdleAnimation;
    19.    
    20.     void Update()
    21.     {
    22.         WeaponAnimator.Play(IdleAnimation);
    23.         if (Input.GetMouseButtonDown(0))
    24.         {
    25.             Shoot();
    26.         }
    27.     }
    28.  
    29.     void Shoot()
    30.     {
    31.         RaycastHit Hit;
    32.         if (Physics.Raycast(Cam.transform.position, Cam.transform.forward, out Hit, range))
    33.         {
    34.             Debug.Log(Hit.transform.name);
    35.         }
    36.  
    37.         ShootSource.PlayOneShot(ShootSound);
    38.         MuzzleFlash.Play();
    39.         WeaponAnimator.Play(ShootAnimation);
    40.     }
    41. }
    42.  
    I have tried putting WeaponAnimator.Play(IdleAnimation) under Void Start and it does, but if I do that, then it screws up the weapon switch code by only playing the "Idle Animations" once. I also do not have any of the idle animations on loop.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You should really use the Animator State machine to control your animation transitions. Then you can make the shooting animation play just by setting a trigger in code and the state machine will take care of playing the shooting animation and transitioning back to idle.

    Using Play causes all kinds of issues like this.
     
  3. NotARealPerson1

    NotARealPerson1

    Joined:
    Oct 23, 2019
    Posts:
    4
    I have already messed with triggers but the animation wouldn't play