基本功能:實現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 } }