Unity3d--让我们的小坦克动起来
一、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的脚本中调用getcomponent将设置好的信息赋给派生类实例}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轴)
Unity3d--让我们的小坦克动起来相关推荐
- Unity3D学习之路——AI小坦克
Unity3D学习之路--AI小坦克 作业要求: 坦克对战游戏 AI 设计 从商店下载游戏:"Kawaii" Tank 或 其他坦克模型,构建 AI 对战坦克.具体要求 使用&qu ...
- 我所知道坦克大战(单机版)之添加多个机器坦克、让机器坦克动起来、让坦克智能起来...
本章目的 添加多个机器坦克 让机器坦克动起来 让坦克智能起来 一.添加多个机器坦克 目前我的坏阵营的坦克只有一个,是我们手动创建出来的 但是我们现在想要玩的过瘾,杀的痛快就要创建多个坦克,也需要使用一 ...
- Unity3d笔记——利用Animator使模型动起来
Unity3d笔记--利用Animator使模型动起来 一. 从Asset Store中下载合适的模型 二. 为模型创建Avatar 三. 增加AnimatorController文件 四. 将Ani ...
- python画图之小坦克
python小坦克 直接上代码 from turtle import * pencolor('black') pensize(5) speed(0)#调画图速度 ht()#隐藏画笔 forward(2 ...
- unity3D期末大作业,坦克射击游戏
unity3D期末大作业,坦克射击游戏 坦克射击游戏,可以发射子弹打击物体,坦克可以撞碎墙壁,树木,有背景音效详情如下动态图:(下载链接在文章末) 制作过程: 首先,从Asset Store中下载并i ...
- 【微信小程序—动画工坊】动画入门keyframe
[微信小程序-动画工坊]动画入门 前情 需要了解的前置知识: 子代选择器 基本布局 分析 可以将任务进行一下拆分. 如何让小球跑动起来? 通过@keyframe创造动画函数,然后再通过animatio ...
- 使用jQuery让我们的小方块动起来
让我们的小方块动起来 首先第一步给它一个div,再用jQuery给这个div设置一些样式,设置什么样式呢?就由我来为大家解读: 就是先设置宽高,再来张漂亮的图片 让图片的大小显示最大我们需要给它这个属 ...
- 树莓派小坦克玩具设想
树莓派小坦克玩具设想 想做一个树莓派小坦克玩具: 1.小坦克用两条履带驱动 2.小坦克头上有摄像头一个,超声波测距模块一个,激光灯一个,以上三者安装在一个可上下转动各45度的平台上,可以抬头.低头观察 ...
- 小坦克的FidderScript
通过前一篇博客 [小坦克: Fiddler教程],我们了解了Fiddler的基本用法, 现在我们来看看Fiddler的高级用法. Fiddler Script. Fiddler中的script ...
最新文章
- 用python画小猪佩奇(非原创)
- springboot 得到端口_带你入门SpringBoot
- GitHub:人群密度估计最全资料集锦
- java执行jar中的main_浅谈java 执行jar包中的main方法
- 【译】 Sparky: A Lightning Network in Two Pages of Solidity
- V-3-3 在没有vCenter的情况下,复制虚拟机
- Offline spike sorter 神经元脉冲单位分类软件
- sql2012找不到到服务器,sql server 2012导入数据时找不到服务器名称
- 异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
- 贺利坚老师汇编课程39笔记:用于内存寻址的寄存器同时引入BP
- 安装wordpress时候报错:Parse error: syntax error, unexpected '.', expecting '' or variable (T_VARIABLE)
- 多个excel工作簿合并_EXCEL2016中如何快速合并多个工作簿中内容到一个工作表
- scum服务器在线玩家,SCUM服务器选择推荐 官服跟私服有什么区别
- JDK官方下载(旧版本,以前老版本)
- 机器学习-决策树算法
- 《鬼谷子》飞箝第五(译文)
- 畜牧业的论文发表一般多少钱
- 清醒认识数据第一步,把关数据质量
- 2020年iOS 和Android程序员请开始修炼内功
- 公式编辑神器-MathType