Unity 3D 泰课网打砖块实例 可以用来发射炮弹

说先看下运行之后的效果图
Unity 3D 泰课网打砖块实例 可以用来发射炮弹_第1张图片
可以不断的从摄像机的位置发射出小球!去推倒墙体
这个写了三个脚本,都挂载在摄像机上面!
下载地址
首先看一下如何通过代码造出墙体出来
这里我们造出一个行列纵的一个墙体

using UnityEngine;
using System.Collections;

public class BrickBld : MonoBehaviour {

    public GameObject obj;//需要的砖块
    public int coluNumber=10;//列数
    public int rowNumber = 6;//行数
    public int zNumber = 2;//纵数
    // Use this for initialization
    void Start () {
        //Instantiate (obj);
        for (int zIndex=0; zIndexfor (int rowIndex=0; rowIndexfor (int coluIndex=0; coluIndexnew Vector3 (coluIndex - 10, rowIndex + 0.5f, zIndex+5.0f), Quaternion.identity);
                                }
                        }
                }
    }

    // Update is called once per frame
    void Update () {

    }
}

最内层的循环造出一行,上一次造出一列,最外层造一纵,
下面看一下打击的代码和移动摄像机的代码

using UnityEngine;
using System.Collections;

public class Biu : MonoBehaviour {

    public GameObject BiuPosition;//位置点
    public float speed = 1000.0f;//s速度
    public Rigidbody bill;//物体
    // Use this for initialization
    public float speedMove = 100.0f;

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton (1)) {

                        Rigidbody rig = Instantiate (bill, BiuPosition.transform.position, Quaternion.identity) as Rigidbody;
                        rig.AddForce (speed * BiuPosition.transform.forward);

                }
        float h = Input.GetAxis ("Horizontal")*speedMove * Time.deltaTime;
        float v = Input.GetAxis ("Vertical") * speedMove * Time.deltaTime;

        transform.Translate (h,v,0.0f);
    }

}

给小球一个向前的力,将小球射出!
释放小球的代码

using UnityEngine;
using System.Collections;

public class Destory : MonoBehaviour {


    // Use this for initialization
    void Start () {
        //Destory (this.gameObject, 0.5f);
        Destroy (this.gameObject, 2.5f);
    }

    // Update is called once per frame
    void Update () {
//      Destory (this.gameObject, 0.5f);
    }
}

小球发射出之后两秒后释放!

你可能感兴趣的:(Unity,3D,开发从入门到精通,unity,脚本,实例)