- Home /
Instantiate objects faster and faster over game time,Instantiate objects faster and faster over time
Hi! I'm a unity newbie working on a game where I want objects ("Thoughts") to instantiate faster and faster over time. I want to start at around 1/second and then end up with an overwhelmingly fast 1/update. Thank you so much for looking and helping!
Here's my messy code, which has some unfinished attempted floats for timers and st uff in it, it's super imperfect, but should give you some idea? Let me know if you have any questions, and thank you again.
public class ThoughtSpawner : MonoBehaviour
private float startTime = 0;
private float TimePassed = 0;
public float InstantiationTimer = 1f;
public GameObject thoughtPrefabVar;
public float projectileSpeed = 100;
// Amount each Thought will grow each frame
public float scalingFactor = 1.05f;
void Awake()
{
startTime = Time.time;
}
// Use this for initialization
void Start()
{
// Instantiate(cubePrefabVar);
}
// Update is called once per frame
void Update()
{
CreateThought();
}
void CreateThought()
{
InstantiationTimer -= Time.deltaTime;
if (InstantiationTimer <= 0)
{
GameObject Thought = Instantiate(thoughtPrefabVar);
InstantiationTimer = 1f;
Thought.transform.position = transform.position;
Rigidbody rigidB = Thought.GetComponent<Rigidbody>();
rigidB.velocity = Vector3.up * projectileSpeed;
}
}
} ##
Answer by QKlon · Jun 02, 2018 at 06:15 AM
The default unity way to set timeouts is using coroutines. At least for the beginning it is also the easiest approach. You could substract e.g. 1/10 of the interval duration each call.
using System.Collections;
using UnityEngine;
public class AccelTimer : MonoBehaviour {
void Start()
{
StartCoroutine(Interval());
}
IEnumerator Interval()
{
float intervalTime = 5; // start with 5 seconds
while(true) // endless loop
{
yield return new WaitForSeconds(intervalTime);
// this is first done 5 seconds after start.
// move it before yield return to start the action immediately
print(Time.time);
// change interval
intervalTime -= intervalTime/10;
}
}
}
Your answer
Follow this Question
Related Questions
Instantiate over time? 1 Answer
increasing rate of spawn on button press 1 Answer
instantiating at a random time 1 Answer
Instantiate clones with error. 1 Answer
Instatiate object in every given second problem C# 2 Answers