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;
}
}