unity打砖块

新建一个平面,放大到四倍,附上材质ide

新建一个方块,加上Rigidbody和材质this

编写脚本,生成一面8*6的墙spa

public class qiang : MonoBehaviour {orm

    public GameObject brick;blog

    private int columnNum = 8;it

    private int rowNum = 6;io

 

    // Use this for initializationform

    void Start () {class

                   for(int i = 0; i < rowNum; i++)transform

        {

            for (int j=0;j<columnNum;j++)

            {

                Instantiate(brick, new Vector3(j - 5, i), Quaternion.identity);

            }

        }

         }

建立一个附上材质的小球

编写发射小球的脚本

public class fashe : MonoBehaviour {

    public GameObject shootPos;

    private float force = 1000;

    public Rigidbody shootBall;

    private float speed = 0.1f;

         // Use this for initialization

         void Start () {

                  

         }

        

         // Update is called once per frame

         void Update () {

        Rigidbody ball;

        if(Input.GetKeyDown(KeyCode .Space))

        {

            ball = Instantiate(shootBall, shootPos.transform.position, Quaternion.identity) as Rigidbody;

            ball.AddForce(force * ball.transform.forward);

        }

        if (Input.GetKey(KeyCode.LeftArrow))

        {

            this.transform.Translate(Vector3.left * speed);

        }

    else if (Input.GetKey(KeyCode.RightArrow))

        {

           this.transform.Translate(Vector3.right * speed);

        }

        else if (Input.GetKey(KeyCode.UpArrow))

        {

           this.transform.Translate(Vector3.up * speed);

        }

        else if (Input.GetKey(KeyCode.DownArrow))

        {

           this.transform.Translate(Vector3.down * speed);

        }

    }

}

添加使发射的小球消失的脚本

 Destroy(this.gameObject, 10f); 

相关文章
相关标签/搜索