- Home /
There is a generation lag
I have this script here but there is a generation lag. What I mean is the player moves up but it still doesn't generate anything for a while but then sudenly after a while it just generates a load in a few seconds and then repeats the process. So how would I change it so that it generates on time and not a load after some time. Here is the script underneath. Thanks in advance. It would be great if people could help.
using UnityEngine;
using System.Collections;
public class Generation : MonoBehaviour {
public GameObject[] barGroups;
public float minDistanceBetweenBarGroups;
public float maxDistanceBetweenBarGroups;
public Transform playerTransform;
void Start ()
{
}
// Update is called once per frame
float lastPlayerPosition;
void Update ()
{
//if the player 6 units distance from its last distance ,then we will create new bar group
if( playerTransform != null && playerTransform.position.y - lastPlayerPosition > 6)
{
createNewGroup();
lastPlayerPosition = playerTransform.transform.position.y; // to store the present ball position y .
}
}
int groupIndex=1;
void createNewGroup()
{
GameObject barGroupNewInstance = GameObject.Instantiate(barGroups[Random.Range (0, barGroups.GetLength(0))]) as GameObject;
barGroupNewInstance.transform.position= new Vector3(Random.Range(minDistanceBetweenBarGroups, maxDistanceBetweenBarGroups),1.5f*groupIndex,0 ); //this will create new bars group
groupIndex++;
}
}
Answer by rodude16 · Apr 28, 2016 at 10:15 AM
what you could do is change this:
if( playerTransform != null && playerTransform.position.y - lastPlayerPosition > 6)
{
createNewGroup();
lastPlayerPosition = playerTransform.transform.position.y;
to this:
if( playerTransform != null && playerTransform.position.y - lastPlayerPosition > 1)
{
createNewGroup();
lastPlayerPosition = playerTransform.transform.position.y;
this should work. Hope this helps!
You welcome I just played around with the script you gave. It was prety neat as well I have seen more unefficiant ones out there. This for me is the most effieciant I have seen so I will use this in a game I am creating