Unity Tutorial - Tanks! 总结(上)
尝试自己总结一下整个 Tanks 游戏,由于整个游戏大部分的脚本和 prefabs 都是 unity 官方做好给我们的,所以能总结的地方大概也只是整个游戏的框架结构以及实现思路一些比较浅的层面。
首先,unity 教程中将整个游戏分成了 8 节视频供我们一同完成,如下:
接下来,我们一个一个章节的来总结
01 - PROJECT & SCENE SETUP
创建工程和下载官方的 asset ,直接在 window -> Asset Store
中输入所需要的教程,下载后 import 进 project 就可以了。
随后你可以选择自己喜欢的布局来进行开发,官方一般推荐使用 layout -> 2 by 3
之后就是 LevelArt 中的一些颜色设置了,主要是让场景更加逼近真实沙漠,就按着教程做就是了
02 - TANK CREATION & CONTROLS
从这里开始才算真正有一点做游戏的感觉,这里我们要做的事情是将官方提供好的 model 加工成为一个 prefab
,什么是 prefab
,官方文档的说明是:
Fortunately, Unity has a Prefab asset type that allows you to store a GameObject object complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. Any edits made to a prefab asset are immediately reflected in all instances produced from it but you can also override components and settings for each instance individually.
翻译过来大概就是 unity 的 prefab
允许你储存一个设置好了各种属性,添加了各种组件的一个 Game Object
模板,它是预先被我们设定好的,当我们在 scene
中需要的时候供我们随时用脚本任意数量的实例化,并且我们可以在脚本中为每一个独立的实例重写其属性及组件。
- 理解
prefab
是 unity 中一个非常基础而且重要的概念,因为游戏中几乎所有的角色、特效都会先制作成prefab
并在脚本中调用
这里坦克模型的一些尘埃特效,添加引擎音效就不说了,主要看一下控制坦克移动的脚本 TankMovement.cs
,里面展示了一种非常典型的控制角色移动的方式
首先,考虑清楚我们在这个脚本中到底需要做些什么
Script Checklist
1. GET THE INPUT
2. SETUP THE AUDIO
3. SETUP FORWARD/BACK MOVEMENT
4. SETUP TURNING
- 翻译过来,我们需要做的其实就是:设置坦克的移动按钮,设置坦克两种状态下的音效,以及坦克前后移动的速度,左右转向的速度
先看官方代码
public class TankMovement : MonoBehaviour
{
public int m_PlayerNumber = 1; // Used to identify which tank belongs to which player. This is set by this tank's manager.
public float m_Speed = 12f; // How fast the tank moves forward and back.
public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second.
public AudioSource m_MovementAudio; // Reference to the audio source used to play engine sounds. NB: different to the shooting audio source.
public AudioClip m_EngineIdling; // Audio to play when the tank isn't moving.
public AudioClip m_EngineDriving; // Audio to play when the tank is moving.
public float m_PitchRange = 0.2f; // The amount by which the pitch of the engine noises can vary.
private string m_MovementAxisName; // The name of the input axis for moving forward and back.
private string m_TurnAxisName; // The name of the input axis for turning.
private Rigidbody m_Rigidbody; // Reference used to move the tank.
private float m_MovementInputValue; // The current value of the movement input.
private float m_TurnInputValue; // The current value of the turn input.
private float m_OriginalPitch; // The pitch of the audio source at the start of the scene.
private ParticleSystem[] m_particleSystems; // References to all the particles systems used by the Tanks
private void Awake ()
{
m_Rigidbody = GetComponent<Rigidbody> ();
}
private void OnEnable ()
{
// When the tank is turned on, make sure it's not kinematic.
m_Rigidbody.isKinematic = false;
// Also reset the input values.
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
// We grab all the Particle systems child of that Tank to be able to Stop/Play them on Deactivate/Activate
// It is needed because we move the Tank when spawning it, and if the Particle System is playing while we do that
// it "think" it move from (0,0,0) to the spawn point, creating a huge trail of smoke
m_particleSystems = GetComponentsInChildren<ParticleSystem>();
for (int i = 0; i < m_particleSystems.Length; ++i)
{
m_particleSystems[i].Play();
}
}
private void