unity3d---案例分析

来源:互联网 发布:明星语音模拟软件 编辑:程序博客网 时间:2024/06/04 19:31
1.用数组保存三个cube,  鼠标点击一下消失一个。

      public GameObject[] cube;//创建数组

      public float speed = 5;//  速度

     int i = 0;

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

     float nextTime = 0.0f;//间隔时间
 
void Update () {

        float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;//设置键盘控制水平移动

        float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;//设置键盘控制垂直移动

        transform.Translate(x, 0, z);//键盘x轴,  z轴
 
        if (Input.GetButton("Fire1") && Time.time > nextTime)//鼠标控制

        {

            nextTime = fireTime + Time.time;// 

                   Destroy(cube[i]);//销毁

                   i++;

                   print(i);
        }
 
        //if (Input.GetButtonDown("Fire1"))
        //{
        //    Destroy(cube[i]);
        //    i++;
        //}
}

2.克隆子弹的发射:

      public GameObject bullet;//创建子弹这个对象
 
    public float fireTime = 0.5f;//发射时间

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

    void Update()
    {
 
        if (Input.GetButton("Fire1") && Time.time > nextTime)
        {
 
            nextTime = fireTime + Time.time;
 
           //GameObject o= GameObject.Instantiate(bullet)as GameObject;//克隆子弹

           //o.rigidbody.AddForce(0, 0, 1000);//添加刚体。

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

            go.rigidbody.AddForce(0, 0, 1000);
 
        }
 
        //if (Input.GetButtonDown("Fire1"))
        //{
        //    GameObject.Instantiate(bullet);         
        //}

创建一个预设体 添加到摄像机  并且加入刚体。
 

更多精彩请关注:http://unity.gopedu.com/
0 0
原创粉丝点击