目录
基础资料
Unity官方教程:
https://unity3d.com/cn/learn/tutorials/projects/roll-ball-tutorial/setting-game?playlist=17141
Unity API
https://docs.unity3d.com/ScriptReference/
API翻译
http://www.ceeger.com/Manual/index.html
左上角scale tool 伸缩,中上部Gizmos取消show grid
为物体添加刚体(Rigidbody)组件(Component)
在物体上Add Component可以一步完成脚本的创建与绑定,但创建的文件会位于Asset根目录
获取、使用input的方法:Update、FixedUpdate
Update:在渲染每帧前被调用(多数)
FixedUpdate:执行有关物理计算前被调用(实现物理效果)
Unity API查看(ctrl+’):有问题,按了之后在浏览器一堆404窗口
AddForce:给刚体添加作用力,使之移动(参数-三维向量+作用力模式(ForceMode可省略:加速度、力、脉冲和速度变化))
在脚本中访问刚体组件:在Start方法中, GetComponent<Rigidbody>();返回组件引用
Input.GetAxis ("Horizontal")使用输入轴Returns the value of the virtual axis identified by axisName.
Start方法中的代码会在脚本(通常也是游戏)运行时的第一帧被调用
用浮点数值组成三维向量:new Vector3(x,y,z)
创建公共变量可以不打开脚本,在检视窗口直接修改
检查页脚、控制台报错
CTRL + ALT +C 注释/不注释该行
小球脚本:监听键盘使小球移动
//PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
第三人称设置:将摄像机绑定在player(若成为游戏对象的子对象后,摄像机根据父对象Transform组件数值进行更改或偏移,在三根轴上旋转,)通过脚本将二者相连,而不是作为子对象,二者间的偏移值是游戏角色与摄像机间的位移(不旋转)
LateUpdate():在update执行完后调用,游戏对象已经完成移动
创建游戏对象的引用(拖拽到参数框)
摄像机脚本:关联小球与摄像头(镜头不翻滚)
//CameraController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;//得出运行开始的固定偏移
}
// Update is called once per frame
void LateUpdate () {
transform.position = player.transform.position
+ offset;
}
}
建立父游戏对象Walls放置所有的墙(Create Empty)
游戏项目可以嵌套别的游戏项目,用空白游戏对象作为层级窗口的目录或者文件夹
新建Object:Cube作为墙体,使用前先重置物体位置。改变长度与游戏区域一致
复制物体Edit-Duplicate
编辑器设为Local模式时运行,小球坐标会翻转
游戏对象激活状态复选框(属性框Inspector中,物体名前)
自动旋转脚本:Rotation值
Transform:Translate移动,Rotate旋转(传入旋转方向V3或xyz三个浮点数)
将物体作为预制件prefab:资源格式,包含游戏对象的模板,可用于当前项目的任何场景,预制件更新,所有相应对象随之更新。从层级窗口放到项目文件夹后就成为预制件
物体属性框最上框的Apply,能将属性更改应用到预制件
物体旋转脚本
//Rotator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotator : MonoBehaviour {
// Update is called once per frame
public float speed;
void Update () {
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime*speed);
}
}
存储计分
在Start中设置私有变量初始值
触碰物体时在OnTriggerEnter方法中,.other.gameObject.CompareTag ( "Pick Up")
UI工具集显示文本Text,父对象Canvas,EventSystems游戏对象。所有UI工具必须是Canvas的子对象才能正常工作
UI组件的Transform使用Rect transform
Anchor定位组件轴心(Shift+Alt选择),posit x y改变文字定位
UI工具集存放在命名空间(namespace)UnityEngine.UI。为countText设置初始值
将Text对象引用拖拽到PlayerController脚本属性框
删除物体gameObject.SetActive (false);
winText没有初始化为空字符串时,可以不显示
Collider :Sphere Collider球体碰撞体组件(属性栏右侧链接到组件的参考文档)点击Switch to Script,查看怎样在脚本使用
Collider.OnTriggerEnter检测触碰,实际不会造成物理碰撞。触碰到的触发器引用作为参数返回
GameObject:
Destroy(other.GameObject)销毁
Tag用于识别,在Tags and Layers面板创建。CompareTag:将标签值与字符串比较,比用gameObject.tag比较高效
SetActive禁用或激活
选中多个物体后,属性框显示共有组件与属性。同时禁用所有Mesh Renderer组件。不允许两个碰撞体重叠,碰撞时检测碰撞体的速度、旋转、形状,是静态还是动态,计算碰撞。动态几何体受影响弹开。
碰撞体变为触发器,允许模型重叠渗透。勾选Box Collider的Is Trigger。
碰撞体OnCollider、触发器OnTrigger
触发器会从地板掉下不发生碰撞,禁用Use Gravity,或Is Kinematic:变为只受脚本控制的刚体,运动学刚体不受物理作用力的影响,但可以播放动画或通过Transform组件移动,适用于像电梯或移动的平台
静态碰撞体移动、旋转、缩放时,Unity会重新计算这些静态碰撞体体积,更新缓存数据
动态碰撞体不缓存数据,但预测碰撞体的移动
动态碰撞体:刚体。
静态碰撞体:非刚体
动态(刚体)带重力属性(默认Kinematic属性不打开),且触发器,不被地面托住
静态刚体:不移动(墙面、地面)
动态碰撞体:可运动,且有刚体组件
普通刚体:通过作用力移动
Kinematic刚体:通过Transform组件移动
Debug:
确定OnTriggerEnter是否发生。other.gameObject
OnTriggerEnter未发生:球体的Sphere Collider勾选,并且Is Trigger勾选
Tag对象分配的标签属性:在属性栏上部Tag
OnTriggerEnter函数触发的条件:
1、两个物体都必须有碰撞器(Collider)组件;
2、其中一个物体的碰撞器(Collider)的IsTrigger属性必须勾上;(这个属性还会影响物体是否从地面掉下)
3、最重要的一点,其中一个物体必须有刚体(Rigidbody)组件。如果是一个运动的物体去碰撞一个静止的物体,则刚体(Rigidbody)组件必须加在运动的物体上,否则无法触发OnOnTriggerEnter函数。
存储计分,并显示Text:
//PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
// countText.text = "Trigger ";
// countText.text = "Object: " + other.gameObject.ToString ();
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win!";
}
}
}
Build
Building、build target构建与构建目标
Build setting
更改构建目标后,Switch Platform
Add Current或拖拽场景到空白区,添加场景。不添加,会自动用当前编辑的窗口
保存Build文件的Build文件夹应存在项目根文件夹下,与Assets、Library同级。命名文件,save,生成.exe与data文件夹,可直接运行。