该源代码转载自Unity游戏案例中的TANKS代码中
------------来自第二次使用Unity3D制作游戏的游戏制作新人小白
一、代码自我解析
二、油管学习地址
三、Unity3D源代码
一、源代码自我解析
using UnityEngine;
public class TankMovement : MonoBehaviour
{
public int m_PlayerNumber = 1; // 玩家代号
public float m_Speed = 12f; // 移动速度
public float m_TurnSpeed = 180f; // 转动速度
public AudioSource m_MovementAudio; // 移动的音效
public AudioClip m_EngineIdling; // 坦克不动时的音效
public AudioClip m_EngineDriving; // 开坦克时的音效
public float m_PitchRange = 0.2f; // 为了让声音更有层次感,这个值是用来起降调升调作用的
private string m_MovementAxisName; // 用于向前和向后移动的输入轴的名称。
private string m_TurnAxisName; // 用于旋转的输入轴的称
private Rigidbody m_Rigidbody; // 加载刚体组件(坦克)
private float m_MovementInputValue; // 移动输入的当前值
private float m_TurnInputValue; // 旋转输入的当前值
private float m_OriginalPitch; // 当前场景需要加载的坦克引擎音效
private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
private void OnEnable ()
{
// 当坦克加载时,启用坦克遵循的运动学规律(坦克动起来了)
m_Rigidbody.isKinematic = false;
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
}
private void OnDisable ()
{
// 如果坦克被摧毁了,禁用坦克遵循的运动学规律(坦克不动了)
m_Rigidbody.isKinematic = true;
}
private void Start()
{
// 在玩家名字的基础上建立轴名(旋转轴和移动轴)
m_MovementAxisName = "Vertical" + m_PlayerNumber;
m_TurnAxisName = "Horizontal" + m_PlayerNumber;
// 存储音频的原始音高
m_OriginalPitch = m_MovementAudio.pitch;
}
private void Update()
{
// Store the player's input and make sure the audio for the engine is playing.
// 让玩家动起来,转起来!写在每帧更新的函数里
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
EngineAudio();
}
private void EngineAudio()
{
// Play the correct audio clip based on whether or not the tank is moving and what audio is currently playing.
// 两种音乐的切换播放以及让音乐的声调得到随机值以保持音乐的层次感
if (Mathf.Abs(m_MovementInputValue) < 0.1 && Mathf.Abs(m_TurnInputValue) < 0.1)
{
if (m_MovementAudio.clip == m_EngineDriving)
{
m_MovementAudio.clip = m_EngineIdling;
m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
m_MovementAudio.Play();
}
}
else
{
if (m_MovementAudio.clip == m_EngineIdling)
{
m_MovementAudio.clip = m_EngineDriving;
m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
m_MovementAudio.Play();
}
}
}
private void FixedUpdate()
{
// Move and turn the tank.
// 移动和旋转坦克
Move();
Turn();
}
private void Move()
{
// Adjust the position of the tank based on the player's input.
// 设置一个向量表示坦克要移动的方向、速度、目标
Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
// 让坦克移动起来
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
}
private void Turn()
{
// Adjust the rotation of the tank based on the player's input.
// 设置一个坦克旋转的值
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
// 旋转类对象定义
Quaternion turnRotation = Quaternion.Euler(0f,turn, 0f);
// 坦克转起来!
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
}
}
// 以上只是自己对于该代码的理解,如有误还望指出让我及时改正。
二、油管学习Unity地址
https://www.youtube.com/watch?v=paLLfWd2k5A
三、Unity3D中该案例源代码:
using UnityEngine;
namespace Complete
{
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 OnDisable ()
{
// When the tank is turned off, set it to kinematic so it stops moving.
m_Rigidbody.isKinematic = true;
// Stop all particle system so it "reset" it's position to the actual one instead of thinking we moved when spawning
for(int i = 0; i < m_particleSystems.Length; ++i)
{
m_particleSystems[i].Stop();
}
}
private void Start ()
{
// The axes names are based on player number.
m_MovementAxisName = "Vertical" + m_PlayerNumber;
m_TurnAxisName = "Horizontal" + m_PlayerNumber;
// Store the original pitch of the audio source.
m_OriginalPitch = m_MovementAudio.pitch;
}
private void Update ()
{
// Store the value of both input axes.
m_MovementInputValue = Input.GetAxis (m_MovementAxisName);
m_TurnInputValue = Input.GetAxis (m_TurnAxisName);
EngineAudio ();
}
private void EngineAudio ()
{
// If there is no input (the tank is stationary)...
if (Mathf.Abs (m_MovementInputValue) < 0.1f && Mathf.Abs (m_TurnInputValue) < 0.1f)
{
// ... and if the audio source is currently playing the driving clip...
if (m_MovementAudio.clip == m_EngineDriving)
{
// ... change the clip to idling and play it.
m_MovementAudio.clip = m_EngineIdling;
m_MovementAudio.pitch = Random.Range (m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
m_MovementAudio.Play ();
}
}
else
{
// Otherwise if the tank is moving and if the idling clip is currently playing...
if (m_MovementAudio.clip == m_EngineIdling)
{
// ... change the clip to driving and play.
m_MovementAudio.clip = m_EngineDriving;
m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
m_MovementAudio.Play();
}
}
}
private void FixedUpdate ()
{
// Adjust the rigidbodies position and orientation in FixedUpdate.
Move ();
Turn ();
}
private void Move ()
{
// Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
// Apply this movement to the rigidbody's position.
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
}
private void Turn ()
{
// Determine the number of degrees to be turned based on the input, speed and time between frames.
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
// Make this into a rotation in the y axis.
Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
// Apply this rotation to the rigidbody's rotation.
m_Rigidbody.MoveRotation (m_Rigidbody.rotation * turnRotation);
}
}
}