打飞碟游戏编写介绍
流程总览
以下段落是按照自己在编写该程序时的先后顺序安排的,该程序可以在该github地址找到,有兴趣可以download下来跑一跑:打飞碟小游戏
disk(飞碟对象)
首先需要制作飞碟的预制件(prefeb),觉得省事的话可以创建一个球体或者胶囊对象然后调整它的Y轴scale,将它压扁成飞碟状。
我的做法采取了将两个对象放置在一个父的空对象上,并将父对象称作disk:
Ruler(每个disk独有的属性保存类)
public class Ruler
{
public float size;
public Color color;
public int score;
public Vector3 position;
public Vector3 direction;
public float speed;
public Ruler(float size, Color color, Vector3 position, Vector3 direction, float speed)
{
this.size = size;
this.color = color;
this.position = position;
this.direction = direction;
this.speed = speed;
this.score = (int)(this.speed + this.size);
}
}
Ruler类是一个放置在DiskData文件内的并被DiskData类所依赖的一个公有类,
它实际上承担了disk对象的所有属性,包括:
颜色(Color),大小(float),速度(float),位置(Vector3),力(Vector3),分数(int)。
通过构造函数
来生成某个disk的所有属性,然后再通过DiskData对象的set方法来为挂载该脚本的disk对象设置相关属性。
public class DiskData : MonoBehaviour
{
Ruler _ruler;
public Ruler ruler
{
get
{
return _ruler;
}
set
{
_ruler = value;
this.gameObject.GetComponent<Transform>().position = value.position;
this.gameObject.GetComponent<Transform>().localScale = new Vector3(1, 1, 1) * value.size;
this.gameObject.transform.GetChild(0).gameObject.GetComponent<Renderer>().material.color = value.color;
}
}
}
注意:
- 如果对c#语法不熟悉的话,可以去了解一下getÿ