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  <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
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值