网龙暑期训练营第二周:Unity介绍、调试以及小游戏demo开发

本文梳理第二周训练营视频中所提到的知识点,便于大家复习巩固,也便于自己日后查阅。视频内容主要分为三个部分:

1.Unity功能介绍

2.玩泥巴游戏开发

3.Unity调试操作 

1.Unity功能界面介绍

本部分主要介绍Unity的安装、界面布局以及菜单栏几个部分,便于大家快速熟悉Unity这个开发引擎。

Unity安装与界面介绍:https://blog.csdn.net/liyuerge/article/details/78518972

Unity菜单栏介绍:https://www.jianshu.com/p/d859b60c48ae (简书上图文并茂的比较好理解)

                             https://www.cnblogs.com/CZoro7/p/6938937.html (文字版适合随时查找)

2.丢泥巴游戏开发

丢泥巴游戏是在线视频中的实战demo案例,本节实现基本功能的开发,并在此基础上添加“丢泥巴”作业中的额外操作功能。

2.1 基础物体的创建

首先新建一个Cube,命名为Ground,将Scale调整为适合的尺寸,为其添加一个材质信息,可以在Projerct面板下创建一个Material,调整颜色,并赋给Ground。

之后添加一个Cube作为砖块,命名为brick,添加一个Rigidbody,加入材质(这里的材质是在Asset Store下载的,是一个处理砖块纹理的材质包,其余物体例如地面和“泥巴”的材质也可以在Asset Store中下载),并拖放到Asset下,作为一个Prefab

为了管理所有的砖块,建立一个Empty GameObject,将所有砖块拖放进来,在以后对砖块统一操作时会比较方便。最终效果如下。

创建好这堵“墙”后,我们创建一个Sphere作为“泥巴”,命名为mud,添加RigidBody以及对应的材质信息,同样做成一个Prefab

2.2 为物体添加脚本,实现功能

有了上述准备工作后,就可以利用脚本控制相机视角的移动和球的发射了。

为相机创建一个脚本,脚本代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMoveAndShoot : MonoBehaviour {
    public float moveSpeed = 5f;
    public Rigidbody bullet;
    public float power = 1500f;
    private bool isRotating = false;    //旋转标志位
    public float rotateSpeed = 2f;
    public float timer = 0;
    public float changeTime = 2f;   //自动发射时间间隔
        // Use this for initialization
        void Start () {
                
        }
        
        // Update is called once per frame
        void Update () {
        //控制移动
        float h = Input.GetAxis("Horizontal") * Time.deltaTime*moveSpeed;
        float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
        transform.Translate(h, v, 0f);
        //左键发射
        if (Input.GetButtonUp("Fire1"))
        {
            FireMud();
        }
        //自动发射
        timer += Time.deltaTime;    //定时器开始工作
        if(timer >= changeTime)    //当定时器大于2秒时
        {
            timer = 0;
            FireMud();
        }
        //Camera旋转控制
        if (Input.GetMouseButtonDown(1))
            isRotating = true;
        if (Input.GetMouseButtonUp(1))
            isRotating = false;
        if (isRotating)
         {
             Vector3 originalPosition = transform.position;
             Quaternion originalRotation = transform.rotation;
             transform.RotateAround(transform.position, transform.up, rotateSpeed * Input.GetAxis("Mouse X"));
             transform.RotateAround(transform.position, transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));
             float x = transform.eulerAngles.x;
             if (x < 10 || x > 80)
            {
                transform.position = originalPosition;
                 transform.rotation = originalRotation;
            }
             
        }
        }
    void FireMud()
    {
        Rigidbody instance = (Rigidbody)Instantiate(bullet, transform.position, transform.rotation);
        Vector3 fwd = transform.TransformDirection(Vector3.forward);
        instance.AddForce(fwd * power);
    }
}

并将Asset下的mud小球赋给代码中的RigidBody。

至此,可以通过键盘↑↓控制镜头移动,鼠标右键控制镜头转向,泥巴的发射默认为2秒一次,通过定时器实现,也可通过鼠标左键点击发射,根据需要选取发射方式。

3.Unity代码的调试

U3D的调试一般分为Debug.Log(简单的输出到控制台)、自带的MonoBehavior或是利用其它编译器(如VS),以下两篇博客对各类调试方法进行了较详尽的说明,可以参考:

Debug.Log和MonoBehavior调试介绍:https://www.cnblogs.com/HangZhe/p/7460413.html

Visual Studio下的调试:https://blog.csdn.net/gtncwy/article/details/44594371

猜你喜欢

转载自blog.csdn.net/s1314_JHC/article/details/81407743
今日推荐