创建plane与小球方块的prefab
创建一堵墙
public class Bricks : MonoBehaviour {
public GameObject bricks;
private int columnNum = 8;
private int rowNum = 6;
// Use this for initialization
void Start () {
for (int i = 0;i<rowNum;i++){
for(int j= 0;j<rowNum;j++){
Instantiate(bricks,new Vector3(j-5,i),Quaternion.identity);
}
}
}
// Update is called once per frame
void Update () {
}
}
小球代码(使小球射出后消失):
public class BallDestory : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Destroy(this.gameObject,3f);
}
}
摄像机代码(能控制视角移动并发射小球)
public class Shoot : 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);
}
}
}