It looks like you're new here. If you want to get involved, click one of these buttons!
As seen in the Brackeys video (Ammo & Reloading (FPS Game)) when I play the reload animation and then switch my weapon and then go back to my previous weapon, the animation gets all messed up and the gun is in a weird position.
What it is SUPPOSED to be like.
What happens after reloading and switching. (the gun gets slightly distorted from it's original position)
This is my gun script:
--------------------------------
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 100f;
public int maxAmmo = 10;
private int currentAmmo;
public float reloadTime = 1f;
private bool isReloading = false;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
public Animator animator;
private void Start()
{
currentAmmo = maxAmmo;
}
void OnEnable()
{
isReloading = false;
animator.SetBool("Reloading", false);
}
// Update is called once per frame
void Update()
{
if (isReloading)
return;
if(currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetKeyDown(KeyCode.R) && currentAmmo < maxAmmo)
{
StartCoroutine(Reload());
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
IEnumerator Reload()
{
isReloading = true;
Debug.Log("RELODING...");
animator.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool("Reloading", 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))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
-----------------------------------
AND THIS IS MY WEAPON SWITCHING SCRIPTS IF U NEED IT!!!!
=================================================
using UnityEngine;
public class WeaponSwitching : MonoBehaviour
{
public int selectedWeapon = 0;
// Start is called before the first frame update
void Start()
{
SelectWeapon();
}
void Update()
{
int PreviousSelectedWeapon = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
selectedWeapon = 0;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
{
selectedWeapon = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
{
selectedWeapon = 2;
}
if (Input.GetKeyDown(KeyCode.Alpha4) && transform.childCount >= 4)
{
selectedWeapon = 3;
}
if (Input.GetKeyDown(KeyCode.Alpha5) && transform.childCount >= 5)
{
selectedWeapon = 4;
}
if (Input.GetKeyDown(KeyCode.Alpha6) && transform.childCount >= 6)
{
selectedWeapon = 5;
}
if (Input.GetKeyDown(KeyCode.Alpha7) && transform.childCount >= 7)
{
selectedWeapon = 6;
}
if (Input.GetKeyDown(KeyCode.Alpha8) && transform.childCount >= 8)
{
selectedWeapon = 7;
}
if (Input.GetKeyDown(KeyCode.Alpha8) && transform.childCount >= 9)
{
selectedWeapon = 8;
}
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++;
}
}
}
==================
Any suggestions and answers is greatly apreciated!