简体   繁体   中英

How to generate ground in unity2d endless runner

I currently write a game, and I have 2 scripts that generate the ground in the game. However, instead of generating them as the player comes to the end of one ground, they're generated as soon as the game starts. I don't want this to happen.

Does someone know why it happens?
Please help me fix this.

Thanks!

This is my code:

Script 1:

public class ObjectPooler : MonoBehaviour
 {

public GameObject pooledObject;
public int pooledamnt;
List<GameObject> pooledObjects;
// Start is called before the first frame update
void Start()
{
    pooledObjects = new List<GameObject>();
    for (int i = 0; i < pooledamnt; i++)
    {
        GameObject obj = (GameObject)Instantiate(pooledObject);
        obj.SetActive(false);
        pooledObjects.Add(obj);
    }

}
public GameObject GetPooledObject()
{
    for (int i = 0; i < pooledObjects.Count; i++)
    {
        if (pooledObjects[i].activeInHierarchy)
        {
            return pooledObjects[i];
        }
    }
    GameObject obj = (GameObject)Instantiate(pooledObject);
    obj.SetActive(false);
    pooledObjects.Add(obj);
    return obj;

}

// Update is called once per frame
void Update()
{

}
}

Script 2:

    public class 
  GroundGenerator : MonoBehaviour
  {
    public GameObject thePlatform;
    public Transform GenOnPoint;
    public float DistanceBetween;
    private float PlatformWidth;
public float DistanceBewtweenmin;
public float Di stanceBetweenmax;
  public ObjectPooler objectpool;
public GameObject[] thePlatforms;
   private int platformSelecter;

// Start is called before the first frame update
void Start()
{
    
}
// Update is called once per frame
void Update()
{
   
      
        

        GameObject newPlatform = objectpool.GetPooledObject();
        newPlatform.transform.position = transform.position;
        newPlatform.transform.rotation = transform.rotation;
    newPlatform.SetActive(true);
    

    
}
}

Here is a basic script to generate ground as the player moves :

   using UnityEngine;

    public class GroundGeneration : MonoBehaviour
    {
    public float ClosestDistacnceFromPlayer;

    public GameObject GroundTile;

    public float TileWidth;

    public Transform Player;

    void Start()
    {
        //Spawn a tile so that the player won't fall off at the start
        SpawnTile(1);

    }

    void SpawnTile(int n)
    {
        int i = 0;

        //Spawn n tiles
        while (i < n)
        {
            Instantiate(GroundTile, transform.position, Quaternion.identity);
            i++;

            //Teleport the "Ground generator" to the end of the tile spawned
            transform.position += TileWidth * Vector3.right; // Or use Vector3.forward if you want to generate ground on z axis.

        }
    }


    void FixedUpdate()
    {
        if (Vector3.Distance(Player.position, transform.position) <= ClosestDistacnceFromPlayer)
        {
            SpawnTile(1);
        }
    }


}

The GroundTile should have its origin at the point where it meets with the previous tile and it should be a prefab. The Ground will start generating at the position of the object containing the ground generation script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM