《杜增强讲Unity之Tanks坦克大战》3-添加坦克
3 添加坦克
3.1 本节效果预览
![](https://upload-images.jianshu.io/upload_images/9047377-0a99a11addf84029.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
3.2 另存新场景
首先打开上次的场景s1,另存为s2,放到同一个文件夹下面.
![](https://upload-images.jianshu.io/upload_images/9047377-3df7cf08806bfc8d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/229/format/webp)
3.3 添加坦克模型
在Model文件夹下面找到Tank模型
![](https://upload-images.jianshu.io/upload_images/9047377-50730eefa8e768ae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/509/format/webp)
将Tank拖放到Hierarchy里面
![](https://upload-images.jianshu.io/upload_images/9047377-be7db59eda99d487.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/512/format/webp)
选中Tank,在Inspector面板里面设置Layer为Players
![](https://upload-images.jianshu.io/upload_images/9047377-3369567d36796a52.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/511/format/webp)
在弹出的面板里面选择No,this object only.
![](https://upload-images.jianshu.io/upload_images/9047377-cf717eb9dd0d694a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/395/format/webp)
在后面章节子弹碰撞检测的时候需要检测一定范围之内有没有坦克,就是通过判断这个Layer是否为Players来实现的.
3.4 为坦克添加刚体组件
![](https://upload-images.jianshu.io/upload_images/9047377-2e6800bfb35ed5a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/230/format/webp)
设置刚体组件Constraints属性,锁定Y position和xzrotation.
![](https://upload-images.jianshu.io/upload_images/9047377-ced1d5062f218dd6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/274/format/webp)
3.5 添加碰撞检测器
![](https://upload-images.jianshu.io/upload_images/9047377-2ac486c6450e2b6e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/230/format/webp)
设置Center为(0,0.85,0),Size为(1.5,1.7,1.6).
![](https://upload-images.jianshu.io/upload_images/9047377-f9ac8c62ef080106.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/273/format/webp)
设置好BoxCollider效果如下
![](https://upload-images.jianshu.io/upload_images/9047377-304ba5417eef8fd3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/416/format/webp)
3.6 添加音效
![](https://upload-images.jianshu.io/upload_images/9047377-bf4c9e57b6179b23.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/230/format/webp)
选择 AudioClip为EngineIdle
![](https://upload-images.jianshu.io/upload_images/9047377-44232eaf71c8a7b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/392/format/webp)
勾选Play On Awake 这样组件被唤醒的时候就会自动播放音效
![](https://upload-images.jianshu.io/upload_images/9047377-3076cd66a2e4622d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/275/format/webp)
勾选Loop, 音效就会循环播放
然后添加第二个AudioSource备用
![](https://upload-images.jianshu.io/upload_images/9047377-f5cf5a4d7cefd153.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/270/format/webp)
3.7 保存坦克为预设体
将Hierarchy里面的Tank拖放到wm/Prefabs里面,生成预设体
![](https://upload-images.jianshu.io/upload_images/9047377-c0a495c754dd4faf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/537/format/webp)
保存当前场景,本节内容到此结束,下节为大家讲解如果控制坦克移动和旋转.
---------------------------我是目录分割线---------------------------
《杜增强讲Unity之Tanks坦克大战》4-坦克的移动和旋转
《杜增强讲Unity之Tanks坦克大战》9-发射子弹时蓄力
《杜增强讲Unity之Tanks坦克大战》11-游戏流程控制
---------------------------我是目录分割线---------------------------
/// <-----------summary----------->
/// < ---------杜增强的博客--------->
/// < -专注于Unity游戏,VR/AR开发->
/// <-----------summary----------->