unity之小游戏

欢迎来到我们的狗刨网,我们今天主要做了三个小游戏,分别是打砖块,滚动的盒子和打飞机。

一、      首先来看第一个小游戏:打砖块


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/ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值