Labels

Friday, April 1, 2016

Unity入门教程之ROLL-A-BALL 学习笔记 Part III

Unity入门教程

之ROLL-A-BALL 学习笔记

教程链接:http://unity3d.com/cn/learn/tutorials/projects/roll-a-ball/introduction?playlist=17141

3     收集, 得分以及BUILDING THE GAME

3.1    创建可收集的OBJECTS

Create->3D Object->Cube,重命名为Pick Up
Inspector菜单下的Active checkbox可以控制object在scene view里的显示/隐藏

3.1.1     使Pick Up Object在游戏中旋转

Inspector菜单下 Add Component-> New Script,命名为Rotator
Update()函数下增加
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
乘以Time.deltaTim使得rotate动作更加smooth和frame rate independent
把Pick Up Object 放入prefab
Prefab: Prefab is an asset that contains a template or blueprint of a game object family, once created, we can use this prefab in any scene in our current project
使用Prefab的好处:对一个object更改,或直接对prefab asset本身更改,所有相关object都会得到更新,比如我们想要给一个Prefab关联的多个instance着色,有两个办法:
1.      新建material,设置颜色后拖动material到Scene View中的一个instance,这时只有该instance得到着色,单击inspector菜单的apply,所有相关instance都会得到着色
2.      新建material,设置颜色后直接拖动material到Project View下的prefab,所有相关instance都会得到着色
使用prefab步骤:
1.      在根目录下创建Prefabs文件夹
2.      把Pick Up Object从Hierarchy拖入Project View中的Prefabs文件夹,当我们从Hierarchy View拖动object到Project View 中时,我们就创建了一个new prefab asset containing a template or blueprint of our game object or game object family
3.      为了使hierarchy更有序,我们在Hierarchy View创建空的Game Obejct来hold之前的Pick Up Object

3.2    收集PICK UP OBJECTS

如何学习一个component:点击inspector菜单下的相应component的 « ? »标志,进入reference,可以查看两周reference,manual(in the context of editor)和scripting
GameObject的属性之tag,tag是一个string value,可以让我们区分不同的game object
GameObject的方法之compareTag,可以让我们比较不同的tag
如何给game object增加tag:
1.      在project view中选中prefab- >点击inspector的Taglist->Add Tag
2.      在project view中选中prefab- >点击inspector的Taglist ->选择新建Tag
按住ctrl,可以在Scene View中选择多个game object,这时inspector菜单会显示多个game object的共同属性
碰撞collisions,在Unity的物理引擎下是如何工作的?
物理引擎不运行两个collider的volumes重叠(overlap),如果物理引擎在某一帧检测到了一个或多个colliders的重叠,物理引擎将会查看该object,分析其速度、rotation和形状以此计算碰撞。该计算中的一个重要因素是,该collider是static的还是dynamic的。Static collider一般是scene中不能移动的物体,比如墙壁,花草;Dynamic collider一般是移动的物体,比如player’s sphere或是汽车。计算碰撞时,Static collider的形状(geometry)不会受到影响,但是Dynamic collider会受到影响。
如果我们不希望colliders有重叠时发生碰撞,那么我们可以把其中的一个该为trigger collider。当我们把一个collider变成trigger collider时,我们可以通过OnTrigger event message来检测两个colliders的重叠。当一个collider是trigger时,我们可以做很多有意思的事情。比如在冒险游戏中,我们在middle of the door way中放置这样一个trigger,当player到大门口时,我们可以更新小地图,并在屏幕上显示“你发现了这个房间“。
把collider变成trigger方法:选中该物体,在inspector菜单下Collider选项中将Is Trigger选中
关于Unity的性能优化问题
Unity计算all the volumes of all the static colliders in a scene and holds this information in a cache。这种情况在static colliders不move的假设下是合理的,因为这样可以save recaculation time,然而,当我们使static collider运动时(比如move, rotate, scale),Unity会重新计算所有static collider并且更新cache。为了避免Unity重新计算这样的Static collider,我们需要将其转变为dynamic collider。
Static collider:有collider attache但是没有physics rigid body
dynamic collider:同时有collider和rigid body
将Static collider转变为dynamic collider只需要增加rigid body就行了,但是要注意rigid body会带来物理属性,为了消除rigid body的物理属性,要勾选inspector菜单下Rigidbody一栏中,Use Gravity 和 Is Kinemactic两个勾选框,这样它就变成了kinematic rigid body。kinematic rigid body不会相应物理forces,但是可以相应我们在脚本中设置的动画,比如move, rotate, scale。kinematic rigid body可以用于游戏中的升降梯,移动平台以及可以收集的小豆豆

3.3    显示得分以及文字

在game中显示text需要用到Unity的UI Toolset
在Hierarchy View中Create->UI->Text
同时新建了三个东西,Canvas,Text和EventSystem。Text必须是Canavs的child以确保其behave correctly
注意Inspector View下的Text的Transform属性与game object不同,更改Text的默认显示位置可以在Rect Transform属性下修改Anchor Presets
在Script PlayerController中增加public变量countText 以及相应方法后怎么与Hierarchy View中的Text链接呢? 将其拖入到Player的Script相应属性中就行,这样Unity会找到与Hierarchy View中的Text的reference

Unity入门教程之ROLL-A-BALL 学习笔记 Part II

Unity入门教程

之ROLL-A-BALL 学习笔记

教程链接:http://unity3d.com/cn/learn/tutorials/projects/roll-a-ball/introduction?playlist=17141

2     设置摄像头和PLAY AREA

2.1    移动摄像头

·        方法一:
1.      调整Main Camera角度:Inspector设置postion
2.      拖动Main Camera使其成为Player的child
3.      这样Main Camera会随着Player的移动而移动,注意,如果Player rotate,Main Camera也会跟着rotate,这样不好
·        方法二:
1.      使用script让Player与 Camera联系起来,选中Main Camera Add Component->New Script, 重命名script为CameraController
2.      为得到Player的reference,可以新建public GameObject变量,获取Player的refercence
3.      在LateUpdate里插入代码
4.      进取Editor,把Player拖入Main Camera在Inspector菜单下的Script Slot

2.2    设置PLAY AREA

就是增加4堵墙
GameObject ->create empty
注意:Empty GameObject的意义在于充当object的folder,有利于结构化管理
建造四面墙壁:
1.      先新建Empty GameObject当作parent,重命名为Walls
2.      先建造一面墙壁:GameObject->3D Object->Cube,重命名为West Wall
3.      把West Wall归入Walls
4.      把刚刚的墙复制三遍:Edit->Duplicate (ctrl + d)
Global Mode:这个模式下,拖动object,object与ground平行
Local Mode : Highlight 某个object后,使Editor进入Local Mode,按下Play按钮,这样我们就可以看到 transform rotate


Unity入门教程之ROLL-A-BALL 学习笔记 Part I

Unity入门教程

之ROLL-A-BALL 学习笔记

1     环境和PLAYER设置

1.1    环境Environnement 设置

创建新场景:File->New Scene(Ctrl + S)
创建Gameboard/Play Field: GameObject->3D Object -> Plane
重命名新建Plane名称 :点击Plane,按下Enter重命名
Reset Object:单击右上方的 Context sensitive gear menu -> Reset
Reset Object 使object的Postion调整在x=0, y=0, z=0
To see the entire game object in the Scene View: Edit -> Frame Selected (F)
显示/关闭Grid lines:在Scene View的Gizmo菜单下选择或取消选择Show Grid
Rescale object有三种方法:

1.      快捷键R,拉动三个坐标轴
2.      在inspector菜单下的Transform栏里拖动Scale
3.      在inspector菜单下的Transform栏里填写Scale数值
注意:Rescale plane时要注意plane没有volume所以scale plane的y坐标轴没有意义,通常y=1
创建objet三部曲:
1.      Rename
2.      Reset position
3.      Frame Selected
更改objet颜色三部曲:
1.      创建Materials文件夹,选中Materials文件下,Project栏Create->Material
2.      inspector菜单下Aibedo栏更改颜色
3.      拖动Material 到Scene View中需要着色的object
转动the main directional light : 选中Directional Ligth在Transform Component中更爱Rotation x=50 y=60 z=0,以达到更好的光照效果

1.2    PLAYER设置

给object增加physics属性:先选择object,Component ->Physics ->Rigidbody ;或者是在inspector菜单下点击Add Component->Physics ->Rigidbody 
如何让player得到外界input:
1.      新建Scripts放置C# scripts
2.      inspector菜单下点击Add Component->New Scripts (both create and attach script in one step), 重命名script为PlayerController
3.      把新建的script拖入scripts文件夹中
Start:is called on the first frame that the script is active,放置初始化code
Update:is called before rendering a frame,放置一般code
FixUpdate:is called just before performing any physics calculations,放置physics code
LateUpdate:it is guaranteed to run after all items have been processed in update,for follow cameras, procedural animation and gathering last known states
注意:Script里的public变量可以在Inspector菜单里直接赋值,方便调试
数据结构:
·        Rigidbody,为object增加物理属性,使用AddForce(Vector3)方法使object移动
·        Vector3, 三个方向的数据结构