unity-----------------------------关于WheelCollider设置或小车运动的注意事项

it2025-02-07  38

转载:Unity3d 汽车物理引擎 WheelCollider总结

 

                                                WheelCollider总结

    写了前面两篇文章,我想总结一下WheelCollider!

    让我们能够更清晰的学会物理车的开发!

    1.车的层次结构

       一般这样分,车身,车身的包围盒,四个轮子和四个轮子的碰撞器!

    2.分析驱动车的原理

       车主体本身加了一个Rigidbody,物理车我们可以加一个到两个包围盒来

       包住车身(但是绝对不能包住车轮,否则物理车不运动!)物理车有前后四个轮子,那么有的车需要前轮进行驱动,后轮进 行辅助,有的车需要后轮进行驱动,前轮进行辅助!这个很好办,如果是哪个

       轮进行驱动,就设置这个轮子的WheelCollider.motorTorque,也就是轮子

       的动力,WheelCollider.steerAngle,设置轮子的旋转角度,这样就基本OK

       了,那么车轮如何随着地面的起伏行走呢?这就是另一个问题了!

       关于如何使车轮始终保持在地面之上,并且随着地面的起伏进行起伏的运    动?

       可以从车轮碰撞器(WheelCollider)向地面发射一条射线,用Physics.Raycast来获取一个RaycastHit;获取碰撞点,调整车轮的位置,

      这里对WheelCollider的属性做一个补充,是拷贝的http://game.ceeger.com/Components/class-WheelCollider.html的片段!

 

    Properties 属性

Center  中心 Center of the wheel in object local space.  车轮在对象自身坐标的中心位置 Radius  半径 Radius of the wheel.   车轮的半径大小 Suspension Distance  悬挂距离 Maximum extension distance of wheel suspension, measured in local space.   Suspension always extends downwards through the local Y-axis.  最大车轮悬挂伸缩距离,以自身坐标计算。悬挂总是通过自身Y轴伸展向下。 Suspension Spring  悬挂弹簧 The suspension attempts to reach a Target Position by adding spring and damping forces.  通过添加弹簧和阻尼力,悬挂试图达到目标位置(Target Position)。     Spring  弹簧 Spring force attempts to reach the Target Position. A larger value makes the suspension reach the Target Position faster. 弹簧力尝试达到目标位置(Target Position),大的值可使悬挂快速达到目标位置(Target Position)。     Damper  阻尼器 Dampens the suspension velocity. A larger value makes the Suspension Spring move slower.  阻尼器控制悬挂速度,大的值可使悬挂弹簧移动变慢。     Target Position  目标位置 The suspension's rest distance along Suspension Distance. 0 maps to   fully extended suspension, and 1 maps to fully compressed suspension.   Default value is zero, which matches the behavior of a regular car's   suspension.  悬挂的静止状态距离沿着悬挂距离,0值充分伸展悬挂,1值充分压缩悬挂,默认值为0,这是一个标准的汽车悬挂行为 Mass  质量 The Mass of the wheel.  车轮的质量 Forward/Sideways Friction  向前摩擦力 Properties of tire friction when the wheel is rolling forward and sideways. See  Wheel Friction Curves section below.  当车轮向前/侧向滚动时的摩擦力属性。参见下面车轮摩擦力曲线部分。

     对于物理车就写到这儿,以后理解的更深一些,就继续续写……

     下面我就物理,Rigidbody,以及“关节”的分别做个教程,呵呵……!

 

 

                                 Unity3d物理汽车第二篇

             1.第一篇留下的问题,还没有得到解答!我目前的结论就是可以赋值为负值!

             2.我自己用了个方块做为车身,然后用四个轮子,把代码套了上去,车子竟然一动不动!

             并且出现了以下警告:

             MissingComponentException: There is no 'AudioSource' attached to the "Cube" game object, but a script is trying to access it.You probably need to add a AudioSource to the game object "Cube". Or your script needs to check if the component is attached before using it.PlayerCar_Script.Update () (at Assets/Scripts/Car Control/PlayerCar_Script.js:48)

我贴出来48行的代码:

 audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0 ;

就是audio,但是我忘了给物理车添加Audio Source(音频源),导致程序出现警告,导致后面的程序没有执行!

所以,汽车就没有开动了!当大家遇到警告的提示之后,一定要分析原因,检查问题!养成良好的分析问题的习惯,这就是积累经验的时候了!

Audio Source中一个参数pitch是音频源的音调!这里的audio.pitch的最大值是2;

           3.现在贴出来车轮与地面碰撞的代码!JS语言写的!

            //车轮碰撞器

             var CorrespondingCollider : WheelCollider;            //车轮印            var SlipPrefab : GameObject;

           //车轮的旋转值           private var RotationValue : float = 0.0;

          //更新函数

           function Update () {

                  //光线投射碰撞,用来获取从raycast函数中得到的信息反馈的结构。

                  var hit : RaycastHit;

                  //WheelCollider是挂在一个空物体上的Transform上,所以,要用空物体的变换去变换

                  WheelCollider的中心点(本地坐标的点),这样ColliderCenterPoint就是世界坐标中的点了!

                  var ColliderCenterPoint : Vector3 = CorrespondingCollider.transform.TransformPoint( CorrespondingCollider.center );

                 //最主要的部分

                 //光线投射,从ColliderCenterPoint的位置,向CollesponndingCollider.transform.down方向,发射一条长度为 (车轮悬挂的最大延长距离(suspensionDistance)+车轮碰撞器的半径(radius))的线段!准确的叫线段!返回hit这个信息

                 if ( Physics.Raycast( ColliderCenterPoint, -CorrespondingCollider.transform.up, hit, CorrespondingCollider.suspensionDistance + CorrespondingCollider.radius ) )

{

//如果这条线段碰撞到了物体!

//设置车轮物体(不是车轮碰撞器)的坐标为碰撞点的坐标+车轮碰撞器的半径  transform.position = hit.point + (CorrespondingCollider.transform.up * CorrespondingCollider.radius); }else{

//如果没有碰撞,就让车轮物体,不断下落,每帧下落(车轮悬挂的最大延长距离(suspensionDistance))的  距离  transform.position = ColliderCenterPoint - (CorrespondingCollider.transform.up * CorrespondingCollider.suspensionDistance); }

           }

     //车轮物体的旋转为 车轮碰撞器所附加的空物体的变换*车轮碰撞器绕x旋转RotationValue,

     //绕Y轴旋转steerAngle,绕Z轴旋转0度

    WheelCollider.steerAngel返回车轮碰撞器绕自身Y轴旋转的角度

//乘法的顺序很重要,这里的意思,实际中是先后面的旋转,再前面的旋转!

     transform.rotation = CorrespondingCollider.transform.rotation * Quaternion.Euler( RotationValue, CorrespondingCollider.steerAngle, 0 ); // 累加RatationValue  每一帧车轮的旋转值为rpm*6*Time.deltaTime RotationValue += CorrespondingCollider.rpm * ( 360/60 ) * Time.deltaTime;

 //WheelHit是有WheelCollider返回的碰撞信息

 var CorrespondingGroundHit : WheelHit;

//返回WheelHit CorrespondingCollider.GetGroundHit( CorrespondingGroundHit ); //WheelCollider.sidewaySlip是侧轮滑动的值,如果这个值大于2.0,则复制一个粒子对象,做为车轮印!  if ( Mathf.Abs( CorrespondingGroundHit.sidewaysSlip ) > 2.0 ) {  if ( SlipPrefab ) {

   //复制 , SlipPrefab原物体,WheelCollider.point车轮其它物体碰撞点的位置,Quaternion.identity,不进行任何旋转   Instantiate( SlipPrefab, CorrespondingGroundHit.point, Quaternion.identity );  } }

  我有理解不正确的地方,麻烦指正一下……!

   下一篇总结一下,呵呵!

     

转载于:https://www.cnblogs.com/w-wfy/p/7617841.html

最新回复(0)