I have created a quad, and made it a prefab. Now I need to clone it one after another at equal interval of time and space. I check for a time period of 0.2 seconds and clone the next set of prefabs. but this is what happens. the first few clones are not fine. the time gap between clones is erratic and seems out of control. advice me for a solution.
the prefab is a quad. in update routine i check for 0.2 seconds delay and clone it. the quads has a script component which makes it move to the left
transform.position += m_direction * m_speed * Time.deltaTime; // this is how the quads move (update routine of the quad)
timeInterval += Time.deltaTime;
if (timeInterval > 0.5) {
redRunner = Instantiate (Resources.Load (“Prefabs/TRed”), instantiantionPos, Quaternion.identity) as GameObject;} // this is where i clone objects in update routine of an empty game object.
Also, in your code examples - which are much better to read with code tags - you finish the if-statement without resetting the timeInterval variable to 0. This will make your game load (from resources) and instantiate the quad every frame, which can be quite heavy.
I’d recommend to simply make a public variable of type GameObject and assign your prefab to it. If you only need a certain amount of these quads, you could also use a pooling technique which additionally avoids all the instantiation in Update, so you can just re-use quads again by moving them around when they’re no longer needed.
Ah okay.
I tested this myself and it seems to work just fine. Note that i also just used instantiate for this example, but in a project it does make sense to avoid instantiation.
This is the script which spawns the prefabs.
private float timeSinceInstantiation = 0f;
public float interval = 1f;
public GameObject prefab;
public Vector3 instantiantionPos = Vector3.zero;
void Update()
{
timeSinceInstantiation += Time.deltaTime;
if (timeSinceInstantiation >= interval)
{
Instantiate(prefab, instantiantionPos, Quaternion.identity);
timeSinceInstantiation = 0;
}
}
The script on the prefab contains the code you posted above in order to move the objects.
You’re probably experiencing some sort of large overhead when your game initially starts. This causes Time.deltaTime to be a higher than normal value which means your object moves at a larger interval. If the spacing needs to be even then only use deltaTime to measure time and keep a count of how may things you have spawned and then derive the position by multplying that count by the spacing.
thank you for your responses. may be the overhead issue should be sorted out. i will consider @KelsoMRK advice on spacing, thanks @Suddoha for the pooling technique. i hope it will work fine. will test it