Hi, I have a problem about WaitForSeconds when editor set zero delay. It is not the result I was expecting, even set to zero, it’s like the instantiate of object delay maybe about 0.02 seconds? Can someone help me with a solution?
Here’s the snapshot (some code parts and result) I was actually working: (Image – left: without delay; right: with WaitForSeconds() set to zero)
But let’s just go here; here’s my simplified testing script:
CoroutineTest.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CoroutineTest : MonoBehaviour {
public Transform spawnPoint;
public float rotationOffset;
private float _rotationOffset;
public int shootAmount = 0;
[Range(0,1)] public float shootDelay = 0f;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space))
StartCoroutine (Test1 (1));
if (Input.GetKeyDown (KeyCode.E))
StopCoroutine (Test1 (2));
}
IEnumerator Test1 (int code) {
float setRotation = _rotationOffset;
if (code == 1)
{
_rotationOffset += rotationOffset;
for (int i = 0; i < shootAmount; i++) {
Instantiate (spawnPoint, transform.position, transform.rotation * Quaternion.Euler (0f, setRotation, 0f));
//work expected: 0.1f, 0.2f, 1, 2, 2.5f except 0 (zero)...
yield return new WaitForSeconds (shootDelay);
//yield return new WaitForSeconds (0);
}
}
else
{
Debug.Log ("Stop CoRoutine!");
}
yield return null;
}
}
I attach a zip file for this ^ StartCoroutineTesting.zip
I assume this is the code with issue? and by issue I mean, there is no pause between each shootAmount. so this is like 6 shoots in a gun. and you are trying to build a cool down system to delay how often the player can shoot his gun.
the issue is really in the Update(), the spacebar is creating multiple instances of the Coroutine. and there for the player can shoot when ever he wants. you might have 20 or 50 coroutines running at the same time.
Hi, thank you for the reply… but sorry for the lack of detail what I really want to achieved; I only concern is that WaitForSeconds() can’t go below 0.02 seconds, is that right? I use WaitForSeconds(), not for player, but for enemy/AI trigger to do hell bullet patterns or with style: for example https://www.youtube.com/results?search_query=game+hell+bullet
I want an option for to shoot parallel bullets with or without delay, I once use WaitForSeconds() for spawning enemies with gaps or instantly spawn all at once (WaitForSeconds(spawnGap) as spawnGap = 0), or they just seem to be but not… not until I make this HellBulletManager.cs:
In short, my only concern is that WaitForSeconds() can’t go instant? There will always be at least 0.02 seconds delay?
If that’s the case, can’t be done; I’ll just have to do the cheap way solution like:
if (linearDelay > 0)
yield return new WaitForSeconds(linearDelay);
What do you think, guys? ^
P.S. Thank you, now I know how to use StopCoroutine(); not using to my actual script HellBulletManager.cs, I just trying it to my testing script…