Unity 3D 集成开发环境作为一个优秀的游戏开发平台,提供了出色的管理模式,即物理管理器(Physics Manager)。 物理管理器管理项目中物理效果的参数,如物体的重力、反弹力、速度和角
物理管理器管理项目中物理效果的参数,如物体的重力、反弹力、速度和角速度等。
在 Unity 3D 中执行 Edit→Project Settings→Physics 命令可以打开物理管理器,如下图所示。

可以根据需要通过调整物理管理器中的参数来改变游戏中的物理效果,具体参数如下表所示。
综合案例:迷宫夺宝
案例构思
迷宫夺宝类游戏是常见的游戏类型,玩家在限定的时间内,在迷宫中寻找宝箱从而达到通关的目的。本项目旨在通过场景虚拟漫游,寻找迷宫中潜藏的宝箱,当靠近宝箱时收集它,实现碰撞检测功能。
案例设计
本案例在 Unity 3D 内创建一个三维迷宫场景,场景内分散着若干个宝箱,游戏玩家需要在规定的时间内找到宝箱,靠近并收集它,最终通关。项目实施
1) 搭建迷宫场景
步骤 1):创建新项目,并将场景命名为 migong。步骤 2):创建游戏对象。执行菜单栏中的 GameObject→3D Object→Plane 命令,创建平面,并赋予材质。
执行 GameObject→3D Object→Cube 命令创建若干个盒子,构成迷宫场景,如下图所示。

步骤 3):导入模型资源。从 Unity 3D 商店中选择 3D 模型资源并加载到场景中,将其命名为 treasure,如下图所示。

步骤 4):将模型资源导入到 Hierarchy 视图中,如下图所示。

步骤 5):执行 Assets→Import Package→Custom Package 命令添加第一人称资源,如下图所示。

步骤 6):选中第一人称资源后单击 Import 按钮导入该资源,如下图所示。

步骤 7):在 Project 视图中搜索 first person controller,将其添加到 Hierarchy 视图中,并摆放到平面上合适的位置,如下图所示。

步骤 8):因为第一人称资源自带摄像机,因此需要关掉场景中的摄像机。
2) 添加触发器
步骤 9):选中 treasure,为 treasure 对象添加 Box Collider,并勾选 Is Trigger 属性,如下图所示。
步骤 10):编写脚本 Triggers.cs,代码如下。
using UnityEngine; using System.Collections; public class Triggers:MonoBehaviour{ void OnTriggerEnter(Collider other){ if(other.tag=="Pickup"){ Destroy(other.gameObject); } } }步骤 11):将 Triggers 脚本链接到 first person controller 上。
步骤 12):为 treasure 添加标签 Pickup。
3) 添加计数功能
步骤 13):修改脚本。using UnityEngine; using System.Collections; public class Triggers:MonoBehaviour{ public static int temp_Num=0; void OnTriggerEnter(Collider other){ if(other.tag=="Pickup"){ temp_Num++; Destroy(other.gameObject); } } void OnGUI(){ if(temp_Num==5) if(GUI.Button(new Rect(Screen.width/2f, Screen.height/2f, 100, 50),"play again")){ temp_Num=0; Application.LoadLevel("migong"); } } }步骤 14):将场景添加到 Build Settings 中,如下图所示。

4) 添加计时功能
步骤 15):完善代码,如下所示。using UnityEngine; using System.Collections; public class Triggers:MonoBehaviour{ public static int temp_Num=0; public int parachuteNum; int timer; int time_T; bool isWin=false; bool isLose=false; void Start(){ Time.timeScale=1; GameObject[]objs=GameObject.FindGameObjectsWithTag("Pickup"); parachuteNum=objs.Length; time_T=(int)Time.time; } void Update(){ timer=20-(int)Time.time+time_T; if(temp_Num==parachuteNum&&timer!=0){ isWin=true; } if(timer==0&&temp_Num!=parachuteNum){ isLose=true; } } void OnTriggerEnter(Collider other){ if(other.tag=="Pickup"){ temp_Num++; Destroy(other.gameObject); } } void OnGUI(){ GUI.Label(new Rect(0, 0, 100, 50), timer.ToString()); if(isWin==true||isLose==true){ Time.timeScale=0; if(GUI.Button(new Rect(Screen.width/2f, Screen.height/2f, 100, 50), "play again")){ isWin=false; isLose=false; temp_Num=0; Application.LoadLevel("migong"); } } } }步骤 16):单击 Play 按钮进行测试,效果如下图所示。

