Unity3d 汽车物理系统
2012-12-25 22:00
288 查看
大家好,认识一下,我叫山西小龙,QQ:1357098586,希望大家能够互相帮助,我的文章偏多与机械方面,呵呵!
第一篇,就写个物理车的!
首先,资料的来源!http://www.gotow.net/andrew/wordpress/?page_id=78在这个链接的底部有下载的链接,如果下载不 到, 我可以共享出来!
第二部,开始一些翻译和讲解!
1.物理车的碰撞!
View Code
解释:
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
设置车轮的motorTorque可以使车轮滚动!
FrontLeftWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
设置车轮的steerAngle可以控制车轮围绕自身Y轴的转向角度!这里的旋转角度是-10度到10度之间!
作者疑问:
第57,58,60,61行代码,
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
Input.GetAxis("Vertical")会返回-1,也就是说motorTorque是负值!
但是官方文档却说,
在轮轴上的电机力矩。根据方向正或负。
To simulate brakes, do not use negative motor torque - use brakeTorque instead.
模拟刹车,不要使用电机力矩负值,使用brakeTorque代替。
这两种说法之间可有冲突,请高手指点!
下篇是车轮与其他物体的碰撞,待续……!
第一篇,就写个物理车的!
首先,资料的来源!http://www.gotow.net/andrew/wordpress/?page_id=78在这个链接的底部有下载的链接,如果下载不 到, 我可以共享出来!
第二部,开始一些翻译和讲解!
1.物理车的碰撞!
View Code
1 // ----------- CAR TUTORIAL SAMPLE PROJECT, ? Andrew Gotow 2009 ----------------- // Here's the basic car script described in my tutorial at www.gotow.net/andrew/blog. // A Complete explaination of how this script works can be found at the link above, along // with detailed instructions on how to write one of your own, and tips on what values to // assign to the script variables for it to work well for your application. // Contact me at Maxwelldoggums@Gmail.com for more information. //这些是外国人写的赛车例子,我个人认为非常好,是用JS写的! //我来注解一下! // These variables allow the script to power the wheels of the car. //前左轮和前右轮 var FrontLeftWheel : WheelCollider; var FrontRightWheel : WheelCollider; // These variables are for the gears, the array is the list of ratios. The script // uses the defined gear ratios to determine how much torque to apply to the wheels. //齿轮转数系数 var GearRatio : float[]; //档位 var CurrentGear : int = 0; // These variables are just for applying torque to the wheels and shifting gears. // using the defined Max and Min Engine RPM, the script can determine what gear the // car needs to be in. //向前的动力 var EngineTorque : float = 600.0; //引擎最大的转速 var MaxEngineRPM : float = 3000.0; //引擎最小的转速 var MinEngineRPM : float = 1000.0; private var EngineRPM : float = 0.0; function Start () { // I usually alter the center of mass to make the car more stable. I'ts less likely to flip this way. //设置车的中心点,一般在车的底盘下,这样车行驶起来稳定,不会轻易翻车! rigidbody.centerOfMass.y = -1.5; } function Update () { // This is to limith the maximum speed of the car, adjusting the drag probably isn't the best way of doing it, // but it's easy, and it doesn't interfere with the physics processing. //设置车的阻力为 车的向前的速度的长度除以250 rigidbody.drag = rigidbody.velocity.magnitude / 250; // Compute the engine RPM based on the average RPM of the two wheels, then call the shift gear function //档位的转速值车 EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear]; ShiftGears(); // set the audio pitch to the percentage of RPM to the maximum RPM plus one, this makes the sound play // up to twice it's pitch, where it will suddenly drop when it switches gears. //音频源的音调 发动机的引擎转速越快,则音调越高! audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0 ; // this line is just to ensure that the pitch does not reach a value higher than is desired. //超过2.0则归位到2.0 if ( audio.pitch > 2.0 ) { audio.pitch = 2.0; } // finally, apply the values to the wheels. The torque applied is divided by the current gear, and // multiplied by the user input variable. //前面的两个轮子的力矩 FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical"); FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical"); Debug.Log(Input.GetAxis("Vertical")); // the steer angle is an arbitrary value multiplied by the user input. FrontLeftWheel.steerAngle = 10 * Input.GetAxis("Horizontal"); FrontRightWheel.steerAngle = 10 * Input.GetAxis("Horizontal"); } //换档位 function ShiftGears() { // this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make // the engine RPM fall within the desired range. The gear is then set to this "appropriate" value. //如果当前引擎的值大于最大引擎 if ( EngineRPM >= MaxEngineRPM ) { var AppropriateGear : int = CurrentGear; //则从0开始,往上查,如果左轮的rpm乘以齿轮数小于 for ( var i = 0; i < GearRatio.length; i ++ ) { if ( FrontLeftWheel.rpm * GearRatio[i] < MaxEngineRPM ) { AppropriateGear = i; break; } } CurrentGear = AppropriateGear; } if ( EngineRPM <= MinEngineRPM ) { AppropriateGear = CurrentGear; for ( var j = GearRatio.length-1; j >= 0; j -- ) { if ( FrontLeftWheel.rpm * GearRatio[j] > MinEngineRPM ) { AppropriateGear = j; break; } } CurrentGear = AppropriateGear; } }
解释:
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
设置车轮的motorTorque可以使车轮滚动!
FrontLeftWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
设置车轮的steerAngle可以控制车轮围绕自身Y轴的转向角度!这里的旋转角度是-10度到10度之间!
作者疑问:
第57,58,60,61行代码,
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
Input.GetAxis("Vertical")会返回-1,也就是说motorTorque是负值!
但是官方文档却说,
在轮轴上的电机力矩。根据方向正或负。
To simulate brakes, do not use negative motor torque - use brakeTorque instead.
模拟刹车,不要使用电机力矩负值,使用brakeTorque代替。
这两种说法之间可有冲突,请高手指点!
下篇是车轮与其他物体的碰撞,待续……!
相关文章推荐
- [Mark] Unity3d之 汽车物理系统
- unity3d物理赛车系统
- 【和我一起学习Unity3D】初识物理系统
- Unity3D汽车物理引擎的安卓重力感应适配
- Unity3d 汽车物理引擎 WheelCollider总结
- unity3d物理赛车系统
- 基于unity3d的物理赛车驱动系统分享
- unity3d中的物理系统
- Unity3d物理汽车
- 基于unity3d的物理赛车驱动系统分享
- Unity3D_脚本和物理系统的结合使用
- unity3d物理赛车系统
- unity3d物理赛车系统
- LTE系统调试记录13: LTE物理传输资源(1)-帧结构和OFDM符号
- 智能手机导航将迅速取代传统汽车导航系统
- unity3d的GUI元素的界面坐标系统总结(有公式)
- 用JavaScript玩转游戏物理(一)运动学模拟与粒子系统
- 汽车的TPMS智能胎压监测系统 -
- Android游戏开发20:物理游戏之重力系统开发--圆形自由落体Demo
- [Unity3D]Unity3D游戏开发之自己主动寻路与Mecanim动画系统的结合