- Home /
Enemy doesn't shoot
So this is the script that manages the health and makes one of my enemies shoot, this script is in a child of the sprite so the players bullets don't interact with the collider that determines if the player is in it's firing range. I've checked the bullet and it works fine, also there is another enemy that works basically the same and it works perfectly fine so I'm quite lost. I think the problem may be in the way it checks if the player is on the trigger but as I said, in another enemy it works perfectly fine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowEnemy : MonoBehaviour
{
public GameObject parent;
public int health = 100;
public Transform firePoint;
public GameObject bulletPrefab;
float timer;
public int waitingTime;
bool readyShoot;
private void Start()
{
timer = 1;
}
private void Update()
{
timer += Time.deltaTime;
if (timer > waitingTime)
{
if (readyShoot == true)
{
Shoot();
timer = 0;
}
}
}
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
public void Die()
{
Destroy(parent);
}
void OnTriggerStay2D(Collider2D targ)
{
CharacterHealth character = targ.GetComponent<CharacterHealth>();
if (character != null)
{
readyShoot = true;
}
else
{
readyShoot = false;
}
}
void Shoot()
{
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
}
Thanks in advance.
Answer by ray2yar · Feb 05, 2019 at 07:52 PM
Have you checked that your readyShoot variable actually changes to true?
Your answer
Follow this Question
Related Questions
bullets kill all enemies, not just the ones hit. 1 Answer
my enemy is broken 1 Answer
Multiple Cars not working 1 Answer
OnTriggerEnter2D with multiple objects 0 Answers