I have multiple enemies and I want the enemy to shoot me but not all in the same time i mean that there is time between the attack of the enemy
my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public _enemyType enmyType;
public bool isDead;
[Header("Shooting Enemy")]
public GameObject bullet;
public float fireRate = 10f;
public float NextFire = -1f;
public bool canFire = true;
[Header("Moving around Enemy")]
public int moveAroundSpeed = 60;
[Header("Attack Enemy")]
public float stoppingDistance = 2.5f;
public float attackSpeed = 5;
public float aeCoolDown = 5f;
public float aelastAttackTime = 0f;
private bool move = true;
// Start is called before the first frame update
private void Awake()
{
}
void Start()
{
}
// Update is called once per frame
void Update()
{
if (transform.position.y < -3f)
{
Destroy(gameObject);
}
switch(enmyType)
{
case _enemyType.attack_enemy:
if (move)
{
transform.position = Vector3.MoveTowards(transform.position, Player.instance.player.transform.position, attackSpeed * Time.deltaTime);
}
StopEnemy();
if (transform.position.y < -3f)
{
Destroy(this.gameObject);
}
break;
case _enemyType.default_enemy:
break;
case _enemyType.shooting_enemy:
if (canFire)
{
CheckTimeToFire();
}
if (transform.position.y < -3f)
{
Destroy(this.gameObject);
}
break;
case _enemyType.moving_Enemy:
if (move)
{
transform.RotateAround(Player.instance.player.transform.position, new Vector3(0f, -1f, 0f), moveAroundSpeed * Time.deltaTime);
}
if (transform.position.y < -3f)
{
Destroy(this.gameObject);
}
break;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy" && Player.instance.isnotGrabbed)
{
LevelManager.instance.RemoveEnemyAndCheck(collision.gameObject);
Destroy(gameObject);
Destroy(collision.gameObject);
}
if (collision.gameObject.tag == "ground" && Player.instance.isnotGrabbed)
{
Destroy(gameObject);
}
}
public void CheckTimeToFire()
{
if (Time.time > NextFire)
{
NextFire = Time.time + fireRate;
Instantiate(bullet, transform.position, Quaternion.identity);
}
}
public void CanFire()
{
canFire = false;
}
public void TuggleMovement(bool Movement)
{
move = Movement;
}
public void StopEnemy()
{
float dist = Vector3.Distance(transform.position, Player.instance.player.transform.position);
if (dist < stoppingDistance)
{
move = false;
Debug.Log(aelastAttackTime);
Debug.Log("cool " + aeCoolDown);
if (Time.time - aelastAttackTime >= aeCoolDown)
{
aelastAttackTime = Time.time;
Player.instance.TakeDamage();
}
}
}
}
public enum _enemyType {default_enemy,attack_enemy,shooting_enemy,moving_Enemy }
↧