欢迎来到我们的狗刨网,我们今天主要做了三个小游戏,分别是打砖块,滚动的盒子和打飞机。
一、 首先来看第一个小游戏:打砖块
public GameObject wall;
void Start ()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Instantiate(wall, new Vector3(j - 5, i - 0.6062002f, 50), Quaternion.identity);
}
}
}
小球射击的代码:
public GameObject pos;
public GameObject bullet;
private float fireRate= 0.5F; //初始化发射时间
public float nextFire = 0.0F; //声明子弹间隔
void Update()
{
transform.Translate(newVector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")));
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate; //更新间隔时间
GameObject t = Instantiate(bullet, pos.transform.position, transform.rotation) as GameObject; //实例化子弹
t.rigidbody.AddForce(0,0,1000);
}
二、 滚动的盒子
public float speed=10;//角色移动的速度
private int i=0; //被吃掉的立方体的个数
void Update()
{
float x=Input.GetAxis("Horizontal");
float z=Input.GetAxis("Vertical");
transform.Translate(new Vector3(x*speed,0,z*speed))
}
transform.Rotate (new Vector3(10,10,0)); //为八个立方体添加旋转脚本
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == "Cube") {
GameObject.Destroy(col.gameObject);
if(i==8){
print ("胜利了!!!");
}
else if(i<8)
{
print("吃掉第个"+i+"食物");
i++;
}
欢迎来到我们的狗刨网,我们的网址是: http://www.gopedu.com/