2017-01-12 5 views
-1

パス上にランダムに旅行のプレーヤーを保つために、私は下のリンクでゲームを再作成しようとしています: https://www.youtube.com/watch?v=88kMVPzybSwユニティどのよう

これまでのところ、私はノーとランダムなパスを生成することに成功したジャンプを入れて(まだジャンプすることはできません問題)。私はこの乱雑さのための情報を保存していないので、私はどのように私のプレーヤーをパスに従うことができますか?

EDIT:これは私のtilemanagerクラスです。このImを使ってランダムパスを生成します。私の質問は、私は上記のゲームのように自分のプレーヤーがこの道に沿って動くように強制することができますか?

パブリッククラス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

+0

あなたがこれまで持っていたコードを投稿してください。ありがとう! –

+0

この質問は非常にあいまいで、特定の形ではありません –

+0

パスの作成方法によって異なります。 –

答えて

0

は1枚のタイルから次を参照して、あなたのようなあなたのパスだろうリンクリストを追跡します。

個々のタイルはすでにプレハブあるので、そのプレハブにシンプル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 
} 

あなたはどのようなタイルのプレーヤーを知っていれば現在オンの場合、パス内の次のタイルをいつでも取得できます。タイルは次のタイルを参照するだけなので、何も破壊することなく前のタイルを安全に取り除くことができます。

0

キューを使用できます。プレーヤーがパス上を移動するようにするには、タイルを作成するときにQueueに追加します。

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