I have attempted to get this to work properly but no matter how I place the Coroutine into the code it keeps breaking the game mechanic. This is the closest I gotten to operation but it is still broken because every 2 loops it ignores the wait time. Can someone help me understand why this is please?
public class PowerUp : MonoBehaviour
{
[SerializeField] float boundry = 1.0f;
[SerializeField] float minSpeed = 3.0f;
[SerializeField] float maxSpeed = 5.0f;
[SerializeField] float minTime = 3.0f;
[SerializeField] float maxTime = 5.0f;
[SerializeField] float yPos = 7.0f;
float xMin;
float xMax;
[SerializeField] float startPosX;
[SerializeField] float moveSpeed;
[SerializeField] float respawnTimer = 15.0f;
void Start()
{
ViewPortCamera();
respawnTimer = Random.Range(minTime, maxTime);
Invoke("BoostPos", respawnTimer);
}
void Update()
{
if (transform.position.y > -yPos)
{
MoveBoost();
}
else
{
StartCoroutine(Respawn());
}
}
IEnumerator Respawn()
{
yield return new WaitForSeconds(respawnTimer);
while (transform.position.y <= -yPos)
{
respawnTimer = Random.Range(minTime, maxTime);
BoostPos();
}
}
private void MoveBoost()
{
transform.Translate(Vector2.down * moveSpeed * Time.deltaTime);
}
private void BoostPos()
{
moveSpeed = Random.Range(minSpeed, maxSpeed);
startPosX = Random.Range(xMin + boundry, xMax - boundry);
Vector2 boostPos = new Vector2(startPosX, yPos);
transform.position = boostPos;
}
private void ViewPortCamera()
{
Camera camera = Camera.main;
xMin = camera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x;
xMax = camera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
}
}
So someone on another site had told me it was absurd to place a coruotine in this code. So I went back to my original code block but have no Idea how to make it spawn at random intervals everything I try is still broken. Tried both Invoke and Time.time in the Update() but with undesirable results.
public class PowerUp : MonoBehaviour
{
[SerializeField] float boundry = 1.0f;
[SerializeField] float minSpeed = 3.0f;
[SerializeField] float maxSpeed = 5.0f;
[SerializeField] float minTime = 2.0f;
[SerializeField] float maxTime = 10.0f;
[SerializeField] float yPos = 7.0f;
float xMin;
float xMax;
[SerializeField] float startPosX;
[SerializeField] float moveSpeed;
[SerializeField] float respawnTime;
private void Awake()
{
ViewPortCamera();
}
void Start()
{
respawnTime = Random.Range(minTime, maxTime);
Invoke("BoostPos", respawnTime);
}
void Update()
{
if (transform.position.y > -yPos)
{
MoveBoost();
}
else
{
BoostPos();
}
}
private void MoveBoost()
{
transform.Translate(Vector2.down * moveSpeed * Time.deltaTime);
}
private void BoostPos()
{
moveSpeed = Random.Range(minSpeed, maxSpeed);
startPosX = Random.Range(xMin + boundry, xMax - boundry);
Vector2 boostPos = new Vector2(startPosX, yPos);
transform.position = boostPos;
}
private void ViewPortCamera()
{
Camera camera = Camera.main;
xMin = camera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x;
xMax = camera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
}
}