Delaying Shooting animation time

Iam writing a simple AI script just for self learning for a sniping game. I am trying to implement a task of AI firing at player and decreasing player’s health. iam trying to achieve this by decreasing player’s health when AI’s shooting animation comes true. the problem is that once i go into the gameplay mode the health starts to decrease rapidly when the shooting animation is true and goes into negative factor where as i have called a function to destroy gameobject when player health reaches zero… here is the code iam implementing… if anyone will be kind enough to just point what iam doing wrong…

using UnityEngine;
using System.Collections;

public class AI_basic : MonoBehaviour {

public Transform player;
private Animator anim;
public float health = 60.0f;
public float fps_health = 300.0f;
public float fps_damage = 10.0f;

public float firingRate;

void Start () 
{
    anim = GetComponent<Animator>();
}


void Update()
{

    firingRate += Time.deltaTime;
    if (Vector3.Distance(player.position, this.transform.position) < 1000)
    {
        Vector3 direction = player.position - this.transform.position;
        direction.y = 0;
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 1f);

        anim.SetBool("isIdle", false);
        anim.SetBool("isShooting", false);

        if (direction.magnitude > 45)
        {
            this.transform.Translate(0, 0, 0.12f);
            anim.SetBool("isRunning_Rifle", true);
        }

        else if (direction.magnitude <= 45)
        {
            
            this.transform.Translate(0, 0, 0);
            anim.SetBool("isRunning_Rifle", false);
            anim.SetBool("isShooting", true);

            fps_TakeDamage(fps_damage);
               
            
           
        }
        
    }
    
}

public void takeDamage(float amount)
{
    health -= amount;
  
        if (health <= 0f)
        {
            die();
            Debug.Log("Target Destroyed");    
        }

}

public void die()
{
    Destroy(gameObject);
}

public void fps_TakeDamage(float amount)
{

    if (firingRate <= 0.9f)
    {
        fps_health -= amount;
        {
            if (health <= 0f)
            {
                health = 0;
                die();

            }
        }
    }

    firingRate = 0f;

}

}

I think you are trying to destroy the AI instead of the player. hint you want to destroy the gameObject of the player

Once the player has been destroyed I expect you wont want to run your code in update anymore. hint if the player transform is null there is nothing to do

You have 2 different functions : takeDamage() and fps_TakeDamage(). You call fps_TakeDamage() when the animation is running, so lets check fps_TakeDamage() function;

You decrease the value of “fps_health”, but then you check if “health” is <= 0. You should check “fps_health”.

Change lines 76-81 to:

  if (fps_health <= 0f)
                 {
                     fps_health = 0;
                     die();
                 }

yes my bad i realized that mistake and rectified it… i played an animation when the first person character’s health gets to see zero a play_dead animation will be played… however my other problem is still persistent which is to make the AI shoot a limited number of times in the frame so that the FPS health doesn’t drop so quickly… I can bypass this problem by increasing the FPS health and make the damage value he sustain with every animation very less but i dont think that this would be an elegant solution…