- Home /
Question by
alpayoguz97 · Dec 03, 2020 at 03:07 PM ·
shootingenemy aiartificial intelligence
Make AI Shoot at Player
Hey Guys,
I'm trying to make simple space game that have a mother ship(player) and enemys.
I wanted the Enemy object to shoot at mother ship when they saw it. but I couldn't. Instead I had enemies shoot at every 2 seconds. But It makes the enemy look stupid ^^
I looked previous subjects but there was nothing about it. Any help will be appreciated. Thanks in advance
Those are my code :
public class Enemy : MonoBehaviour
{
[SerializeField] float health = 100;
[SerializeField] GameObject fireball;
[SerializeField] float timeBetweenShots;
[SerializeField] float startTimeBetweenShots;
private void OnCollisionEnter2D(Collision2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
DamageProcess(other, damageDealer);
}
private void DamageProcess(Collision2D other, DamageDealer damageDealer)
{
health -= damageDealer.GetDamage();
Destroy(other.gameObject);
if (health == 0)
{
Destroy(gameObject);
}
}
private void Start()
{
timeBetweenShots = startTimeBetweenShots;
}
private void Update()
{
MakeEnemyFire();
}
private void MakeEnemyFire()
{
if (timeBetweenShots <= 0)
{
Instantiate(fireball, GameObject.FindGameObjectWithTag("EnemyFire").transform.position, Quaternion.identity);
timeBetweenShots = startTimeBetweenShots;
}
else
{
timeBetweenShots -= Time.deltaTime;
}
}
}
Comment