已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
TheTheTheThe CarCarCarCar TutorialTutorialTutorialTutorial PartPartPartPart 2 2 2 2 Creating a Racing Game for Unity 在Unity中创建赛车游戏 Part 2 Tweaking the Car调整汽车 If you ve followed along part 1 of this tutorial and assembled the car you are already at a point where the car is pretty awesome But if you have driven it around a bit you will probably have noticed that there is after all room for some improvement 如果你已经跟着教程的Part1部分学完并组装了汽车 那么在这个非常不错的汽车这里 你已经做好了准备 但如果你驾驶它已经开了一点 你将可能会注意到还有一些可以改 进的地方 This is where tweaking enters the picture In game development tweaking is a crucial part of making your game fun challenging awesome or whatever goal you have for your specific game The idea is that when you have setup the nuts and bolts that makes your game run there might be something that doesn t feel quite right maybe you want the car handling to be a bit different maybe you want the top speed to be slightly different or maybe you want to change the scene lights 这是一个要调整进入的地方的图片 在游戏中 开发调整会使你的游戏更有趣 更具挑 战性 更棒或你想在游戏中实现任何目标的一个最关键部分 这个想法就是当你设置螺 栓和螺母使你的游戏能运行时 那里有一些东西你可能感觉不怎么正确 也许你想让 赛车的操控有一点不同 也许你希望的最高速度稍为有点不同 或者也许你只想改变场 景内的灯光 A major strength of Unity is it s tweakability as you have seen all the public variables in your scripts are shown in the inspector so you can change values without going into the code And even more powerful When you ve made a change you just hit play and you will instantly see the result of that change You never have to wait for the scene to be rebuilt or for a major recompile of the code 一个Unity引擎的实力是它的可调整性 像你看到的那样 在你的脚本中所有的公共变 量在都显示在检查面板上 所以你能改变这些变量的值而不需要深入代码 并且更强大 的是 当你有了改变时你只需点击播放 你会立即看到这种变化的结果 你不用等待场 景被重构或等引擎重编译代码 CenterCenterCenterCenter ofofofof MassMassMassMass质心 Now the most obvious thing that needs to be tweaked is probably that the car can very easily be flipped around when turning if you haven t tried yet then play again and speed up a bit and then turn from side to side while speeding the car will flip over pretty easily The reason for this flipping is that we haven t yet defined the car s center of mass All the forces that are applied to the Rigidbody of the car are all applied at the Rigidbody s center of mass The center of mass of the Rigidbody will be calculated by Unity according to the Colliders attached to the Rigidbody either on the same GameObject or on child objects Since the center of mass of a car is typically not the center of the car and probably not the center of mass that Unity calculates we want to set the center of mass ourselves 现在大多数明显需要调整的事情可能已经做好了 但在转弯的时候汽车非常容易翻倒 如 果你还没有试过 你可以再试一次 加快点速度 从一边转到另一边 当速度起来后还 能容易发生空翻 发生空翻的原因是我们没有定义质心 所有被应用到汽车刚体上的力 都会被作用到质心 刚体的质心将被Unity根据附加到刚体上的碰撞器来计算 不管是 同一个游戏对象还是它的子对象 由于一辆汽车的质心通常不是车的中心位置 并且可 能不是Unity计算的质心 我们想设置我们自己的质心 The position of the center of mass for a car depends on the placement of the engine and other factors and it can vary a lot from one car model to another For the car in this tutorial project the center of mass could be a little behind the engine slightly above floor height of the car See it s position in this image 汽车的质心位置取决于发动机的位置和其他因素 从一辆汽车模型到另一辆汽车模型它 可能有很多变化 本教程中的汽车的质心应该稍稍位于引擎的后面一点 略高于地面的 车辆高度 看看随后的这张图片中它的位置 Create a new GameObject and drag it to the CarCarCarCar game object 建立一个新的游戏对象并拖拽它到汽车游戏对象上 Rename it to CenterOfMassCenterOfMassCenterOfMassCenterOfMass 重命名它为CenterOfMass Reset the CenterOfMassCenterOfMassCenterOfMassCenterOfMass s s s s Transform click the little cog wheel to the right of the Transform in the Inspector and select ResetResetResetReset This will give it the same position as its parent the car 重设CenterOfMass的变换 在检查面板的变换的右边的齿轮上单击并选择 Reset 将是它和父对象汽车处于同一个位置 Adjust its position to somewhere you like Either do it by dragging or type in the position A good position for this car s center of mass could be 0 0 1 0 65 将它调到你看到的那个位置 可以通过拖拽 或键入一个位置 汽车质心的一个不错的 位置可能在 0 0 1 0 65 In the Inspector assign the CenterOfMassCenterOfMassCenterOfMassCenterOfMass to the slot for it in the Car script Component In general it is a bad idea to have the center of mass be positioned to either side of the center in the x axis because this will make the steering behave oddly and thus we have also set the x variable of the position to 0 在检查面板上将质心对象分配给汽车脚本组件 上的变量插槽中 一般情况下 质心被定位在X 轴的中心一边是个坏的想法 因为这将使得操纵 变得很奇怪 那样我们也能设置位置中X变量为 0 You can also change the Camera s target to be the CenterOfMass Game Object instead of the car itself This will give a slightly different feeling play around with it and decide which setting you like the most 你也能改变照相机的目标到游戏对象的质心 以替换汽车自身 这将给你一个略微不同 的感觉 驾驶它并且决定怎么设置才是你最喜欢的 SuspensionSuspensionSuspensionSuspension悬挂系统 Another factor that can heavily change the behavior of the car is the properties of its suspension The job of a car suspension is to maximize the friction between the tires and the road surface When you are driving the car over a bump all of the wheel s vertical energy gets transfered to the frame If we did not have an intervening structure this could easily result in the wheel loosing contact with the road completely and afterwards slamming down into the road because of the force of gravity The suspension is that intervening structure 另一个因素是可以严重改变汽车行为的属性是它的悬挂系统 汽车的悬挂系统工作是最 大限度地实现与轮胎和路面摩擦 当你驾驶的汽车在颠簸路面时 如果我们没有一个中 介结构 这很容易导致车轮与路面完全失去联系 并且随后因为重力的作用使汽车掉到 地面上 悬挂系统就是那个干预机构 We have three different variables to tweak from the Inspector the range the damper and the spring All are part of the WheelCollider class that we use on the car s wheels 在检查面板上我们有三个不同的变量需要调整 范围 阻尼器和弹簧 它们都是我们在 汽车的轮子中使用的轮子碰撞器的组成部分 To the left we see the car with the standard settings and to the right we see it with a much larger suspension range Combined with the spring and damper properties you can make it behave like everything from a formula one car to a huge monster truck Of course the graphics need to match the settings to make it believable though 我们看到的左边的汽车是按照标准设置的 右边的这个有个大的悬挂范围 合并有弹簧 和阻尼器的属性 你可以使它的特征看起来就像是从一辆一级方程式赛车到一辆巨大的 怪物卡车 当然 图像需要匹配这些设置以使它变得可信 SuspensionSuspensionSuspensionSuspension rangerangerangerange悬挂范围 This is the length of the suspension from when it is a state of being fully compressed to the largest distance it can be away from the frame of the car 这是悬挂的高度范围 在汽车车架上的悬挂状态为从全压缩的 最小距离 到最大距离 SuspensionSuspensionSuspensionSuspension springspringspringspring悬挂弹簧 The value set here determines the stiffness of the suspension spring Setting it very high makes it more likely that the suspension will be fully extended so that the wheels will be far away from the frame and setting it very low will make the suspension much more bouncy When tweaking this value it will be clear that the mass of the car also has a lot to say here A very heavy car requires a spring with more stiffness than a very light car By default we have set the rear suspension spring to be less stiff than the front and the reason is that the center of mass is distributed more to the front side requiring better suspension there Playing around with different values for both front and rear suspension can yield very different results 这里设置的值决定了悬架弹簧的刚性 把它设的很高可以使它看起来像是悬挂完全伸展 的 所以车轮将远离车架 把它设的很小 将使悬挂更颠簸 当调整这个值时 它会变 得很清晰 在这里 汽车的质量也有许多可以说的 一个非常重的汽车比轻的汽车要求 有更大的刚性 默认情况下 我们设置后悬挂弹簧比前置弹簧软 原因是 质心分布比 较的靠前端 靠前端的悬挂比较好 设置不同的前和后的悬挂值 驾驶它可以产生不同 的结果 SuspensionSuspensionSuspensionSuspension damperdamperdamperdamper悬挂阻尼器 Dampening helps controlling the motion in the suspension Without dampening the suspension spring would extend and release it s energy at an uncontrollable rate it would extend at it s natural spring frequency until all the energy stored in it was used up This would result in an extremely bouncy and uncontrollable car The damper or shock controller turns the unwanted kinetic energy into heat that gets transferred away in the hydraulic fluid making the ride a lot smoother 在悬挂系统中阻尼能提供控制方案 如果没有阻尼 悬挂弹簧将伸展并且用无法控制的 速度释放它的能量 它将扩展以自然的弹簧的频率直到储存的能量全部释放完 这将导 致汽车变成一个弹性很大并且无法控制的汽车 阻尼器和震动控制器将不需要的动能转 换成热能 在液压系统中 使震动 越来越弱 使开车变得很平滑 DragDragDragDrag MultiplierMultiplierMultiplierMultiplier阻力系数 When we added the Rigidbody to the car we saw that it had a drag property This drag is the intertia or air resistance that affects the Rigidbody making it harder to move When a car is designed a lot of consideration is often put into giving it a shape that minimizes the friction from the air resistance when it moves But since a car is meant to move forwards the shape takes this into account just take a look at the car model in the editor from the front the sides and the top and you will realize that it is a lot more streamlined when seen from the front than from sides and top 当我们给汽车增加一个刚体组件时 我们看到它有一个阻力属性 这种阻力是惯性或空 气阻力对刚体的影响 它会使刚体的移动变得艰难 在设计汽车时 从移动时的空气阻 力来说 更多的考虑往往是使它的形状 最大限度地减少摩擦 但由于汽车是为了向前 移动 形状要考虑到总体设计中 在前面部分的编辑其中刚刚看到的汽车模型 侧面和 顶部 从前面看比从顶部和侧面看将会做很多简化 We take this into account by creating our own drag multiplier property that we use instead of the drag property built into the rigidbody Take a look at the Car script component in the Inspector where you will see that we have a DragDragDragDrag MultiplierMultiplierMultiplierMultiplier variable which is a vector with x y and z values This makes it possible for us to set different drag values for the front sides and top of the car mimicking the real conditions when driving a car more accurately 我们把这个考虑到总体设计中 建立我们自己的阻力系数属性 我们使用它来代替刚体 组件中的阻力系数属性 刚体本身有Drag和Angular Drag属性 看看检查面板上的 汽车脚本组件 在那里能看到一个Drag Multiplier 阻力系数 变量 拥有x y z向量 值 这使我们给汽车的前面 顶部 侧面设置不同的阻力系数成为可能 在驾驶汽车时 能更真实的模仿实际的情况 The X value is the drag to the side X值是侧面的阻力系数 The Y value is the drag to the top Y值是顶部的阻力系数 The Z value is the drag to the front Z值是前面的阻力系数 The x value is important in controlling the force that prevents the car from sliding sideways when turning The higher the x value the more sideways resistance 对于控制汽车转弯时的影响来说X值非常重要 高的X值可以提供更多的侧移阻力 The z value is by far the most interesting one because it can lower or increase the force that slows the car s velocity down If you set it to less than 1 you will get less resistance faster acceleration and a higher top speed More than 1 and the car must struggle against a more powerful force in order to move forwards making it slower The drag values are very sensitive so you are advised to experiment with small changes when tweaking the drag Z值是迄今为止最有趣的 因为它可以降低或增加阻力以减缓赛车的速度下降 如果你 设置的值比1小你将减少阻力 加速度会更快和最高速度更高 大于1则汽车为了向前移 动必须与更强大的阻力相抗衡 并使汽车变慢 阻力值很敏感 所以建议您尝试调整与 变化时 小幅度拖动 尽量使值变化的幅度小一些 Since the car is not supposed to travel upwards the y value is not as interesting to change The most important force controlling the car in the y axis is after all the gravity that affects the rigidbody 由于汽车不会向上开 所以Y值没有兴趣改变它 在Y轴方向汽车最重要的力控制是所有 的重力对刚体的影响 Speed Speed Speed Speed turningturningturningturning andandandand gearsgearsgearsgears速度 旋转和档位 Now we ve gotten to the more obvious variables that also has a quite large impact on your cars behavior 现在我们已经得到了很多变量 它们对你的汽车的行为有相当大的影响 The TopTopTopTop SpeedSpeedSpeedSpeed variable is a no brainer This sets how fast or slow your car can go Since our car model is a lot simpler than a model for a real car and we for example don t really have any values to set that affects it s acceleration except the drag the Top Speed variable will also indirectly affect the acceleration Making the car very fast will also make it reach a high velocity equally higher and vise versa If you want to play with top speed and acceleration you could try tweaking both the Top Speed value and the drag s z variable which was the air resistance in the forward direction 最高速度变量是一个无脑量 不需要进行计算的量 它设置你的汽车行驶的最快 或最 慢 的速度 由于我们的汽车模型对比真实的汽车来说是一个简单的模型 例如 我们 实际没有设置任何值来影响它的加速度 除了阻力 最高速度变量也将间接影响加速度 使得汽车很快也将使得它能达到一个很高的速度 反之亦然 如果你想用一个很高的速 度和加速度来玩 你可以试着调整二者的最高速度值并改变Z轴方向的阻力值 在前进 方向的空气阻力 For turning we have two variables MaximumMaximumMaximumMaximum TurnTurnTurnTurn and MinimumMinimumMinimumMinimum TurnTurnTurnTurn Both are values for how good the car is at turning A high value means excellent turning and a low value is very limited turning ability We are using them together in the car s script to change the cars ability to turn based on how fast it is going 对于转弯我们有两个变量 Maximum Turn 最大转弯线速度 和Minimum Turn 最小转弯线速度 二者都是让汽车如何更好的转弯的值 值很高意味着转弯能力很优 秀 值很低意味着转弯能力很有限 在汽车的脚本中我们一起使用它们来改变汽车的转 弯能力 在行驶时 拐弯 究竟有多快 At very low speeds it is the value set for MaximumMaximumMaximumMaximum TurnTurnTurnTurn that is used when turning 在非常低的速度时 它是转弯时的所使用的最大转弯设定值 The higher the car s speed gets the closer it s turning ability gets to the Minimum Turn最大转弯 在汽车获得更高速度时 越接近获得它的最小转弯 上述的解释让人产生迷惑 不知道这两个变量究竟是什么物理量 是指的转弯时的线速度 还是转弯半径 或者是转弯时的角速度 但实在令人难以理解 啊 通过另一个函数终于 看明白了这是转弯时的线速度 读书笔记 1 计算角速度 function ApplySteering canSteer boolean relativeVelocity Vector3 if canSteer var turnRadius float 3 0 Mathf Sin 90 steer 30 Mathf Deg2Rad 实 际转弯半径 var minMaxTurn float EvaluateSpeedToTurn rigidbody velocity magnitude 转弯的线速度 Mathf Clamp是在 minMaxTurn 10和minMaxTurn 10之间剪裁值 并返回剪裁 值 这个函数主要是用来将值控制在一定范围内 minMaxTurn 10实际已经是角速度了 v r 默认的转弯半径为10米 turnSpeed 是修正后的角速度 varturnSpeed float Mathf Clamp relativeVelocity z turnRadius minMaxTurn 10 minMaxTurn 10 2 调用用行驶速度换算的转弯线速度 function EvaluateSpeedToTurn speed float 这个函数用来计算转弯时的线速度 用行驶的速度折算 大于最高速的1 2 有最小转弯线速度 小于等于1 2 线速度是计算出的值 if speed topSpeed 2 return minimumTurn var speedIndex float 1 speed topSpeed 2 return minimumTurn speedIndex maximumTurn minimumTurn 3 再回头看其中的转弯半径的计算 function GetInput throttle Input GetAxis Vertical 油门 按向前即为油门 鼠标向前 steer Input GetAxis Horizontal 转向 左右按键 鼠标左右移动 function ApplySteering canSteer boolean relativeVelocity Vector3 if canSteer var turnRadius float 3 0 Mathf Sin 90 steer 30 Mathf Deg2Rad 实 际转弯半径 应该是r l 半径 弧长 角度 4 看如何转动车轮 function UpdateWheelGraphics relativeVelocity Vector3 if w steerWheel 获得转向柱的欧拉角 var ea Vector3 w wheelGraphic parent localEulerAngles 乘 maximumTurn 只 是 为 了 驾 驶 转 弯 时 能 看 到 车 轮 的 偏 转 如 果 不 乘 maximumTurn 转弯不受影响 但车轮始终是直的 这几句只影响显示 ea y steer maximumTurn 又赋回给w中的轮子的父即转向柱 w wheelGraphic parent localEulerAngles ea w tireGraphic Rotate Vector3 right w groundSpeed z wheelRadius Time deltaTime Mathf Rad2Deg 这句好理解 5 真正控制刹车时转向的语句 function ApplySteering canSteer boolean relativeVelocity Vector3 if canSteer 绕着世界坐标的transform position transform right turnRadius steer点 的transform up Y轴 旋转 turnSpeed Mathf Rad2Deg Time deltaTime steer角 角的计算公式是角速 度 弧度转度 180 3 14 帧时间 转动控制量 transform RotateAround transform position transform right turnRadius steer transform up turnSpeed Mathf Rad2Deg Time deltaTime steer 其实这
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年突发事件处置知识考察试题及答案解析
- 2025年专业技术人员继续教育公需课试题库及答案
- 数字内容创作市场需求与发展策略与技术标准
- 细胞与基因治疗研发专家考试题及答案
- 2021年12月北京市公安机关面向社会招录人民警察考试公安专业科目真题
- 建设银行秋招真题及答案
- 金融科技产品经理考试题及答案
- 江西铜业面试题及答案
- 广西公务员b类考试真题及答案
- 2023年国家电网招聘之通信类通关考试题库带答案解析
- 2025年导游资格证考试专项训练 导游业务与政策法规押题试卷
- 公路维修养护质量保证体系
- 中国软件行业协会:2025中国软件行业基准数据报告 SSM-BK-202509
- 国家电投集团五凌电力有限公司笔试
- 2025至2030智利电力行业项目调研及市场前景预测评估报告
- 时间介词in-on-at课件
- 电厂工控网络安全培训课件
- 合理用药培训课件内容
- 室内装修工程监理实施细则
- 个体工商户设立章程模板及编写指南
- DB15T 1969-2020 绵羊人工授精及同期发情技术操作规程
评论
0/150
提交评论