基本功能:实现WASD进行视角在XY轴方向的移动,其次按下鼠标左键产生子弹bullet
对面前的砖块cube
进行碰撞。git
主界面: github
运行状况: 动态过程:
ide
项目地址:BreakBricks3d
制做过程:code
plane
作场景的地面Cube
,包含信息有
Box Collider
Cube Material
Rigidbody
TotalCubes
Main Camera
编写脚本 Short.cs
及 Movement.cs
Movement
:键盘读取WASD值对视角进行XY轴的移动Short
:单鼠标左键按下即实体化子弹预制体 bullet
,而且赋予初速度Physic Material
材质,其中Bounciness
属性即为弹性(0表明无弹力,1表示彻底反弹),将其赋予Collider
的 Material
便可实现//Short.cs using UnityEngine; public class Short : MonoBehaviour { public GameObject bullet; public float speed = 5; void Start() {} void Update() { //左键按下产生子弹 if(Input.GetMouseButtonDown(0)) { GameObject b = GameObject.Instantiate(bullet, transform.position, transform.rotation); Rigidbody rgd = b.GetComponent<Rigidbody>(); rgd.velocity = transform.forward * speed; } } }
//Movement.cs using UnityEngine; public class Movement : MonoBehaviour { public float speed = 5; void Start() {} void Update() { float h = Input.GetAxis("Horizontal"); //x轴 float v = Input.GetAxis("Vertical"); //y轴 //Debug.Log(h); transform.Translate(new Vector3(h, v, 0) * Time.deltaTime * speed); //左右镜头移动速度1 m/s * speed } }