单手操作却独具挑战性的ZigZag游戏是否也让你玩上瘾了呢?今天来教大家使用Unity制作一款ZigZag游戏,附详细步骤及源码。

如果还不知道ZigZag怎么玩,请看:

105131qigyx443zq59y4ac.png
然后需要一个脚本让相机可以跟着小球移动。新建C#脚本命名为CameraFollow,将其绑定到Main Camera上,并添加以下代码:

[C#] 纯文本查看 复制代码using UnityEngine; using System.Collections; public class CameraFollow : MonoBehaviour { public bool shouldRotate = false; // 跟随目标 public Transform target; // 目标距XZ平面的距离 public float distance = 10.0f; // 相机在目标上方的高度 public float height = 5.0f; public float heightDamping = 2.0f; public float rotationDamping = 3.0f; float wantedRotationAngle; float wantedHeight; float currentRotationAngle; float currentHeight; Quaternion currentRotation; void LateUpdate () { if (target){ // 计算当前旋转角度 wantedRotationAngle = target.eulerAngles.y; wantedHeight = target.position.y + height; currentRotationAngle = transform.eulerAngles.y; currentHeight = transform.position.y; currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); // 计算当前高度 currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); // 将角度转换为欧拉角 currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); // 设置相机至XZ平面的距离设置 transform.position = target.position; transform.position -= currentRotation * Vector3.forward * distance; // 设置相机高度 transform.position = new Vector3 (transform.position.x, currentHeight, transform.position.z); // 保持朝向目标 if (shouldRotate) transform.LookAt (target); } } }

到此前期的准备工作完毕,下面来看实现步骤。

第一步

整个游戏都只包含一些基本几何体,所以不需要额外的资源。按照如下步骤在场景中添加平台:

新建Cube,将Position和Rotation均设为(0, 0, 0),Scale设为(5, 1, 5)。
为Cube添加Rigidbody组件并勾选Is Kinematic:
105132umhui9dtzqy93xh9.png
新建Materials文件夹,在文件夹下新建材质Material命名为MatPlatform,将其赋予Cube。将MatPlatform材质的着色器设置为Legacy Shaders-> Bumped Specular,并按照自己的喜好设置Main Color及Shininess等属性。

105132l4os66k464g44z44.png
至此小球所在的平台就设置完毕了,如下:

105133gi781iixr00i8x7x.png
下面按照与平台相同的步骤来创建小球:

新建Sphere,将Position、Rotation及Scale分别设为(0, 1, 0),(0, 45, 0)和(0.5, 0.5, 0.5)。
为Sphere添加Rigidbody组件,展开Constraints,勾选Freeze Rotation后的所有项(X、Y、Z)以避免小球因为摩擦力而发生旋转。

105133tsw5pupvsvt0r44v.png

在Materials文件夹下新建材质Material命名为MatSphere,将其赋予Sphere。将MatSphere材质的着色器设置为Legacy Shaders-> Bumped Specular,并按照自己的喜好设置Main Color及Shininess等属性。

105133t1mlh4wt19pltw35.png
小球创建完成后,下面就让相机可以跟随小球移动。前面已经将CameraFollow脚本绑定到Main Camera上了,接下来勾选CameraFollow脚本的ShouldRotate属性,并将小球(Sphere)拖拽至CameraFollow脚本的Target字段,设置好的属性值:

105134nug1khhbdjzmgj4q.png
运行场景会发现,此时相机会跟随小球移动。

105136bzi9iv8vm1m1fcbi.png
然后将Frictionless分别赋给平台(Cube)Box Collider和小球(Sphere)Sphere Collider上的Material字段:

105137knowkrhrwgf6zghr.png
这样小球就可以流畅地向前移动了,下面来创建平台Prefab以便运行时动态生成平台。新建文件夹Resources,这是由Unity控制的特殊文件夹,所以文件夹名字不要写错。
• 新建Cube重命名为Platform,分别设置Position、Rotation、Scale为(3, 0, 2)、(0, 0, 0)和(1, 1, 1)。
• 将Materials文件夹下的Frictionless赋给Box Collider的Material,MatPlatform赋给Mesh Renderer的Material。

105137th2ecwvrity4hits.png
最后将Platform从层级视图拖拽至项目视图的Resources文件夹下保存为Prefab,删除层级视图的Platform。

SpawnPlatform函数用于生成平台,在游戏开始时及小球走过一个平台后调用。游戏开始时一次性生成50块平台,此后每次小球经过一块平台则重新生成一个。小球已经经过的平台会在等待一秒后坠落,这是通过调用StartCoroutine来实现的。

将Ball脚本绑定到小球(Sphere)上,然后将Resources下的Platform拖拽至Ball脚本的Platform字段:

105137yjwacnjkznsn11ha.png

到此整个游戏就完成了,一共就两个脚本,两个步骤。最后运行效果如下:

110552p2dadt2dgho3z9h7.gif
这还只是最简单的版本,大家还可以继续打磨,比如增加一些道具和游戏计分的逻辑等等。
下载工程文件(请回复本贴哦):

davinci8,如果您要查看本帖隐藏内容请回复



Unity, 教程, 小游戏, zigzag锐亚教育

锐亚教育 锐亚科技 unity unity教程