unity之常用核心类习题总结

一、用数组存储3个cube,点击鼠标一次消失一个?(这个题有两种解答)

1、public class DestoryCube : MonoBehaviour {

   public GameObject[] cube;   //创建一个数组的cube

   int i = 0;     //数组的下标是从零开始的

   float fireTime = 0.5f;//发射时间

   float nextTime = 0.0f;//间隔时间

   Update is called once per frame

   void Update () 

   {

       if (Input.GetButton("Fire1") && Time.time>nextTime){

       nextTime = fireTime + Time.time;

       Destroy(cube[i]);

       i++;

       print(i);

   }

2、if (Input.GetButtonDown("Fire1"))

   {

        Destroy(cube[i]);

        i++;

    }

二、用克隆方式发射球体子弹?

   public class Fire : MonoBehaviour {

   public GameObject bullet;  //这是在游戏对象中创建一个bullet

   public float fireTime = 0.5f;//发射时间

   float nextTime = 0.0f;//间隔时间

   void Update () {

   if(Input.GetButton("Fire1") && Time.time>nextTime){

   nextTime = fireTime + Time.time;

   GameObject.Instantiate(bullet);

   GameObject go= GameObject.Instantiate(bullet,new Vector3(0,3,0),Quaternion.identity) as

   GameObject;

   go.rigidbody.AddForce(0,0,1000);

   }

   if (Input.GetButtonDown("Fire1"))

  {

    GameObject.Instantiate(bullet);

  }

这是我们今天讲的两个例题,欢迎来到我们的狗刨网来看新的更新内容。网址是:http://www.gopedu.com/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值