说先看下运行之后的效果图
可以不断的从摄像机的位置发射出小球!去推倒墙体
这个写了三个脚本,都挂载在摄像机上面!
下载地址
首先看一下如何通过代码造出墙体出来
这里我们造出一个行列纵的一个墙体
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; zIndex<zNumber; zIndex++) {
for (int rowIndex=0; rowIndex<rowNumber; rowIndex++) {
for (int coluIndex=0; coluIndex<coluNumber; coluIndex++) {
Instantiate (obj, new 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);
}
}
小球发射出之后两秒后释放!