- Home /
Trying to increase spawnspeed every milisecond/frame, please Help!
Hello, I am kind of a beginer in Unity and I have the following problem: I am trying to make that every FRAME my spawner spaws faster. Now, I realized that my code doesn´t have a variable with the speed that spawns, no, that would be simple, but instead I have a timer that goes down, spawns a prefabs , resets the timer and deletes the clone of the prefab.
If you can add a variable, a while code or anything that can help, or a link to a post and stuff like that, I would really appreciate that. Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Spawn : MonoBehaviour {
public GameObject bulletPrefab;
GameObject bulletPrefabClone;
public Transform spawnPoint;
public float Timer = 2f;
public GameObject badPrefab;
GameObject badPrefabClone;
void Start() {
}
void Update() {
Timer -= Time.deltaTime;
if (Timer <= 0f)
{
bulletPrefabClone = Instantiate(bulletPrefab, new Vector3(Random.Range(-9, 9), 5f, 0f), transform.rotation) as GameObject;
Timer = 2f;
Destroy(bulletPrefabClone, 2.1f);
badPrefabClone = Instantiate(badPrefab, new Vector3(Random.Range(-9, 9), 5f, 0f), transform.rotation) as GameObject; Timer = 2f; Destroy(badPrefabClone, 2.1f);
}
}
}
NOTE: English isn´t my native language.
May I ask if you have a code for your bullet and bad prefab?
You should add in object pooling instead of instantiating and destroying, not sure why no one mentioned that in their answers.. Also are you just making a new enemy and then immediately destroying it?
Answer by Ytsorf · May 15, 2021 at 12:39 AM
Hellor there you have multiple ways to do that but... you will kill your pc xD try this code
IEnumerator Spawner()
{
if(Timer <= 01f){
StopAllCourutines();//i dont remember if is like this xD
}
else{
Timer -= 0.1f; // put the var in timer pls
bulletPrefabClone = Instantiate(bulletPrefab, new Vector3(Random.Range(-9, 9), 5f, 0f), transform.rotation) as GameObject;
Destroy(Timer - 0.1f);
yield return WaitForSecondsRealtime(Timer);
Spawner();
}
}
Sorry for my code, i dont like to write without the Lanes and spaces xD if you dont want the ienumerator to stop, take out the stopallcourutines and use a diff way to use the timer or put a limit hope it helps <3 btw: Make the call of the spawner in the start DO NOT PUT THE SPAWNER() IN THE UPDATE OR FIXEDUPDATE you will crash ur unity
He can still put the Spawner() on the UPDATE or FIXEDUPDATE as long as he limits the spawn number :)
For example, he can have a variable that will count the number of spawned objects and stop spawning once the maximum spawn has been reached.
Answer by Isaac_Medina225 · May 15, 2021 at 01:38 AM
I hope this can solve your problem:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour
{
public Transform spawnPoint;
public float Timer = 2f;
public GameObject bulletPrefab;
public GameObject badPrefab;
GameObject bulletPrefabClone;
GameObject badPrefabClone;
private float speedIncreaser = 0.05f; // Change this to change the spawn speed
private float acceleration = 1.3f; // Change this to change the acceleration
void Update()
{
Timer -= Time.deltaTime;
if (Timer <= 0f)
{
bulletPrefabClone = Instantiate(bulletPrefab, new Vector3(Random.Range(-9, 9), 0f, 0f), transform.rotation) as GameObject;
badPrefabClone = Instantiate(badPrefab, new Vector3(Random.Range(-9, 9), 0f, 0f), transform.rotation) as GameObject;
if ((speedIncreaser * acceleration) <= 2) // this will make sure that your game wont crash due to spawnspeed. I use 2 because your timer is 2f
{
speedIncreaser *= acceleration;
}
Timer = 2f - speedIncreaser;
Destroy(bulletPrefabClone, Timer);
Destroy(badPrefabClone, Timer);
}
}
}
I made modify a few modification on your code, I remove the unnecessary lines.
Everything is working as I wanted ! I am so happy and I wanted to thank you. I´ve seen people posting their code so the post isn´t useless, so here it goes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class newSpawn : MonoBehaviour
{
public Transform spawnPoint;
public float Timer = 2f;
public GameObject bulletPrefab;
public GameObject badPrefab;
GameObject bulletPrefabClone;
GameObject badPrefabClone;
private float speedIncreaser = 0.05f; // Change this to change the spawn speed
private float acceleration = 1.3f; // Change this to change the acceleration
void Update()
{
Timer -= Time.deltaTime;
if (Timer <= 0f)
{
bulletPrefabClone = Instantiate(bulletPrefab, new Vector3(Random.Range(-9, 9), 9f, 9f), transform.rotation) as GameObject;
badPrefabClone = Instantiate(badPrefab, new Vector3(Random.Range(-9, 9), 9f, 9f), transform.rotation) as GameObject;
if ((speedIncreaser * acceleration) <= 2) // this will make sure that your game wont crash due to spawnspeed. I use 2 because your timer is 2f
{
speedIncreaser *= acceleration;
}
Timer = 2f - speedIncreaser;
Destroy(bulletPrefabClone, Timer);
Destroy(badPrefabClone, Timer);
}
}
}
You're most welcome, you can upvote my answer to flag it as a correct answer :)
Your answer
Follow this Question
Related Questions
What is the most efficient way to spawn objects with probability weights in this scenario? 2 Answers
Trying to move the instantiated position 1 Answer
prevent object from falling to the left side when instantiated (spawned) ? 1 Answer
How to make enemies spawn IN the ground without rising up automatically first? 1 Answer