[c#] Increase speed of instantiating over time!?
So i need this object that im instantiating to spawn faster over time. Simple but i just can't figure it out, ive only just started on unity and so far its much simpler and easier than UE4 for what i need but i just cant figure this one thing out!
If you fellas have any other tips or perhaps a better way to do my code PLEASE share, im at a stump and i need help, thanks!!
also this may come of use, i have another script applied to something else that has a timer so that i could keep track of how long the games been going and perhaps thought i could use that timer and increase the speed as the number goes higher.
Some of this code i got from other sources and just edited a few bitty lines to my needs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
//Spawn this object
public GameObject spawnObject;
public float maxTime = 2;
public float minTime = .01f;
public float maxOffset = 10;
public float minOffset = -10;
private Vector3 NewPos;
//current time
private float time;
//The time to spawn the object
private float spawnTime;
private float RandomOffSet;
void Start()
{
SetRandomTime();
time = minTime;
SetRandomOffSet();
}
private void Update()
{
}
void FixedUpdate()
{
//Counts up
time += Time.deltaTime;
//Check if its the right time to spawn the object
if (time >= spawnTime)
{
SpawnObject();
SetRandomTime();
SetRandomOffSet();
}
}
//Spawns the object and resets the time
void SpawnObject()
{
time = minTime;
NewPos = new Vector3(transform.position.x + RandomOffSet, transform.position.y, transform.position.z);
Instantiate(spawnObject, NewPos, transform.rotation);
}
//Sets the random time between minTime and maxTime
void SetRandomTime()
{
spawnTime = Random.Range(minTime, maxTime);
}
void SetRandomOffSet()
{
RandomOffSet = Random.Range(minOffset, maxOffset);
}
}
A few things you can think about while designing this:
What's the rate at which you want the spawn timer to increase in frequency?
What's the base frequency of spawns over time?
How much time has elapsed since the last spawn?
How do you normalize things for both framerate and real time?
If you can answer those questions, you can write the code yourself.
Answer by binaryuniverse · Jun 29, 2018 at 08:05 PM
I have no reason to think that this won't work as is, but generally speaking the purpose of the Time.deltaTime function is for the Update function (as opposed to fixedUpdate.) Unless you have any reason for not doing so, I would move all of this to Update, especially since some frames will definitely take more time than others with your setup.
Your issue is that you need the time increments to get smaller while still being random. Just use a function for setting the random value that is designed to get lower with time (or count)
EDIT: I just noticed, you do use a minTime and maxTime. You just don't update them.
A simple version would be that each time (time >= spawnTime) evaluates to true, you lower a new variable called maxTime. Start it at 1 and drop it by .025 each time. That will give you a linear drop. You can use exponential functions instead to make it start fast and slow down or start slow and speed up (Something like Mathf.sqrt(1 - maxTime) perhaps.)
As an example, in Update, in the code block:
if (time >= spawnTime)
{
maxTime -= 0.025f;
SpawnObject();
SetRandomTime();
SetRandomOffSet();
}
I would also add that you should look into the concept of object pools if you're going to do this a lot.
i definitely will, ive just started so anythings helpful at this point XD
I dont know why i didnt think of that! Im going to see if it works how i need it but in the meantime do you know how i could implement a max low point? so that will keep going lower and lower but i dont want it to past a certain point?
THAN$$anonymous$$S!!
if (time >= spawnTime)
{
SpawnObject();
SetRandomTime();
SetRandomOffSet();
if (maxTime > .5)
{
maxTime -= 0.075f;
}
}
}
That makes it that when the max time until something else spawns hits .5 it will stop decreasing time.
Thanks for the help!
Looks like you answered your own question. Glad to help. :-)
Your answer
Follow this Question
Related Questions
How can I make an object clone in the time that I indicate it to appear? 0 Answers
Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers
Spawn a random object on more than one position 1 Answer
Problem with Instantiated clones 1 Answer
What is the most efficient way to spawn in a lot of cubes, like 10k? [c#] 2 Answers