Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Animations Getting Stuck on Enable/Disable of Objects

Discussion in 'Animation' started by AnimusRex, Nov 21, 2019.

  1. AnimusRex

    AnimusRex

    Joined:
    Apr 26, 2019
    Posts:
    67
    So I have an FPS controller I'm trying to set up, and when I switch from one weapon to another, if I cut off the animations mid weapon draw, and then switch back, the weapon goes through the weapon draw animation, but then the idle state snaps down to where it was originally cut off from.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponManager : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private Weapons[] weapons;
    9.  
    10.     private int current_Weapon_Index;
    11.  
    12.     private void Start()
    13.     {
    14.         current_Weapon_Index = 0;
    15.         weapons[current_Weapon_Index].gameObject.SetActive(true);
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (Input.GetKeyDown(KeyCode.Alpha1))
    21.         {
    22.             TurnOnSelectedWeapon(0);
    23.         }
    24.         if (Input.GetKeyDown(KeyCode.Alpha2))
    25.         {
    26.             TurnOnSelectedWeapon(1);
    27.         }
    28.         if (Input.GetKeyDown(KeyCode.Alpha3))
    29.         {
    30.             TurnOnSelectedWeapon(2);
    31.         }
    32.         if (Input.GetKeyDown(KeyCode.Alpha4))
    33.         {
    34.             TurnOnSelectedWeapon(3);
    35.         }
    36.     }
    37.  
    38.     void TurnOnSelectedWeapon(int weaponIndex)
    39.     {
    40.         weapons[current_Weapon_Index].gameObject.SetActive(false);
    41.  
    42.         weapons[weaponIndex].gameObject.SetActive(true);
    43.  
    44.         if (weapons[weaponIndex].drawnSound != null)
    45.         {
    46.             weapons[weaponIndex].drawnSound.Play();
    47.         }
    48.  
    49.         current_Weapon_Index = weaponIndex;
    50.     }
    51.  
    52.     public Weapons GetCurrentSelectedWeapon()
    53.     {
    54.         return weapons[current_Weapon_Index];
    55.     }
    56. }
    57.  
    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public enum WeaponAim {
    6.     NONE, SELF_AIM, AIM
    7. }
    8.  
    9. public enum WeaponFireType
    10. {
    11.     SINGLE,
    12.     BURST,
    13.     AUTO
    14. }
    15.  
    16. public enum WeaponBulletType
    17. {
    18.     HITSCAN,
    19.     PROJECTILE,
    20.     NONE,
    21. }
    22.  
    23. public class Weapons : MonoBehaviour
    24. {
    25.     public Animator anim;
    26.  
    27.     [SerializeField]
    28.     private GameObject muzzleFlash;
    29.  
    30.     [SerializeField]
    31.     public AudioSource shootSound, reloadSound, drawnSound;
    32.  
    33.     public WeaponFireType fireType;
    34.  
    35.     public WeaponAim weaponAim;
    36.  
    37.     public WeaponBulletType bulletType;
    38.  
    39.     public GameObject attack_point;
    40.  
    41.     void Awake()
    42.     {
    43.         anim = GetComponent<Animator>();
    44.     }
    45.  
    46.     public void ShootAnimation()
    47.     {
    48.         anim.SetTrigger("Shoot");
    49.        
    50.         Instantiate(muzzleFlash);
    51.     }
    52.  
    53.     public void Aim(bool canAim)
    54.     {
    55.         anim.SetBool("isAiming", canAim);
    56.     }
    57.  
    58.     void Turn_On_MuzzleFlash()
    59.     {
    60.         muzzleFlash.SetActive(true);
    61.     }
    62.  
    63.     void Turn_Off_MuzzleFlash()
    64.     {
    65.         muzzleFlash.SetActive(false);
    66.     }
    67.  
    68.     void Play_Shoot_Sound()
    69.     {
    70.         shootSound.Play();
    71.     }
    72.  
    73.     void Play_Reload_Sound()
    74.     {
    75.         reloadSound.Play();
    76.     }
    77.  
    78.     void Turn_On_AttackPoint()
    79.     {
    80.         attack_point.SetActive(true);
    81.     }
    82.  
    83.     void Turn_Off_AttackPoint()
    84.     {
    85.         if (attack_point.activeInHierarchy)
    86.         {
    87.             attack_point.SetActive(false);
    88.         }
    89.     }
    90. }
    91.  
    upload_2019-11-20_17-3-41.png

    I've been stuck on this for a few days and read multiple other threads with various suggestions but I can't get any of them to work.
     
  2. leejewitt

    leejewitt

    Joined:
    Jun 5, 2020
    Posts:
    6
    hi i know this is old but did you fix this i am having the same issue?
     
    FlightOfOne likes this.
  3. TheMagnificentSwill

    TheMagnificentSwill

    Joined:
    Oct 6, 2020
    Posts:
    1
unityunity