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);
}
}
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();
}