Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Animation Completely screws up when I switch between 2 weapons.

Discussion in 'Animation' started by Wontairr, Jan 5, 2021.

  1. Wontairr

    Wontairr

    Joined:
    Sep 7, 2019
    Posts:
    5
    I have a pistol in my 3d unity game, and I made a reload for it, it works fine until you either: switch to another gun then back to the pistol then reload. or: switching really fast between weapons. which the "weird" part is where the left upper arm doesnt appear to put the new mag in. I imported the animations from blender

    My script is using brackeys code from the weapon switching and weapon raycasting tutorials.

    WeaponSwitching:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class WeaponSwitching : MonoBehaviour {
    5.  
    6.     public int selectedWeapon = 0;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         SelectWeapon();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         int previousSelectedWeapon = selectedWeapon;
    18.  
    19.         if (Input.GetKeyDown(KeyCode.Alpha1))
    20.         {
    21.             selectedWeapon = 0;
    22.         }
    23.         if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >=2)
    24.         {
    25.             selectedWeapon = 0;
    26.             selectedWeapon = 0;
    27.             selectedWeapon = 0;
    28.             selectedWeapon = 0;
    29.             selectedWeapon = 0;
    30.             selectedWeapon = 0;
    31.             selectedWeapon = 0;
    32.             selectedWeapon = 1;
    33.         }
    34.  
    35.         if (previousSelectedWeapon != selectedWeapon)
    36.         {
    37.             SelectWeapon();
    38.         }
    39.     }
    40.    
    41.     void SelectWeapon ()
    42.     {
    43.         int i = 0;
    44.         foreach (Transform weapon in transform)
    45.         {
    46.             if (i == selectedWeapon)
    47.                 weapon.gameObject.SetActive(true);
    48.             else
    49.                 weapon.gameObject.SetActive(false);
    50.             i++;
    51.         }
    52.     }
    53. }
    54.  
    Gun Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class Gun : MonoBehaviour {
    7.  
    8.     public float damage = 10f;
    9.     public float range = 100f;
    10.     public float impactForce = 30f;
    11.     public float fireRate = 1f;
    12.  
    13.  
    14.     public int maxAmmo = 8;
    15.     private int currentAmmo;
    16.     public float reloadTime = 1.933f;
    17.     private bool isReloading = false;
    18.  
    19.     public Camera fpsCam;
    20.     public ParticleSystem muzzleflash;
    21.     public GameObject bulletimpact;
    22.     public GameObject pistol;
    23.     public AudioSource shootsound;
    24.     public AudioSource reloadsound;
    25.     public GameObject reloadsoundobject;
    26.  
    27.     private float nextTimeToFire = 0f;
    28.  
    29.     void Start()
    30.     {
    31.         currentAmmo = maxAmmo;
    32.     }
    33.  
    34.     void OnEnable()
    35.     {
    36.         isReloading = false;
    37.         reloadsoundobject.SetActive(false);
    38.  
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update()
    43.     {
    44.  
    45.         if (isReloading)
    46.             return;
    47.  
    48.  
    49.  
    50.  
    51.         if (currentAmmo <= 0)
    52.         {
    53.             StartCoroutine(Reload());
    54.             return;
    55.         }
    56.  
    57.         if (Input.GetKeyDown(KeyCode.R) && currentAmmo < maxAmmo)
    58.         {
    59.             StartCoroutine(Reload());
    60.         }
    61.  
    62.         if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
    63.         {
    64.             nextTimeToFire = Time.time + 1f / fireRate;
    65.             Shoot();
    66.             pistol.GetComponent<Animator>().Play("shoot");
    67.             shootsound.Play();
    68.  
    69.         }
    70.  
    71.     }
    72.  
    73.     IEnumerator Reload()
    74.     {
    75.  
    76.         isReloading = true;
    77.  
    78.         Debug.Log("realoding");
    79.  
    80.         reloadsoundobject.SetActive(true);
    81.  
    82.         reloadsound.Play();
    83.  
    84.         pistol.GetComponent<Animator>().Play("reload");
    85.  
    86.         yield return new WaitForSeconds(reloadTime);
    87.  
    88.         currentAmmo = maxAmmo;
    89.         isReloading = false;
    90.     }
    91.  
    92.     void Shoot()
    93.     {
    94.  
    95.         muzzleflash.Play();
    96.  
    97.         currentAmmo--;
    98.  
    99.         RaycastHit hit;
    100.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    101.         {
    102.             Debug.Log(hit.transform.name);
    103.  
    104.             hit.transform.GetComponent<Target>();
    105.  
    106.             Target target = hit.transform.GetComponent<Target>();
    107.             if (target != null)
    108.             {
    109.                 target.TakeDamage(damage);
    110.             }
    111.  
    112.             if (hit.rigidbody != null)
    113.             {
    114.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    115.             }
    116.  
    117.             GameObject impactGO = Instantiate(bulletimpact, hit.point, Quaternion.LookRotation(hit.normal));
    118.             Destroy(impactGO, 2f);
    119.         }
    120.     }
    121. }
     
  2. Wontairr

    Wontairr

    Joined:
    Sep 7, 2019
    Posts:
    5
    fixed, my animation just was really screwed up lol.