本文只记录重要知识点以及涉及到的脚本 API,详细教学过程:视频链接 。

本文中大量关于 API 的描述来自 Unity 官方文档 。

另:学完之后发现这个教程非常非常像这个 Unity 官方教程 。

0. 搭建场景

这个简单的场景需要一个平面、四堵墙、一个球体作为玩家操控的对象,以及七个待收集的小方块。

搭完的场景如下所示:

1. 用方向键操纵球

首先需要为球添加刚体组件(Rigidbody),并为其挂载一个脚本,关键代码如下:

    Rigidbody rigid;float speed = 0.5f;void Start(){rigid = GetComponent<Rigidbody>();       // 获取刚体组件}void Update(){var h = Input.GetAxis("Horizontal");    // 取值范围 -1 到 1,代表从左到右var v = Input.GetAxis("Vertical");      // 取值范围 -1 到 1,代表从下到上var movement = new Vector3(h, 0, v);rigid.AddForce(movement * speed);}

新学到的 API:

Input.GetAxis

public static float GetAxis(string axisName);

Returns the value of the virtual axis identified by axisName.

The value will be in the range -1…1 for keyboard and joystick input.

那么有哪些轴可以调用呢?在 Unity 中点击 Edit -> Project Settings -> Input Manager 就能查看所有的轴以及键位,这段代码中就使用了HorizontalVertical,对应的键位是 wasd 或者方向键。


Rigidbody.AddForce

public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force);

Adds a force to the Rigidbody.

Force is applied continuously along the direction of the force vector. Specifying the ForceMode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change.

以下是可以使用的ForceMode,默认使用 Force:

成员 描述
Force Add a continuous force to the rigidbody, using its mass.
Acceleration Add a continuous acceleration to the rigidbody, ignoring its mass.
Impulse Add an instant force impulse to the rigidbody, using its mass.
VelocityChange Add an instant velocity change to the rigidbody, ignoring its mass.

例如要使用 VelocityChange:
rigid.AddForce(movement * speed, ForceMode.VelocityChange);


2. 让小方块随时间匀速自转

为所有小方块挂载同样的脚本,关键代码如下:

    void Update(){transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);}

transform.Rotate

public void Rotate(Vector3 eulers, Space relativeTo = Space.Self);

参数 描述
eulers The rotation to apply.
relativeTo The rotation axes, which can be set to local axis (Space.Self) or global axis (Space.World)

public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);

参数 描述
relativeTo Determines whether to rotate the GameObject either locally to the GameObject or relative to the Scene in world space.
xAngle Degrees to rotate the GameObject around the X axis.
yAngle Degrees to rotate the GameObject around the Y axis.
zAngle Degrees to rotate the GameObject around the Z axis.

The implementation of this method applies a rotation of zAngle degrees around the z axis, xAngle degrees around the x axis, and yAngle degrees around the y axis (in that order).

Rotate can have the euler angle specified in 3 floats for x, y, and z.


Time.deltaTime

The completion time in seconds since the last frame (Read Only).

This property provides the time between the current and previous frame.

借助这个只读变量,就可以让小方块在每一秒的转动角度是相等的,而不是每一帧的转动角度相等。(通常每秒帧数随不同机器和时间而变化)


3. 当球碰撞到小方块时,让小方块消失

需要在球的脚本中加入下面这个函数:

    private void OnTriggerEnter(Collider other){ // 这里用所有小方块都有的脚本组件来判断撞到的物体是否是小方块// 个人觉得用标签来识别是更好的做法// 总之用所有小方块共有的一个属性来识别即可if (other.GetComponent<Pickup>()){other.gameObject.SetActive(false);}}

Collider.OnTriggerEnter

Parameters
other: The other Collider involved in this collision.

When a GameObject collides with another GameObject, Unity calls OnTriggerEnter.

OnTriggerEnter happens on the FixedUpdate function when two GameObjects collide. The Colliders involved are not always at the point of initial contact.

Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.

根据上述官方文档的描述,我们的球和小方块都需要有 Collider 组件,且只能有其中一方激活 Collider.isTrigger。在这个例子中我们将所有小方块的 isTrigger 激活。


gameObject.SetActive

public void SetActive(bool value);

Activates/Deactivates the GameObject, depending on the given true or false value.

A GameObject may be inactive because a parent is not active. In that case, calling SetActive will not activate it, but only set the local state of the GameObject, which you can check using GameObject.activeSelf. Unity can then use this state when all parents become active.

Deactivating a GameObject disables each component, including attached renderers, colliders, rigidbodies, and scripts.


4. 添加记分板和胜利条件

在小球脚本中的关键代码:

    public UnityEngine.UI.Text tips;int count = 0;private void OnTriggerEnter(Collider other){if (other.GetComponent<Pickup>()){other.gameObject.SetActive(false);++count;RefreshTips();    // 仅在碰撞时调用}}void RefreshTips()  // 自定义的函数,刷新记分板{tips.text = "Count: " + count.ToString();if (count >= 7){tips.text = "Win!";}}

需要在界面上新建一个 Canvas,在其中新建一个 Text,并将这个 Text 挂载到小球脚本的 tips 变量上面。


5. 脚本代码总览

5.1 小球脚本Player.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Player : MonoBehaviour
{public UnityEngine.UI.Text tips;Rigidbody rigid;float speed = 0.5f;int count = 0;// Start is called before the first frame updatevoid Start(){rigid = GetComponent<Rigidbody>();}// Update is called once per framevoid Update(){var h = Input.GetAxis("Horizontal");    // 取值范围 -1 到 1,代表从左到右var v = Input.GetAxis("Vertical");      // 取值范围 -1 到 1,代表从下到上var movement = new Vector3(h, 0, v);rigid.AddForce(movement * speed, ForceMode.VelocityChange);}private void OnTriggerEnter(Collider other){if (other.GetComponent<Pickup>()){other.gameObject.SetActive(false);++count;RefreshTips();}}void RefreshTips(){tips.text = "Count: " + count.ToString();if (count >= 7){tips.text = "Win!";}}
}

5.2 方块脚本Pickup.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Pickup : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);}
}

笔记-【游戏制作教程】30分钟制作一款游戏 (1)【Unity】相关推荐

  1. ae破碎效果在哪_AE教程 | 五分钟制作文字破碎效果

    原标题:AE教程 | 五分钟制作文字破碎效果 UBOOK知识在线 一个很认真的知识分享平台 愚人节快乐呀,亲爱的同学们- 今天又到了CC老师的AE小课堂啦,今天给大家带来的是一个很棒的特效效果--破碎 ...

  2. PPT制作教程:如何制作ppt

    PowerPoint(PPT)是专门用于制作演示文稿(俗称幻灯片).广泛运用于各种会议.产品演示.学校教学等.学会如何制作ppt,成为提升工作效 率的好帮手.PPT包含有很多的功能,我们可以根据个人喜 ...

  3. cad插件制作教程_CAD电子签名制作教程

    文尾左下角阅读原文看视频教程 好课推荐: 1.CAD2014:点击查看 2.室内CAD:点击查看 3.CAD2019:点击查看4.CAD2018:点击查看5.Bim教程:点击查看6.室内手绘:点击查看 ...

  4. pageadmin CMS网站制作教程:visual studio制作网站模板的的步骤

    pageadmin CMS网站建设教程:visual studio制作网站模板的的步骤 工欲善其事,必先利其器,一款好的开发工具可以让我们效率提高很多,前端开发工具很多,visual studio.D ...

  5. 怎么把照片做成计算机主题,Win7主题制作教程 电脑主题制作图文方法

    修改Windows7主题文件的具体步骤: 一.修改.theme主题文件 1.到365主题下载"天涯明月刀主题后"进行安装,然后我们在C:\Windows\Resources\The ...

  6. 【CMS建站】写给大家看的网站制作教程02—网站制作的工具介绍与下载安装

    作者 | 杨小爱 来源 | web前端开发(ID:web_qdkf) hello,大家好,我是杨小爱,欢迎来到web前端开发公号平台. 在上一篇<[CMS建站]写给大家看的网站制作教程01-了解 ...

  7. 游戏必备组件_一款Beta版游戏周销量 30 万份,独立游戏究竟有多火?

    如果你是抖音的重度用户,肯定会不断刷到游戏视频. <Crowd City>是一款有趣的休闲手游,简洁的画面风格,虐心的关卡设计,影响规则很简单,带上队伍在最短的时间聚集更多人群. 在抖音& ...

  8. 粉末游戏计算机教程,如何玩转《粉末游戏》粉末游戏攻略介绍

    粉末游戏作为一款全新理念的游戏,在这里玩家能够体会到创造的快乐.游戏中的一切都是粉末产生的,玩家可以随意的做关卡挑战,也可以进行粉末的搭配,这种轻松自如的操作模式,给每一位玩家都带来了创造性的体验. ...

  9. 用记事本编写小游戏_记事本3分钟编写放置小游戏(v0.4 土豪情趣屋与大型灵石盾构)...

    ------------------------- v0.4新增问山村的土豪情趣屋(大量生产人口),新增灵石盾构(提升灵石产量)! ------------------------- 说明: 本教程无 ...

  10. 抖音直播带货推流机制是什么?怎么跑好直播带货30分钟循环过款模型?

    很多商家讨论,目前推流机制不是根据整场累积数据来分配流量,而是以5分钟为最小单位,通过30分钟中循环,和3小时大循环,形成齿轮驱动流量. 每一个过去五分钟的数据,都会影响下一个五分钟会有多少自然流量进 ...

最新文章

  1. C - 食物链 POJ - 1182
  2. 启动vue项目报错:ENOSPC: System limit for number of file watchers reached, watch
  3. 推荐 21 个顶级的 Vue UI 库
  4. 打破场景边界,PDFlux助你多领域表格提取
  5. 中国人寿构建国内首个Silverlight企业级应用
  6. sqlserver 'sa'密码忘记,windows集成身份验证都登录不了解决办法
  7. php中文制作,php中文验证码制作教程
  8. java实现二维码的生成与解析
  9. coap协议开发实例C语言,CoAP协议及开源实现
  10. Kubernetes证书类型和适用场景
  11. Git的author与committer的区别
  12. LeetCode 213. House Robber II(小偷游戏)
  13. DLNA介绍(包括UPnP,2011/6/20 更新)
  14. 【原创】2009年太白山穿越
  15. 高级搜索-百度和必应
  16. python灰产_python入门之编码风格规范分享
  17. 数据库的这些你都知道吗?
  18. 怎么在linux卸载mysql,在linux中安装和卸载mysql
  19. GNSS单点定位解算与原理(基于MATLAB)
  20. 广而告之退市所带来的启示

热门文章

  1. 3D射线拾取算法揭秘
  2. 计算机和网络的发明与使用手抄报,2020网络安全的手抄报简单又好看
  3. Steam流的中间方法
  4. 达梦数据库用户权限管理
  5. C语言 打印星星(三种方法)
  6. 【Matlab优化选址】蚁群算法求解电动汽车充电站与换电站选址优化问题【含源码 1182期】
  7. Java-SpringBoot发送验证码短信
  8. Django-配置媒体资源-设置路由分发规则(下)
  9. 不用刷机的 MIUI?小米系统 APP 初体验
  10. 详细介绍C语言三大结构(顺序结构,分支结构,循环结构)