unity 官方教程Survival Shooter 学习笔记
1.准备工作
新建项目并导入官方商店的Survival Shooter资源
在Assets下新建文件夹_Scence并在新文件夹内保存当前新场景命名为Main
2.背景和背景音乐设置
在Prefabs文件夹下拖入预制件Environment和Lights,并把系统自带的Direction Light删除。记得拖入的两个新预制件都要reset。
我们可以看到游戏环境里的地板是不平的,为了之后游戏的方便,我们创建一个平面(Hierarchy→Create→3D Object→Qaud)并命名为Floor,reset并设置Rotation为(90,0,0)Scale为(100,100,1)使平行于地面并覆盖。我们只需要平面的碰撞器特性,不需要看见它,所以删掉Floor的Mesh Renderer组件。将Floor的Layer选择为Floor属性(非常重要,不然之后人物不转动)。
接下来新建空对象(Shift+Ctrl+N)命名为BackgroundMusic并reset,添加Audio Source组件,在Audio Clip里选择Background Music,勾选Loop循环,并取消Play On Awake,将音量Volume设为0.1。保存场景(Ctrl+S)。
3.添加游戏角色
将已在游戏包内做好的主角模型Player拖入到Hierarchy下并reset,将角色的Tag设置为Player(Layer决定摄像机的射线投射&渲染对象和灯光的 特定照射,Tag是 为了可以在脚本内使用(GameObject.FindWithTag())语句来快速搜索相关对象)
接下来做一个动画管理器,选定Assets新建文件夹Animation,在Animation下create新建一个Animator Controller命名为PlayerAC。将新建的PlayerAC拖入Hierarchy下的Player,于是Player多了一个动画管理器组件。
双击新建的PlayAC,我们可以在左边看到Animator界面。单机Models下的Player,将三个动画文件一个一个拖入到Animator,拖入后右键点击Idle选择Set Layer As Default State使动画空闲状态为默认动画(点击后所选择的动画变为橙色)。
接下来非常重要,在Animator窗口里点击Parameters选项卡,点击+号新建状态。
先点击+选择Bool,命名为IsWalking,表示判断角色是否在动。再次点击+选择Trigger,命名为Die,判断角色是否死亡。注意一定要拼写正确,大小写都对上,不然之后脚本里没法调用。
然后我们要让管理器知道什么状态下播放什么动画。先右键点击橙色的Idle,选择Make Transition,可以看到鼠标拖出了一条线,将鼠标点击灰色的Move,可以看到Idel有个箭头指向了Move,单击这个箭头,可以看到箭头变成蓝色了,在右面的Inspector选项卡里点击Conditions下面的加号,会出现IsWalking,选择true。表示当运动时由空闲动画转为运动动画。
同样的,点击Move后右键拉箭头指向Idle,之后单击箭头,在conditions下面选择IsWalking,选择false。表示角色不动了则由运动转为空闲动画。
角色死亡状态是在任何状态下死亡都调用死亡动画,所以单击Any State拉箭头指向Death,单击箭头,conditions选择Die。
为了主角可运动和被触发,我们添加Rigidbody和Capsule Collider组件。Rigidbody下我们不希望运动有阻力,所以在Drag和Angular Drag上都填inf,表示infinity。之后在Constraints下,将Freeze Position的Y和Freeze Rotation的X&Z勾选,表示我们不希望角色在这些方向上运动。
Capsule Conllider上我们将中心Center设为(0.2,0.6,0),Height设为1.2以符合高度。
最后我们为角色添加Audio Source组件,Audio Clip选择为Player Hurt,Player On Awake和Loop我们都取消勾选。
现在我们让人物动起来,打开Scripts文件夹下的PlayerMovement脚本并编辑
保存后回到unity,测试。发现角色可以移动了,但不能转向。打开Aimator,单击两个状