标签:
在两个月前曾写了一篇《【Unity3D实战】零基础一步一步教你制作跑酷类游戏(1)》,里面一步一步演示了制作跑酷类游戏,然而由于时间原因,只写到了让角色往前移动为止。这个坑一直没有时间去填,(虽然也没多少人看啦),今天刚好有时间完成了一个跑酷类游戏的Demo。放上来给有兴趣的朋友看看。
Demo源码及对应素材下载:链接:http://pan.baidu.com/s/1i4QkkuD 密码:p04w
游戏类型:跑酷类游戏(Demo,非完整游戏)
操作方式:左右方向键(可自己移植到手机端)
游戏要素:
1.游戏角色会自动向前跑,玩家可通过左右方向键让其左右移动
2.游戏中存在障碍物,玩家需避开这些障碍物,否则会因为被障碍物阻挡的原因无法前进
3.当游戏角色因为被阻挡而消失在视野中时,视为失败
4.当游戏角色因为被阻挡而处于偏后方时,会提高移动速度直到回到原本所处的屏幕位置
使用准备好的素材(路面、人物、障碍物),将这些素材制作成Prefab,然后根据自己喜好搭建好场景。如下图:
<span style="white-space:pre"> </span>// 前进移动速度 <span style="white-space:pre"> </span>float moveVSpeed; <span style="white-space:pre"> </span>// 水平移动速度 <span style="white-space:pre"> </span>public float moveHSpeed = 5.0f; <span style="white-space:pre"> </span>// 最大速度 <span style="white-space:pre"> </span>public float maxVSpeed = 10.0f; <span style="white-space:pre"> </span>// 最小速度 <span style="white-space:pre"> </span>public float minVSpeed = 5.0f;其中moveHSpeed、maxVSpeed、minVSpeed声明为public,方便在面板上修改。
float h = Input.GetAxis("Horizontal");
Vector3 vSpeed = new Vector3(this.transform.forward.x, this.transform.forward.y, this.transform.forward.z) * moveVSpeed ;
Vector3 hSpeed = new Vector3(this.transform.right.x, this.transform.right.y, this.transform.right.z) * moveHSpeed * h;
Vector3 jumpSpeed = new Vector3(this.transform.up.x, this.transform.up.y, this.transform.up.z) * jumpHeight * m_jumpState;
this.transform.position += (vSpeed + hSpeed + jumpSpeed) * Time.deltaTime;
保存一下cs文件,切换到Unity,将该脚本挂载在角色对象的身上,保留默认值或手动设置: // 生成障碍物点列表
public List<Transform> bornPosList = new List<Transform>();
// 道路列表
public List<Transform> roadList = new List<Transform>();
// 抵达点列表
public List<Transform> arrivePosList = new List<Transform>();
// 障碍物列表
public List<GameObject> objPrefabList = new List<GameObject>();
// 目前的障碍物
Dictionary<string, List<GameObject>> objDict = new Dictionary<string, List<GameObject>>();
// 道路间隔距离
public int roadDistance; 并定义函数: // 切出新的道路
public void changeRoad(Transform arrivePos)
{
int index = arrivePosList.IndexOf(arrivePos);
if(index >= 0)
{
int lastIndex = index - 1;
if (lastIndex < 0)
lastIndex = roadList.Count - 1;
// 移动道路
roadList[index].position = roadList[lastIndex].position + new Vector3(roadDistance, 0, 0);
initRoad(index);
}
else
{
Debug.LogError("arrivePos index is error");
return;
}
}
void initRoad(int index)
{
string roadName = roadList[index].name;
// 清空已有障碍物
foreach(GameObject obj in objDict[roadName])
{
Destroy(obj);
}
objDict[roadName].Clear();
// 添加障碍物
foreach(Transform pos in bornPosList[index])
{
GameObject prefab = objPrefabList[Random.Range(0, objPrefabList.Count)];
Vector3 eulerAngle = new Vector3(0, Random.Range(0, 360), 0);
GameObject obj = Instantiate(prefab, pos.position, Quaternion.EulerAngles(eulerAngle)) as GameObject;
obj.tag = "Obstacle";
objDict[roadName].Add(obj);
}
} void Start () {
foreach(Transform road in roadList)
{
List<GameObject> objList = new List<GameObject>();
objDict.Add(road.name, objList);
}
initRoad(0);
initRoad(1);
} 然后打开之前的moveController.cs,声明变量: void OnTriggerEnter(Collider other)
{
// 如果是抵达点
if (other.name.Equals("ArrivePos"))
{
gameManager.changeRoad(other.transform);
}
// 如果是透明墙
else if (other.tag.Equals("AlphaWall"))
{
// 没啥事情
}
// 如果是障碍物
else if(other.tag.Equals("Obstacle"))
{
}
} 呼,一大串代码,大家敲的累不累,什么!你是copy过去的?太过分了!我要拿刀子了!// 当人物与摄像机距离小于cameraDistance时 让其加速
if(this.transform.position.x - cameraTransform.position.x < cameraDistance)
{
moveVSpeed += 0.1f;
if (moveVSpeed > maxVSpeed)
{
moveVSpeed = maxVSpeed;
}
}
// 超过时 让摄像机赶上
else if(this.transform.position.x - cameraTransform.position.x > cameraDistance)
{
moveVSpeed = minVSpeed;
cameraTransform.position = new Vector3(this.transform.position.x - cameraDistance, cameraTransform.position.y, cameraTransform.position.z);
}
// 摄像机超过人物
if(cameraTransform.position.x - this.transform.position.x > 0.0001f)
{
Debug.Log("你输啦!!!!!!!!!!");
gameManager.isEnd = true;
}void OnGUI()
{
if (gameManager.isEnd)
{
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.MiddleCenter;
style.fontSize = 40;
style.normal.textColor = Color.red;
GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100), "你输了~", style);
}
}【Unity3D实战】零基础一步一步教你制作跑酷类游戏(填坑完整版)
标签:
原文地址:http://blog.csdn.net/sun15980/article/details/51093048