Question by
Gamingersp · Nov 18, 2017 at 05:17 AM ·
c#enemyshooting
How to make a bullet delay?
Hi guys i am currently coding an enemy AI but cant figure out how to make a delay between shots, btw i am coding in c#. Here is my script: using System.Collections; using UnityEngine;
public class EnemyAIScript : MonoBehaviour {
private Animator anim2;
public Transform player;
public float playerDistance;
public float rotationDamping;
public float moveSpeed;
public static bool isPlayerAlive = true;
// Use this for initialization
void Start () {
anim2 = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if(isPlayerAlive)
{
playerDistance = Vector3.Distance (player.position, transform.position);
if(playerDistance < 15f)
{
lookAtPlayer();
}
if(playerDistance < 12f)
{
if(playerDistance > 5f)
{
chase();
}
else if(playerDistance < 15f)
{
attack();
}
}
}
}
void lookAtPlayer()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
void chase()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
void attack()
{
RaycastHit hit;
anim2.SetBool("Fire2", true);
if(Physics.Raycast (transform.position, transform.forward, out hit))
{
if(hit.collider.gameObject.tag == "Player")
{
hit.collider.gameObject.GetComponent<HealthScript>().health -= 5f;
}
}
}
}
Comment