打开unity,点击open,打开素材文件

在assets面板中找到level,拖到场景中

调整一下摄像机的位置,选择平行光,关闭。

在左侧面板单击鼠标右键,选择create empty新建一个空物体,在下方添加九个point light点光源,移动场景的视角,排列如下图。

选中九个点光源,更改range参数,使之有交叉部分。更改各个点光源的颜色,营造一种黑暗恐怖的气氛。

更改一下点光源的强度,mode更改为baked

选中level,勾选右侧static,在弹出的消息框中选择YES

保存场景,window-lighting-setting,执行下方操作

烘焙一下灯光,查看效果,选中gameObject,component-rendering-light probe group添加光照探针,点击最右侧红框框,更改一下探针的大小和位置。

点击右侧的select all将探针放在左上角。

选择duplicate selected,复制一个,移动坐标轴,如图。

重复上述操作,当我们复制好第一排时,点击select all之后点击duplicate selected,向下移动坐标轴就可以复制第一排的探针了,重复操作,使探针布满整个场景。

window-lighting-settings-generate lighting重新烘焙一下场景。
测试有没有烘焙成功:
选择gameObject,关掉点光源,添加一个cube放到场景中,会有颜色的改变。

将cube删掉,选择gameObject,按F2键,重命名为light。

添加一个空物体(右键-create empty)选中点击F2键,更改名字为player,点击右侧add component按钮,搜索并添加character controller组件。

找到Scripts文件夹,右键-create-C# script,添加一个C#脚本,命名为player,拖到左侧面板的player上。

添加如下代码


选择场景中的两个长方体,component-physics-box collider,给小盒子添加碰撞器。

选中外边的大盒子,component-physics-mesh collider,添加网格碰撞器。

通过脚本将摄像机固定在玩家的身上。
更新脚本


运行测试:以第一人称通过控制“W”“A”“S”“D”在场景中移动,移动鼠标可以转换视野。
在资源面板找到M16的预设体,拖动到主摄像机的下方,作为摄像机的子对象,移动更改摄像机的clipping planes参数为0.01,选择M16移动其位置,得到如Game窗口所示状态,枪口放置在屏幕的中心。

在资源面板中找到Zombie,拖到场景中,我们运行后发现摄像机的高度存在问题。

因此,打开Player脚本,更改为m_CamHeight=0.4f;
要使敌人能自动攻击玩家,这就用到了自动寻路的技术。
window-navigation,选择bake再点击下方的bake烘焙场景,得到下图所示的蓝色区域。

点击zombie查看有没有Nav mesh agent组件,如果没有,component-navigation-nav mesh agent添加导航组件。

添加一个新脚本命名为Enemy,拖到zombie上,添加代码

运行测试:敌人会自动飘到玩家的位置。
找到animator

未完待续……
网友评论