Unity3d--让我们的小坦克动起来
2017-09-12 11:05
351 查看
一、Tank的movement 源码解读
我将一些理解补充在代码的注释里了using UnityEngine; 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 void Awake() { m_Rigidbody = GetComponent<Rigidbody>(); // 一个component是所有挂载在gameobject上的属性的基类,其派生类包括 rigidbody等等,在Unity3dUI界面可以手段add a component,然后在该gameobject的脚本中调用getc be97 omponent将设置好的信息赋给派生类实例 } 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; } private void OnDisable() { // When the tank is turned off, set it to kinematic so it stops moving. m_Rigidbody.isKinematic = true; } 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); FixedUpdate (); 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); } }
TANK的前后运动模块主要是以下代码
m_MovementAxisName = "Vertical" + m_PlayerNumber; //1 m_MovementInputValue = Input.GetAxis(m_MovementAxisName); //2 Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime; //3
2、Input.GetAxis();函数主要有两种用法:
这里是键盘操作类型:输入了string类型”Verticl“,这里可能就有疑问了,verticl为什么能实现ws建物体z轴上的前后移动:答案见下图:
在input的设置中预定义的,我猜测unity3d中是采取某种宏定义实现的
m_MovementInputValue为float类型的数字(-1,1),transform.forward是一个三元向量(1,0,0)其和速度、时间、设置轴上移动的比例的积就是forward方向上移动的距离(z轴)
相关文章推荐
- 一、创建Assetbundle 在unity3d开发的游戏中,无论模型,音频,还是图片等,我们都做成Prefab,然后打包成Assetbundle,方便我们后面的使用,来达到资源的更新。
- 细数那些年我们一起玩过的Unity3D游戏(unity开发的游戏有哪些)
- Unity3D AI人工智能(坦克模型自动寻怪,自动追击)
- [Torque社区活动] 我们TORQUE社区将开始利用TORQUE引擎开发我们的第一个范例游戏[坦克],欢迎参加
- [Unity3D入门]分享一个自制的入门级游戏项目"坦克狙击手"
- [Torque项目活动] 我们TORQUE社区将开始利用TORQUE引擎开发我们的第一个范例游戏[坦克],欢迎参加
- Unity3d切水果,坦克,投篮游戏视频
- TankWar 单机(JAVA版) 版本0.4~版本0.6 让坦克动起来
- Cocos2d-x 单点触摸--让我们用手指动起来的精灵
- Unity3d切水果,坦克,投篮游戏视频
- 我们来谈谈Unity3D 优化
- 我们月光工作室和我们的3款游戏被国内Unity3D游戏引擎第一技术论坛--“游戏蛮牛” 报道啦!
- 【Unity3D游戏开发学习笔记】(四)一切都动起来—Animator组件的应用
- Unity3D AI人工智能(坦克模型自动寻怪,自动追击)
- 浅谈在Unity3D中实现坦克履带模拟器
- 坦克大战底层框架搭建并思路分析(二)=》让坦克动起来
- 让我们来导出unity3d中的mainTexture贴图转换保存为png
- 我们一起学习unity3d
- 【 java版坦克大战--事件处理】 让坦克动起来--事件处理的准备
- 【Unity3D基础】让物体动起来②--UGUI鼠标点击逐帧移动