Minor Issue: Gun slide animation not working all the time
Hello, I'm working on an FPS game and I'm trying to get the gun to stay racked back when bullet count is 0. The only sure fire way for this to work is when I hold the "Fire1" button on the last bullet. When I quickly tap the fire button on the last bullet, the animation doesn't go through. I only know this because I watched my Animator Controller as I pressed Fire1.
I am trying to trigger the rack back as soon as bullets 0, regardless of how quickly I press it. Do you guys have any suggestions? Sorry, I know this is messy and confusing.
I've tried changing conditions from Bool to Integer in the Animator Controller, using racked.SetInteger("bullets", 0) and racked.SetBool("bullets", true/false); Here is the code:
public class ammocount : MonoBehaviour
{
public Text bullets;
private int count;
public Animator racked;
public Animator anim;
void Start ()
{
count = 18;
bullets.text = count.ToString();
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
count -= 1;
if (count == 0)
{
racked.SetInteger("bullets", 0);
}
if (count < 0)
{
GameObject.Find("Glock").GetComponent<PlayerShoot>().enabled = false;//won't be able to shoot
count = 0; //bullet count will not go below 0
}
bullets.text = count.ToString();
}
if (Input.GetKey("r")) //reload
{
count = 18; //reset bullets to max magazine capacity
bullets.text = count.ToString(); //display bullets
GameObject.Find("Glock").GetComponent<PlayerShoot>().enabled = true; //re-enable shooting script
racked.SetInteger("bullets", 18); //argument 2 can be any value above 0, because condition in the Animator condition (Integer) is > than 0
}
}
}
This is my ammocount script. My Count script (UI > Text) is attached to the Bullets slot in my ammocount component. I have the Glock (Animator) assigned to both Animator slots (if that makes sense).
using UnityEngine;
using UnityEngine.UI;
public class PlayerShoot : MonoBehaviour
{
public float damage = 10f; //player now does 10 dmg
public float range = 100f;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public Camera cam;
private bool isRunning;
public Animator anim;
void Start()
{
isRunning = true;
}
void Update ()
{
if (isRunning == true)
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
anim.SetBool("Shot", true);
}
if (Input.GetButtonUp("Fire1"))
{
anim.SetBool("Shot", false);
}
}
}
void Recoil()
{
}
void Shoot() //what happens when you shoot
{
muzzleFlash.Play();
RaycastHit hit; //variable to store what we hit
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range)) //if this hits something
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>(); //target we hit that we want to destroy with Target script
if(target != null) // if we found a target, we want to execute the below script
{
target.TakeDmg(damage); //damage variable defined in this script, target.TakeDmg() function is defined in Target script
}
//playershoot script (10 dmg) --> target script (takedmg function) then die script
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, .2f);//argument 2 is how fast the script destroys the object (argument 1)
}
}
}
This is my PlayerShoot script. Its attached to my Glock object. The hierarchy being Player > Main Camera > Glock > Animator Controller: "Player 1" > Glock object's Animator component.
I tried replacing Input.GetButtonDown("Fire1") with Input.GetButtonUp("Fire1"), Input.GetKeyDown(KeyCode.Mouse0) and Input.GetKeyUp(KeyCode.Mouse0). Neither worked to what I wanted it to do. I also messed with Transition Duration and Offset in the Animator Controller conditions settings to no avail. I tried a bunch of things, I don't exactly remember all of it.