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

来源:互联网 发布:你是我的兄弟网络电影 编辑:程序博客网 时间:2024/04/29 20:47






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  <MeshRenderer >().material .color = color ;
previousRoad = currentRoad ;
currentRoad = nextRoad;
nextRoad = newRoad .GetComponent <Road>();


//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 ;
}


}


0 0