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可以控制objectscene view里的显示/隐藏

3.1.1     使Pick Up Object在游戏中旋转

Inspector菜单下 Add Component-> New Script,命名为Rotator
Update()函数下增加
transform.Rotate (new Vector3 (153045) * Time.deltaTime);
乘以Time.deltaTim使得rotate动作更加smoothframe 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,设置颜色后拖动materialScene View中的一个instance,这时只有该instance得到着色,单击inspector菜单的apply,所有相关instance都会得到着色
2.      新建material,设置颜色后直接拖动materialProject View下的prefab,所有相关instance都会得到着色
使用prefab步骤:
1.      在根目录下创建Prefabs文件夹
2.      Pick Up ObjectHierarchy拖入Project View中的Prefabs文件夹,当我们从Hierarchy View拖动objectProject View 中时,我们就创建了一个new prefab asset containing a template or blueprint of our game object or game object family
3.      为了使hierarchy更有序,我们在Hierarchy View创建空的Game Obejcthold之前的Pick Up Object

3.2    收集PICK UP OBJECTS

如何学习一个component:点击inspector菜单下的相应component « ? »标志,进入reference,可以查看两周referencemanual(in the context of editor)scripting
GameObject的属性之tagtag是一个string value可以让我们区分不同的game object
GameObject的方法之compareTag,可以让我们比较不同的tag
如何给game object增加tag
1.      project view中选中prefab- >点击inspectorTaglist->Add Tag
2.      project view中选中prefab- >点击inspectorTaglist ->选择新建Tag
按住ctrl,可以在Scene View中选择多个game object,这时inspector菜单会显示多个game object的共同属性
碰撞collisions,在Unity的物理引擎下是如何工作的?
物理引擎不运行两个collidervolumes重叠(overlap),如果物理引擎在某一帧检测到了一个或多个colliders的重叠,物理引擎将会查看该object,分析其速度、rotation和形状以此计算碰撞。该计算中的一个重要因素是,该colliderstatic的还是dynamic的。Static collider一般是scene中不能移动的物体,比如墙壁,花草;Dynamic collider一般是移动的物体,比如player’s sphere或是汽车。计算碰撞时,Static collider的形状(geometry)不会受到影响,但是Dynamic collider会受到影响。
如果我们不希望colliders有重叠时发生碰撞,那么我们可以把其中的一个该为trigger collider。当我们把一个collider变成trigger collider时,我们可以通过OnTrigger event message来检测两个colliders的重叠。当一个collidertrigger时,我们可以做很多有意思的事情。比如在冒险游戏中,我们在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 collidersmove的假设下是合理的,因为这样可以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:同时有colliderrigid body
Static collider转变为dynamic collider只需要增加rigid body就行了,但是要注意rigid body会带来物理属性,为了消除rigid body的物理属性,要勾选inspector菜单下Rigidbody一栏中,Use Gravity Is Kinemactic两个勾选框,这样它就变成了kinematic rigid bodykinematic rigid body不会相应物理forces,但是可以相应我们在脚本中设置的动画,比如move, rotate, scalekinematic rigid body可以用于游戏中的升降梯,移动平台以及可以收集的小豆豆

3.3    显示得分以及文字

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

No comments:

Post a Comment