Hello, can you please help me to create correctly shoot of bullet to enemy center with my method ?
private void Update()
{
if (GameObject.FindGameObjectWithTag("Enemy") && !_shooting)
{
StartCoroutine(Shoot());
_shooting = true;
}
}
private IEnumerator Shoot()
{
int random = Random.Range(1, Camera.main.transform.childCount);
int rnd = Random.Range(0, transform.childCount);
Instantiate(Bullet, transform.parent);
target = Camera.main.transform.GetChild(1).transform.position;
var rb = transform.parent.GetChild(transform.parent.childCount - 1).GetComponent<Rigidbody2D>();
rb.transform.position = positions[rnd];
transform.GetChild(rnd).GetComponent<Transform>().localScale = new Vector2(1.2f, 1.2f);
StartCoroutine(ReturnDotSize(rnd));
while (Vector2.Distance(rb.transform.position, target) > 0.1f)
{
rb.transform.position = Vector2.MoveTowards(rb.transform.position, target, shootSpeed * Time.deltaTime);
yield return null;
}
yield return new WaitForSeconds(ReloadTime);
_shooting = false;
}
Now bullet shoot only one time. I was try to write break instead of yield return null, but in this time bullet not shoot to center of enemy.In addition, I tried to write “_shooting = false” over “yield return null” and in this case the bullet clearly goes to the center of the enemy, but the “ReloadTime” delay does not work. Now I need the bullet to accurately go to the center of the enemy and work correctly “ReloadTime”.