Unity项目 - 打砖块游戏

基本功能:实现WASD进行视角在XY轴方向的移动,其次按下鼠标左键产生子弹bullet对面前的砖块cube进行碰撞。

项目地址:BreakBricks

制作过程:

  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
    }
}

猜你喜欢

转载自www.cnblogs.com/SouthBegonia/p/10890048.html