Games Basic Premise
Ball rolls along procedurally generated path. Click to changer directions. Don't fall off. I have a script dedicated to the path. The path is made up of 20 cubes, along the x+ and z+ axis. Path is generated at the start then changes randomly as you go.
Problem
In my path designer script, I also control another cube I call "Fake Path". It's a single cube to throw off the player. But whenever the control script runs it freezes my game, I'm assuming it's processing something. Then after awhile it might resume the game. I know my code is probably a little wonky since I'm still getting the basics of game design. Key_CircLinkList
is a circular linked list. With each element storing an index.
void Start(){
sphere = GameObject.Find("Sphere");
sphereScript = sphere.GetComponent<SphereMove>();
startingCube = GameObject.Find("StartCube");
fakeCube = GameObject.Find("FakeCube");
randIndex = 10;
isGenerated = true;
pathV2 = new Key_CircLinkList<GameObject>();
pathV2.AddFirst(startingCube);
for (int i = 0; i < pathLength-1; i++)
{
float random = Random.value;
pathV2.AddLast(Instantiate(startingCube, this.transform));
if (random <= 0.5f)
{
pathV2.GetLast().GetElement().transform.position = new Vector3(pathV2.SelectInd(i).GetElement().transform.position.x + distMove, pathV2.SelectInd(i).GetElement().transform.position.y, pathV2.SelectInd(i).GetElement().transform.position.z);
//Setting initial movement direction
if (i == 0)
{
sphereScript.l_or_r = true;
}
}
else
{
pathV2.GetLast().GetElement().transform.position = new Vector3(pathV2.SelectInd(i).GetElement().transform.position.x, pathV2.SelectInd(i).GetElement().transform.position.y, pathV2.SelectInd(i).GetElement().transform.position.z + distMove);
//Setting initial movement direction
if (i == 0)
{
sphereScript.l_or_r = false;
}
}
}
if (pathV2.SelectInd(randIndex).GetNext().GetElement().transform.position.x >
pathV2.SelectInd(randIndex).GetElement().transform.position.x)
{
if (pathV2.SelectInd(randIndex).GetNext(2).GetElement().transform.position.z > pathV2.SelectInd(randIndex).GetElement().transform.position.z){
fakeCube.transform.position = new Vector3(pathV2.SelectInd(randIndex).GetNext().GetElement().transform.position.x + distMove,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(randIndex).GetElement().transform.position.z);
}else{
fakeCube.transform.position = new Vector3(pathV2.SelectInd(randIndex).GetElement().transform.position.x,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(randIndex).GetElement().transform.position.z + distMove);
}
}else{
if (pathV2.SelectInd(randIndex).GetNext(2).GetElement().transform.position.x > pathV2.SelectInd(randIndex).GetNext().GetElement().transform.position.x){
fakeCube.transform.position = new Vector3(pathV2.SelectInd(randIndex).GetNext().GetElement().transform.position.x,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(randIndex).GetElement().transform.position.z + distMove);
}else{
fakeCube.transform.position = new Vector3(pathV2.SelectInd(randIndex).GetElement().transform.position.x + distMove,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(randIndex).GetElement().transform.position.z);
}
}
isGenerated = false;
cubeX = pathV2.GetLast().GetElement().transform.position.x;
cubeZ = pathV2.GetLast().GetElement().transform.position.z;
updatedX = cubeX;
updatedZ = cubeZ;
}
// Update is called once per frame
void Update()
{
SpawnCube();
}
private void SpawnCube()
{
float random = Random.value;
//Updates path to move cubes by seeing if (position of the first cube) < ((position of the sphere) - however long the trail of cubes should be)
if ((pathV2.GetSelect().GetElement().transform.position.x + pathV2.GetSelect().GetElement().transform.position.z) < ((sphere.transform.position.x + sphere.transform.position.z) - 5.0f))
{
if (random <= 0.5f)
{
updatedX = updatedX + distMove;
pathV2.GetSelect().GetElement().transform.position = new Vector3(updatedX, pathV2.GetSelect().GetPrev().GetElement().transform.position.y, updatedZ);
}
else
{
updatedZ = updatedZ + distMove;
pathV2.GetSelect().GetElement().transform.position = new Vector3(updatedX, pathV2.GetSelect().GetPrev().GetElement().transform.position.y, updatedZ);
}
pathV2.Rotate(1);
}
if(isGenerated == false){
randIndex = pathV2.SelectInd(prevIndex).GetNext(10).GetIndex();
prevIndex = randIndex;
isGenerated = true;
Debug.Log("Random Index: " + randIndex);
}
if ((fakeCube.transform.position.x + fakeCube.transform.position.z) + 2 < (sphere.transform.position.x + sphere.transform.position.z) &&
((pathV2.SelectInd(prevIndex).GetElement().transform.position.x + pathV2.SelectInd(prevIndex).GetElement().transform.position.z) > (sphere.transform.position.x + sphere.transform.position.z))
){
if (pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x > pathV2.SelectInd(prevIndex).GetElement().transform.position.x){
if(pathV2.SelectInd(prevIndex).GetNext(2).GetElement().transform.position.z > pathV2.SelectInd(prevIndex).GetElement().transform.position.z){
fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x+distMove,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(prevIndex).GetElement().transform.position.z);
}
else{
fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetElement().transform.position.x,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(prevIndex).GetElement().transform.position.z + distMove);
}
}else{
if (pathV2.SelectInd(randIndex).GetNext(2).GetElement().transform.position.x > pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x)
{
fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(prevIndex).GetElement().transform.position.z + distMove);
}
else
{
fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetElement().transform.position.x + distMove,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(prevIndex).GetElement().transform.position.z);
}
}
isGenerated = false;
}
}
When this bit runs, it freezes:
if ((fakeCube.transform.position.x + fakeCube.transform.position.z) + 2 < (sphere.transform.position.x + sphere.transform.position.z) &&
((pathV2.SelectInd(prevIndex).GetElement().transform.position.x + pathV2.SelectInd(prevIndex).GetElement().transform.position.z) > (sphere.transform.position.x + sphere.transform.position.z))
){
if (pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x > pathV2.SelectInd(prevIndex).GetElement().transform.position.x){
if(pathV2.SelectInd(prevIndex).GetNext(2).GetElement().transform.position.z > pathV2.SelectInd(prevIndex).GetElement().transform.position.z){
fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x+distMove,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(prevIndex).GetElement().transform.position.z);
}
else{
fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetElement().transform.position.x,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(prevIndex).GetElement().transform.position.z + distMove);
}
}else{
if (pathV2.SelectInd(randIndex).GetNext(2).GetElement().transform.position.x > pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x)
{
fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(prevIndex).GetElement().transform.position.z + distMove);
}
else
{
fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetElement().transform.position.x + distMove,
pathV2.GetSelect().GetElement().transform.position.y,
pathV2.SelectInd(prevIndex).GetElement().transform.position.z);
}
}
isGenerated = false;
}
Any help as to what to do, and maybe tips, that would be awesome.