Reputation: 945
I am building a scrolling game. The camera is set to scroll at a set speed. There is a trigger gameobject connected to the camera that is destroying all the land blocks it creates and tells the landscape to create the next line. This work great but after a small amount of time it just stops creating lines. My thoughts are there's a world size I need to change, but then this needs to be infinate.
Landscape Generator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LandscapeGeneration : MonoBehaviour {
public int sizeX;
public int sizeZ;
public int zIndex = 0;
public GameObject roadBlock;
// Use this for initialization
void Start () {
GenerateTerrain();
}
void GenerateTerrain()
{
for(int x = 0; x < sizeX; x++)
{
for(int z = 0; z < sizeZ; z++)
{
zIndex = z;
Instantiate(roadBlock, new Vector3(x, 0, z), Quaternion.identity);
}
}
}
public void GenerateRow(int row)
{
if (row == zIndex)
{
zIndex++;
for (int x = 0; x < sizeX; x++)
{
Instantiate(roadBlock, new Vector3(x, 0, zIndex), Quaternion.identity);
}
}
}
}
Block Shredder
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockShredder : MonoBehaviour {
public GameObject landscape;
public void OnTriggerEnter(Collider other)
{
LandscapeGeneration script = landscape.GetComponent<LandscapeGeneration>();
float x = other.gameObject.transform.position.x + 44;
Destroy(other.gameObject);
script.GenerateRow(int.Parse(x.ToString()));
}
}
My question is why is the world generation stopping and how can I fix it?
Upvotes: 0
Views: 45
Reputation: 945
I found the problem was, in the Shredder I am passing in the x value as the row, yet the row value should be z. It was getting passed the width of the map and since there's no more blocks to check, it wouldn't create the block.
Upvotes: 0
Reputation: 4110
You didn't give much information, such as what is the approximate world position at which it stops, but I don't think that it is the issue here
Unity should handle big enough world values. check this post where a user tested large (more than 1'000'000) position values. The bigger the value, the less accuracy you'll get from Unity. I remember seeing somewhere that values above 100'000 started losing precision.
My guess is that the shredder stops being called at some point
Here is steps that you can try to debug:
Check if objects really stop being generated. Maybe they are just generated outside of your view. Look for the objects in the editor scene hierarchy
use Debug.Log
to print something in the trigger function. Does it stop being called?
Are you sure that the Shredder doesn't delete itself (Check the parenting in the scene)
When it stops creating, do you still have old objects or did the Shredder delete everything?
As for your design, here a suggestion about the way infinite runners usually work:
The camera should remain fixed. It is your world that is moving backwards, with block being deleted when out of the camera and new ones being created before. That way you avoid going out of range with the position in Unity (even if it would take some time, see the paragraph above). It also become easier to see what happens in your editor, because everything happends more or less at the same place, always. Finally it is easier to place elements because you don't have to find the correct position relative to the camera, or parent everything to the camera.
Switching to such a system shouldn't be too hard and could help you debug since you will clearly see what happens in the editor 3d view
Upvotes: 1