Unity跑酷类游戏自动生成销毁地图






using UnityEngine;
using System.Collections;


public class move : MonoBehaviour {
Vector3 dir;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
dir = RoadManager .Instance .GetDir (transform .position );
transform .Translate (dir*Time .deltaTime );
}
}




using UnityEngine;
using System.Collections;


public class Road : MonoBehaviour {


public Transform [] wayPoints;//路的标示
public Vector3 endPosition;//最后位置
void OnEnable(){
endPosition = wayPoints [wayPoints .Length - 1].position ;
}
void Awake(){


}
}



using UnityEngine;
using System.Collections;


public class RoadManager : MonoBehaviour {
public static RoadManager Instance;
void Awake(){
Instance = this ;
}
private Road previousRoad;//上一个道路
public Road currentRoad;//当前道路
public Road nextRoad;//下一个道路
public GameObject roadPrefab;//道路预设体
//生成道路
public void CreateRoad(){
GameObject newRoad = (GameObject ) Instantiate (roadPrefab ,nextRoad.endPosition,Quaternion .identity );
//随即一个颜色
float r = Random .Range (0f, 1f);
float g = Random .Range (0f, 1f);
float d= Random .Range (0f, 1f);


Color color = new Color (r,g,d);


newRoad .GetComponentInChildren  ().material .color = color ;
previousRoad = currentRoad ;
currentRoad = nextRoad;
nextRoad = newRoad .GetComponent ();


//Destroy (previousRoad.gameObject  ,1f);
}
Vector3 dir;//代表方向
int targetIndex = 1;
public Vector3 GetDir(Vector3 pos){//参数代表玩家位置
dir = currentRoad .wayPoints [targetIndex].position  - currentRoad .wayPoints [targetIndex -1].position ;
//如果玩家移动到下一个点
if (Vector3 .Distance (pos ,currentRoad .wayPoints [targetIndex ].position )< 1f) {
targetIndex ++;
}
//到达了最后一个点
if (targetIndex == currentRoad .wayPoints .Length ) {
targetIndex = 1;
CreateRoad ();
}
return dir ;
}


}


你可能感兴趣的:(Unity跑酷类游戏自动生成销毁地图)