操作步骤
- 新建一个plane,修改位置坐标为(0,0,0)导入资源包,新建一个空对象,命名为Wall,用于存放后面脚本生产的小方块
- 新建一个脚本,重命名为Brick,用于生成由Cube组成的墙。把脚本附给Wall,脚本复制对象选择资源包中的小方块的预制件。脚本内容如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Brick : MonoBehaviour { public GameObject brick; private int columnNum=8;//列数 private int rowNum = 6;//行数 // Start is called before the first frame update void Start() { for(int i = 0; i < rowNum; i++) { for(int j = 0; j < columnNum; j++) { Instantiate(brick, new Vector3(j-5,i),Quaternion.identity); } } } // Update is called once per frame void Update() { } }
- 新建一个脚本文件,命名为shoot,用于生成发射的小球,将脚本附给Main Camrea,发射位置选择摄像机的位置,复制对象选择资源包中的小球的预制件,脚本内容如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shoot : MonoBehaviour { public GameObject shootPosition; private float force = 1000; public Rigidbody shooter; private float cameraSpeed = 0.1f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { Rigidbody ball; if (Input.GetKeyDown(KeyCode.Space)) { ball = Instantiate(shooter, shootPosition.transform.position, Quaternion.identity) as Rigidbody; ball.AddForce(force * ball.transform.forward); } if (Input.GetKey(KeyCode.LeftArrow)) { this.transform.Translate(Vector3.left * cameraSpeed); } if (Input.GetKey(KeyCode.RightArrow)) { this.transform.Translate(Vector3.right * cameraSpeed); } if (Input.GetKey(KeyCode.UpArrow)) { this.transform.Translate(Vector3.up * cameraSpeed); } if (Input.GetKey(KeyCode.DownArrow)) { this.transform.Translate(Vector3.down * cameraSpeed); } } }
5. 制作完成
来源:oschina
链接:https://my.oschina.net/u/4462201/blog/3207883