Unity項目 - 打磚塊遊戲

基本功能:實現WASD進行視角在XY軸方向的移動,其次按下鼠標左鍵產生子彈bullet對面前的磚塊cube進行碰撞。git

主界面:
主界面github

運行狀況:
運行狀況
動態過程:
ide

項目地址:BreakBricks3d

製做過程:code

  1. 建立平面plane作場景的地面
  2. 建立磚塊的預製體Cube,包含信息有
    • 碰撞體 Box Collider
    • 材質 Cube Material
    • 剛體 Rigidbody
  3. 複製磚塊堆積建立牆壁 TotalCubes
  4. 對鏡頭Main Camera編寫腳本 Short.csMovement.cs
    • Movement:鍵盤讀取WASD值對視角進行XY軸的移動
    • Short:單鼠標左鍵按下即實體化子彈預製體 bullet,而且賦予初速度
  5. 至此實現基本功能,如下爲其餘可添加功能
    • 彈跳性:建立Physic Material材質,其中Bounciness屬性即爲彈性(0表明無彈力,1表示徹底反彈),將其賦予ColliderMaterial便可實現
//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
    }
}
相關文章
相關標籤/搜索