I'm working on a FPS Game and everything seems to work but i have a problem : i have 3 weapons, i'm reloading the first one and switch to the second one with the mouse scrollwheel. When i switch back to the first weapon, my reload animation stopped at the moment when i switched to the second Weapon (and the animation stay like this).
Looks like this :![alt text][1]
[1]: /storage/temp/148181-reloadanimbroken.png
Script of gun Shooting :
using System.Collections;
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float FireRate = 15f;
public int maxAmmo = 10;
private int currentAmmo;
public float reloadTime = 1f;
public Camera fpsCam;
public ParticleSystem MuzzleFlash;
public GameObject ImpactEffect;
public AudioClip ShootSound;
private float nextTimeToFire;
private bool IsReloading = false;
public Animator animator;
void Start()
{
currentAmmo = maxAmmo;
}
void OnEnable()
{
IsReloading = false;
animator.SetBool("Reload", false);
}
void Update()
{
if (IsReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / FireRate;
GetComponent().PlayOneShot(ShootSound);
Shoot();
}
}
IEnumerator Reload()
{
IsReloading = true;
animator.SetBool("Reload", true);
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool("Reload", false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
IsReloading = false;
}
void Shoot()
{
MuzzleFlash.Play();
currentAmmo--;
RaycastHit hit;
if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
}
GameObject impactGO = Instantiate(ImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 1f);
}
}
And the script of weapon switching :
using UnityEngine;
public class WeaponSwitching : MonoBehaviour
{
public int selectedWeapon = 0;
void Start()
{
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if(previousSelectedWeapon != selectedWeapon)
{
SelectWeapon();
}
}
void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
}
I can still shoot and if i reload again, my animation is freaking out
Someone has a suggestion ?
↧