I am currently making a fps and I cant get my way around this obstacle.
For context I am trying to play a reload animation , but only if the gun has an attachment . I tried to do this in multiple ways but it doesn’t even play a reload animation when the gun has the attachment in the current state.
I thought I’d ask for help here , since I can’t find any tutorial on the matter at hand.
This is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;
using TMPro;
public class WeaponScr : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public int maxAmmo = 10;
public int currentAmmo;
public float reloadTime = 1f;
private bool isRealoading = false;
public bool isAttached = false;
public AudioSource sounds;
public AudioClip[ ] audioArray;
public AudioSource reloadSounds;
public AudioSource clip2;
public Animator animator;
public Camera fpscamera;
public ParticleSystem muzzleflash;
public GameObject impactEffect;
public GameObject ammoDisplay;
public GameObject reloadText;
private ProceduralRecoilWeapon recoilWeapon;
private float nextTimeToFire = 0f;
[Header(“Reference Points”)]
public Transform recoilPosition;
public Transform rotationPoint;
[Space(10)]
[Header(“Speed Settings”)]
public float positionalRecoilSpeed = 8f;
public float rotationalRecoilSpeed = 8f;
[Space(10)]
public float positionalReturnSpeed = 18f;
public float rotationalReturnSpeed = 38f;
[Space(10)]
[Header(“Amount Settings:”)]
public Vector3 RecoilRotation = new Vector3(10, 5, 7);
public Vector3 RecoilKickBack = new Vector3(0.015f, 0f, -0.2f);
[Space(10)]
public Vector3 RecoilRotationAim = new Vector3(10, 4, 6);
public Vector3 RecoilKickBackAim = new Vector3(0.015f, 0f, -0.2f);
[Space(10)]
Vector3 rotationalRecoil;
Vector3 positionalRecoil;
Vector3 Rot;
[Header(“State:”)]
public bool aiming;
void Start()
{
currentAmmo = maxAmmo;
sounds.clip = audioArray[Random.Range(0, audioArray.Length)];
}
private void FixedUpdate()
{
rotationalRecoil = Vector3.Lerp(rotationalRecoil, Vector3.zero, rotationalReturnSpeed * Time.deltaTime);
positionalRecoil = Vector3.Lerp(positionalRecoil, Vector3.zero, positionalReturnSpeed * Time.deltaTime);
recoilPosition.localPosition = Vector3.Slerp(recoilPosition.localPosition, positionalRecoil, positionalRecoilSpeed * Time.deltaTime);
Rot = Vector3.Slerp(Rot, rotationalRecoil, rotationalRecoilSpeed * Time.deltaTime);
rotationPoint.localRotation = Quaternion.Euler(Rot);
Attach();
}
void Update()
{
if (isRealoading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetKey(KeyCode.Mouse0) && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
ShowAmmo();
}
void OnEnable()
{
isRealoading = false;
animator.SetBool(“Reloading”, false);
}
IEnumerator Reload()
{
isRealoading = true;
Debug.Log(“Reloading…”);
animator.SetBool(“Reloading”, true);
yield return new WaitForSeconds(.5f);
reloadSounds.Play();
yield return new WaitForSeconds(1.5f);
clip2.Play();
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool(“Reloading”, false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
isRealoading = false;
}
void Shoot()
{
muzzleflash.Play();
sounds.PlayOneShot(sounds.clip);
currentAmmo–;
RaycastHit hit;
if (Physics.Raycast(fpscamera.transform.position, fpscamera.transform.forward, out hit, range))
{
UnityEngine.Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent();
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);
}
if (aiming)
{
rotationalRecoil += new Vector3(-RecoilRotationAim.x, Random.Range(-RecoilRotationAim.y, RecoilRotationAim.y), Random.Range(-RecoilRotationAim.z, RecoilRotationAim.z));
positionalRecoil += new Vector3(Random.Range(-RecoilKickBackAim.x, RecoilKickBackAim.x), Random.Range(-RecoilKickBackAim.y, RecoilKickBackAim.y), RecoilKickBackAim.z);
}
else
{
rotationalRecoil += new Vector3(-RecoilRotation.x, Random.Range(-RecoilRotation.y, RecoilRotation.y), Random.Range(-RecoilRotation.z, RecoilRotation.z));
rotationalRecoil += new Vector3(Random.Range(-RecoilKickBack.x, RecoilKickBack.x), Random.Range(-RecoilKickBack.y, RecoilKickBack.y), RecoilKickBack.z);
}
}
public void ShowAmmo()
{
ammoDisplay.GetComponent<TMP_Text>().text = currentAmmo + “/20”;
}
public void Attach()
{
if (Input.GetKeyDown(KeyCode.T))
{
isAttached= true;
animator.SetBool(“Attachment”, true);
}
if (Input.GetKey(KeyCode.Y))
{
isAttached = false;
animator.SetBool(“Attachment”, false);
}
}
}
And I will also add some screeenshots of the animator of my gun.