Unity小项目__打砖块

//1.添加地面

1)创建一个平面,命名为Ground。

2)创建一个Materials文件夹,并在其中创建一个Ground材质,左键拖动其赋给平面Plane。

 

 3)根据喜好设置Ground材质和Ground平面的属性。

 

// 2.创建墙体

1)创建一个Cube,命名为Brick,并拖曳至设置的Prefab文件夹作为预制体。

 

 2)在Materials文件夹中创建一个Brick材质,操作同1.2),1.3)

3)网格与捕捉设置,  设置好后可利用Ctrl加鼠标拖动步移物体。或点亮带磁铁的标记,直接移动物体。

// 设置预制体后的Brick 

4)全选:一直按住Shift点击Brick到Brick(10) 或直接在图形界面选中物体,Ctrl+D(复制粘贴),向上拖动。

 5)创建一个空物体对象作为上一层文件夹(父类)。

 

 // 3.控制游戏物体左右移动

1)创建一个Movement脚本控制相机的移动

 

 

 2)创建一个Script文件夹,放置脚本文件

 // Movement脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public int speed = 1; // 定义一个属性,用于控制速度
    // Start is called before the first frame update
    void Start()
    {
       // transform.Translate(Vector3.up); // 开始游戏时控制相机向上移动1m
    }

    // Update is called once per frame
    void Update() // 每秒执行的频率不固定
    {
        float h = Input.GetAxis("Horizontal"); // h:通过按下A、D键来控制移动
        float v = Input.GetAxis("Vertical"); // V:通过按下W、S键来控制移动
        transform.Translate(new Vector3(h, v, 0) * speed * Time.deltaTime); // h:控制水平方向x轴的移动;v:控制上下方向y轴的移动
        // 默认h,v为一帧一米,Time.deltaTime为一帧的秒数,speed可看作为速度的倍数——>每秒的速度*倍数

        // 检测帧率
        // Debug.Log(Time.deltaTime); // 获取上一帧的时间,一帧的秒速
        // Debug.Log(1 / Time.deltaTime); // FPS frame per second,每秒执行的帧数
    }

    //private void FixedUpdate() // 每秒执行的频率固定
    //{
    // Debug.Log(Time.deltaTime); // 获取上一帧的时间,一帧的秒速
    // Debug.Log(1 / Time.deltaTime); // FPS frame per second,每秒执行的帧数
    //}
}

// 4.创建小球

1)建立一个Sphere命名为Bullet,并鼠标拖曳至Prefab文件夹作为预制体,删除Bullet实体。

 

 2)建立子弹脚本

 // 定义一个游戏物体:子弹预制体

public GameObject bulletPrefab; //  定义一个游戏物体:子弹预制体

// 通过鼠标拖曳预制体Bullet指定对象

5.判断鼠标按下并创建子弹

// 脚本

    void Update()
    {
        // if(true)——>运行{代码},否则跳过
        if (Input.GetMouseButtonDown(0)) // 在用户按下给定鼠标按钮的那一帧内返回true。0 ——>鼠标左键
        {
            GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation);
        }
    }

// 给预设体Bullet设置刚体组件(和创建脚本一样)

 // 运行

 6.发射子弹

// 方案一:使用AddForce方法,施加力

    void Update()
    {
        // if(true)——>运行{代码},否则跳过
        if (Input.GetMouseButtonDown(0)) // 在用户按下给定鼠标按钮的那一帧内返回true。0 ——>鼠标左键
        {
            GameObject bullet = GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation); // 设置一个bullet接受克隆的一个游戏物体
            Rigidbody rd = bullet.GetComponent<Rigidbody>(); // 得到bullet身上的<刚体>组件

            // 施加力的方案
            // 方案一,不方便观察速度
            rd.AddForce(Vector3.forward * 80); // 默认施加一个向前的1N的力 * 80
        }
    }

// 方案二:直接给一个速度

rd.velocity = Vector3.forward * 35; // 直接给一个速度

 

 7.墙壁物理模拟

// 给预设体Brick添加刚体

 // 全代码参考

// Movement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public int speed = 1; // 定义一个属性,用于控制速度
    // Start is called before the first frame update
    void Start()
    {
       // transform.Translate(Vector3.up); // 开始游戏时控制相机向上移动1m
    }

    // Update is called once per frame
    void Update() // 每秒执行的频率不固定
    {
        float h = Input.GetAxis("Horizontal"); // h:通过按下A、D键来控制移动
        float v = Input.GetAxis("Vertical"); // V:通过按下W、S键来控制移动
        transform.Translate(new Vector3(h, v, 0) * speed * Time.deltaTime); // h:控制水平方向x轴的移动;v:控制上下方向y轴的移动
        // 默认h,v为一帧一米,Time.deltaTime为一帧的秒数,speed可看作为速度的倍数——>每秒的速度*倍数

        // 检测帧率
        // Debug.Log(Time.deltaTime); // 获取上一帧的时间,一帧的秒速
        // Debug.Log(1 / Time.deltaTime); // FPS frame per second,每秒执行的帧数
    }

    //private void FixedUpdate() // 每秒执行的频率固定
    //{
    // Debug.Log(Time.deltaTime); // 获取上一帧的时间,一帧的秒速
    // Debug.Log(1 / Time.deltaTime); // FPS frame per second,每秒执行的帧数
    //}
}

 // Shoot

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shoot : MonoBehaviour
{
    public GameObject bulletPrefab; //  定义一个游戏物体:子弹预制体
    // Start is called before the first frame update
    void Start()
    {
        // 根据prefab创建实例(实例化Prefab),克隆物体
        // GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation);
    }

    // Update is called once per frame
    void Update()
    {
        // if(true)——>运行{代码},否则跳过
        if (Input.GetMouseButtonDown(0)) // 在用户按下给定鼠标按钮的那一帧内返回true。0 ——>鼠标左键
        {
            GameObject bullet = GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation); // 设置一个bullet接受克隆的一个游戏物体
            Rigidbody rd = bullet.GetComponent<Rigidbody>(); // 得到bullet身上的<刚体>组件

            // 施加力的方案
            // 方案一,不方便观察速度
            //rd.AddForce(Vector3.forward * 80); // 默认施加一个向前的1N的力 * 80
            // 方案二
            rd.velocity = Vector3.forward * 35; // 直接给一个速度
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_57233919/article/details/132354190