unity跑酷怎么添加⾦币_【Unity3D实战】零基础⼀步⼀步教你制作跑酷类游戏(填坑完。。。
在两个⽉前曾写了⼀篇《【Unity3D实战】零基础⼀步⼀步教你制作跑酷类游戏(1)》,⾥⾯⼀步⼀步演⽰了制作跑酷类游戏,然⽽由于时间原因,只写到了让⾓⾊往前移动为⽌。这个坑⼀直没有时间去填,(虽然也没多少⼈看啦),今天刚好有时间完成了⼀个跑酷类游戏的Demo。放上来给有兴趣的朋友看看。
游戏简要说明
游戏类型:跑酷类游戏(Demo,⾮完整游戏)
操作⽅式:左右⽅向键(可⾃⼰移植到⼿机端)
游戏要素:
1.游戏⾓⾊会⾃动向前跑,玩家可通过左右⽅向键让其左右移动
2.游戏中存在障碍物,玩家需避开这些障碍物,否则会因为被障碍物阻挡的原因⽆法前进
3.当游戏⾓⾊因为被阻挡⽽消失在视野中时,视为失败
4.当游戏⾓⾊因为被阻挡⽽处于偏后⽅时,会提⾼移动速度直到回到原本所处的屏幕位置
游戏场景搭建
unity3d入门使⽤准备好的素材(路⾯、⼈物、障碍物),将这些素材制作成Prefab,然后根据⾃⼰喜好搭建好场景。如下图:
游戏脚本编写
游戏⾓⾊控制器moveController:
新建⼀个C#⽂件,命名为moveController,然后将其打开。
由于⾓⾊需要向前、左、右三个⽅向移动,所以我们需要有其在前进⽅向上的速度与左右⽅向上的速度,分别命名为:moveVSpeed、moveHSpeed,
于是我们可以得到以下脚本:
// 前进移动速度
float moveVSpeed;
// ⽔平移动速度
public float moveHSpeed = 5.0f;
// 最⼤速度
public float maxVSpeed = 10.0f;
// 最⼩速度
public float minVSpeed = 5.0f;其中moveHSpeed、maxVSpeed、minVSpeed声明为public,⽅便在⾯板上修改。
接下来,在Start()函数中定义moveVSpeed的初始值:
moveVSpeed = minVSpeed;
在Update()中使⼈物能移动起来:
float h = Input.GetAxis("Horizontal");
Vector3 vSpeed = new ansform.forward.x, ansform.forward.y, ansform.forward.z) * moveVSpeed ;
Vector3 hSpeed = new ansform.right.x, ansform.right.y, ansform.right.z) * moveHSpeed * h;
Vector3 jumpSpeed = new ansform.up.x, ansform.up.y, ansform.up.z) * jumpHeight *
m_jumpState;
运⾏游戏,看看是否能成功跑起来,并且能通过左右键控制⼈物左右移动。
看着⼈物越跑越远越跑越远,最后消失在远⽅......诶!教练,这和说好的不⼀样啊!⼈物咋不见了?
咳咳,这是因为我们没有让摄像机跟随它的原因,接下来,我们让摄像机与⼈物⼀起移动~
打开刚才的C#⽂件,声明⼀个public的变量
// 摄像机位置
public Transform cameraTransform;
在Update()函数中,添加以下代码:
// 设置摄像机移动速度
Vector3 vCameraSpeed = new ansform.forward.x, ansform.forward.y, ansform.forward.z) *
// 让摄像机跑起来
cameraTransform.position += (vCameraSpeed) * Time.deltaTime;
注意到没,这⾥我们所定义的摄像机的移动速度与⼈物移动速度有点差别:
1.摄像机没有左右移动
2.摄像机的速度恒定为minVSpeed,也就是我们所定义的⼈物的最⼩移动速度(当然这个时候⼈物也⼀直是以这个速度在移动)
转到Unity,查看⼈物⾝上的Move Controller组件,现在这⾥应该多了⼀个变量等你设置:
我们将摄像机拖动到camera Transform处,再运⾏游戏。这时候你应该能看到⼈物在不断往前⾛,但在屏幕上的位置是没有变化的,因为摄像机⼀起移动了。
⼈物⾛着⾛着 哎呀 前⾯怎么没路了?别急,让我们来让路⽆限延长起来~
⾸先我们将道路的GameObject复制⼏个,我这⾥是总共有3个道路的GameObject,分别命名为Road1,Road2,Road3
然后在每⼀个Road下,添加⼀个Cube,将Cube的Mesh Renderer关闭,并将其Box Collider的Is Trigger勾上。命名为ArrivePos。(我才不会告诉你们这⼀步应该在上⼀⾏之前做呢!)
将多条道路拼好,连成⼀条笔直的公路。
然后新建⼀个空物体,命名为GameManager,为其新建C#Script  GameManager.cs,然后打开该脚本。
声明⼀下多个变量:(注意引⽤命名空间using System.Collections.Generic;
// ⽣成障碍物点列表
public List bornPosList = new List();
// 道路列表
// 抵达点列表
public List arrivePosList = new List();
// 障碍物列表
public List objPrefabList = new List();
// ⽬前的障碍物
Dictionary> objDict = new Dictionary>();
// 道路间隔距离
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);
}
}
在Start()中:
void Start () {
foreach(Transform road in roadList)
{
List objList = new List();
objDict.Add(road.name, objList);
}
initRoad(0);
initRoad(1);
}然后打开之前的moveController.cs,声明变量:
// 游戏管理器
public GameManager gameManager;
定义函数:
void OnTriggerEnter(Collider other)
{
// 如果是抵达点
if (other.name.Equals("ArrivePos"))
{
gameManager.ansform);
}
// 如果是透明墙
else if (other.tag.Equals("AlphaWall"))