So I got a turret that fires 3 rounds and then it has to pause for a second or two to reload, however I have no idea how to implement some kind of wait time inside the coroutine to make it work. I tried yield return WaitforSeconds kind of everywhere but without success. Sorry I’m new to coding and my research didn’t give me the results I was looking for. Thanks for your patience!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class burstshoot : MonoBehaviour
{
public Transform player;
public GameObject bullet;
public float numberOfBullets;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
transform.LookAt(player);
StartCoroutine(Shoot());
}
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
transform.LookAt(player);
}
}
IEnumerator Shoot()
{
for (int i = 0; i < numberOfBullets; i++)
{
Instantiate(bullet, transform.position, transform.rotation);
yield return new WaitForSeconds(0.2f);
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
StopCoroutine(Shoot());
}
}
}
Give this a try
IEnumerator Shoot( )
{
for( int i = 0; i < numberOfBullets; i++ )
{
Instantiate ( bullet, transform.position, transform.rotation );
}
yield return new WaitForSeconds ( 2f );
}
I already tried that but then my turret only shoots every 2 seconds. I want it to shoot a burst of numberOfBullets before pausing for a second and then reshoot numberOfBullets again, indefinitely.
put the whole code in a loop and pause inside the inner loop for .2f and outside of the inner loop, for 2s.
IEnumerator Shoot() {
while(true)
{
for(int i = 0; i < numberOfBullets; ++i)
{
Instantiate(bullet, transform.position, transform.rotation);
yield return new WaitForSeconds(0.2f);
}
yield return new WaitForSeconds(2);
}
}
You can also cache the WaitForSeconds*.
Oh, btw, you should start/stop the coroutine like this:
Coroutine shootRoutine;
// upon entering the trigger
shootRoutine = StartCoroutine(Shoot());
// when exiting the trigger
if(shootRoutine != null) StopCoroutine(shootRoutine);
How about this then:
public Transform player;
public GameObject bullet;
public float numberOfBullets;
private IEnumerator shootCoroutine;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
transform.LookAt(player);
if (shootCoroutine != null)
{
StopCortouine(shootCoroutine);
shootCoroutine = null;
}
shootCoroutine = Shoot()
StartCoroutine(shootCoroutine);
}
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
transform.LookAt(player);
}
}
IEnumerator Shoot()
{
while (true)
{
for (int i = 0; i < numberOfBullets; i++)
{
Instantiate(bullet, transform.position, transform.rotation);
yield return new WaitForSeconds(0.5f);
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
StopCoroutine(shootCoroutine);
shootCoroutine = null;
}
}
}
You want to loop indefinitely inside the coroutine. The code above also shows how to properly stop a coroutine.
You just forgot the 2s delay inside the (outter) loop. 