Unity如何让玩家在路径上随机旅行

时间:2017-01-12 16:13:16

标签: unity3d

我正在尝试在以下链接中重新创建游戏: https://www.youtube.com/watch?v=88kMVPzybSw

到目前为止,我已经成功生成了一个没有跳转的随机路径(跳转不成问题)。我没有存储这种随机性的信息,那么我怎样才能让我的玩家遵循这条路?

编辑:这是我的tilemanager类。使用此Im生成随机路径。我的问题是如何像上面的游戏一样强迫我的玩家自己沿着这条路走?

public class TileManager : MonoBehaviour {

    public GameObject[] tilePrefabs;
    public GameObject currentTile;

    private Stack<GameObject> leftTiles = new Stack<GameObject>();
    private Stack<GameObject> topTiles = new Stack<GameObject>();


    private static TileManager instance;

    public static TileManager Instance
    {
        get
        {
            if (instance == null) {
                instance = GameObject.FindObjectOfType<TileManager>();
            }
            return instance;
        }
    }

    public Stack<GameObject> LeftTiles
    {
        get {   return leftTiles; }
        set {   leftTiles = value; }
    }

    public Stack<GameObject> TopTiles
    {
        get{    return topTiles; }
        set{    topTiles = value; }
    }



    // Use this for initialization
    void Start() {

        CreateTiles(50);

        for (int i = 0; i < 20; i++) {
            SpawnTile();
        }


    }

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

    }

    public void CreateTiles(int amount)
    {
        for (int i = 0; i < amount; i++) {
            LeftTiles.Push(Instantiate(tilePrefabs[0]));
            TopTiles.Push(Instantiate(tilePrefabs[1]));
            LeftTiles.Peek().name = "LeftTile";
            LeftTiles.Peek().SetActive(false);
            TopTiles.Peek().name = "TopTile";
            TopTiles.Peek().SetActive(false);
        }

    }

    public void SpawnTile() {

        //before we spawn we need to check if theres enough tiles, if not create more.
        if (LeftTiles.Count == 0 || TopTiles.Count == 0) {
            CreateTiles(10);
        }

        int randomIndex = Random.Range(0,2);

        if (randomIndex == 0) {
            GameObject temp = LeftTiles.Pop();
            temp.SetActive(true);
            temp.transform.position = currentTile.transform.GetChild(0).transform.GetChild(randomIndex).position;
            currentTile = temp;
        }
        else if(randomIndex == 1)
        {
            GameObject temp = TopTiles.Pop();
            temp.SetActive(true);
            temp.transform.position = currentTile.transform.GetChild(0).transform.GetChild(randomIndex).position;
            currentTile = temp;
        }

        int spawnPickUp = Random.Range(0 , 10);
        if (spawnPickUp == 0) {
            currentTile.transform.GetChild(1).gameObject.SetActive(true);
        }
        //currentTile = (GameObject) Instantiate(tilePrefabs[randomIndex], currentTile.transform.GetChild(0).transform.GetChild(randomIndex).position, Quaternion.identity);
    }

    public void ResetGame() {
        Application.LoadLevel(Application.loadedLevel);
    }
}

this is a snap shot of my scene view

2 个答案:

答案 0 :(得分:0)

跟踪您的路径,就像链接列表一样,从一个磁贴到下一个磁贴的引用。

每个单独的图块已经是预制件,因此在该预制件中添加一个简单的MonoBehavior脚本:

public class Tile : MonoBehaviour
{
    public Tile NextTile;
}

然后你的spawn函数可能是:

public void SpawnTile() 
{
    //Check if you need to create tile

    int randomIndex = Random.Range(0,2);

    if (randomIndex == 0) 
    {
        //Create the new tile and assign its position
        GameObject temp = LeftTiles.Pop();
        temp.SetActive(true);
        temp.transform.position = currentTile.transform.GetChild(0).transform.GetChild(randomIndex).position;

        //NEW: Assign the previous tile
        currentTile.GetComponent<Tile>().NextTile = temp;

        //Update current tile
        currentTile = temp;
    }
    else if(randomIndex == 1)
    {
       //Similar for top tiles
    }

    //Spawn Pickups
}

如果您知道播放器当前处于哪种磁贴,您始终可以获取路径中的下一个磁贴。由于瓷砖仅引用下一个瓷砖,因此您可以安全地摆脱以前的瓷砖而不会破坏任何东西。

答案 1 :(得分:0)

您可以使用队列。创建一个磁贴时,如果你想让玩家在路径上移动,只需给他一个完整的Queue对象。

void MoveOnPath()
{
  Vector3 nextPos = Queue.Dequeue().position;
  And use transform or rigidbody to move player in right direction.
  if(playerpos==nextpos)
    MoveOnPath();
}