unity3d的动力学汽车脚本_第1页
unity3d的动力学汽车脚本_第2页
unity3d的动力学汽车脚本_第3页
unity3d的动力学汽车脚本_第4页
unity3d的动力学汽车脚本_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/*unity3d的动力学汽车脚本收藏这是从官网论坛上收集的一个汽车脚本,经过验证可以使用。由于skidmarks这个配套的脚本没有找到,所以把 skidmarks相关的语句都屏蔽了,所以很遗憾没有刹车印的效果,其他的没有改。使用方法如下: 1、把脚本直接连到汽车车身网格上,车身要有rigidbody component,要有四个轮子网格做子物体。要想有声音的话还要有audiosource component 。 2、打开 inspector,选择汽车脚本,把四个轮子连接到相对应的transform 参数上。设置 wheelradius参数为你轮子网格的大小。wheelcollider是自动生

2、成的,所以无需手动添加。这样就能保证运行了,其他的声音和灰尘可以再添加。脚本源代码如下:*/ #pragma strict /maximal corner and braking acceleration capabilities var maxcorneraccel=10.0; var maxbrakeaccel=10.0; /center of gravity height - effects tilting in corners var cogy = 0.0; /engine powerband var minrpm = 700; var maxrpm = 6000; /maximum

3、engine torque var maxtorque = 400; /automatic transmission shift points var shiftdownrpm = 2500; var shiftuprpm = 5500; /gear ratios var gearratios = -2.66, 2.66, 1.78, 1.30, 1.00; var finaldriveratio = 3.4; /a basic handling modifier: /1.0 understeer /0.0 oversteer var handlingtendency = 0.7; /grap

4、hical wheel objects var wheelfr : transform; var wheelfl : transform; var wheelbr : transform; var wheelbl : transform; /suspension setup var suspensiondistance = 0.3; var springs = 1000; var dampers = 200; var wheelradius = 0.45; /particle effect for ground dust var grounddusteffect : transform; pr

5、ivate var queryuserinput = true; private var enginerpm : float; private var steervelo = 0.0; private var brake = 0.0; private var handbrake = 0.0; private var steer = 0.0; private var motor = 0.0; /private var skidtime = 0.0; private var onground = false; private var cornerslip = 0.0; private var dr

6、iveslip = 0.0; private var wheelrpm : float; private var gear = 1; /private var skidmarks : skidmarks; private var wheels : wheeldata; private var wheely = 0.0; private var rev = 0.0; /functions to be used by external scripts /controlling the car if required /= /return a status string for the vehicl

7、e function getstatus(gui : guitext) gui.text=v=+(rigidbody.velocity.magnitude * 3.6).tostring(f1) + km/hngear= +gear+nrpm= +enginerpm.tostring(f0); /return an information string for the vehicle function getcontrolstring(gui : guitext) gui.text=use arrow keys to control the jeep,nspace for handbrake.

8、; /enable or disable user controls function setenableuserinput(enableinput) queryuserinput=enableinput; /car physics /= /some whee calculation data class wheeldata var rotation = 0.0; var coll : wheelcollider; var graphic : transform; var maxsteerangle = 0.0; var lastskidmark = -1; var powered = fal

9、se; var handbraked = false; var originalrotation : quaternion; ; function start () /setup wheels wheels=new wheeldata4; for(i=0;i1) handbrakeslip=1; totalslip=0.0; onground=false; for(w in wheels) /rotate wheel w.rotation += wheelrpm / 60.0 * -rev * 360.0 * time.fixeddeltatime; w.rotation = mathf.re

10、peat(w.rotation, 360.0); w.graphic.localrotation= quaternion.euler( w.rotation, w.maxsteerangle*steer, 0.0 ) * w.originalrotation; /check if wheel is on ground if(w.coll.isgrounded) onground=true; slip = cornerslip+(w.powered?driveslip:0.0)+(w.handbraked?handbrakeslip:0.0); totalslip += slip; var hi

11、t : wheelhit; var c : wheelcollider; c = w.coll; if(c.getgroundhit(hit) /if the wheel touches the ground, adjust graphical wheel position to reflect springs w.graphic.localposition.y-=vector3.dot(w.graphic.position-hit.point,transform.up)-w.coll.radius; /create dust on ground if appropiate if(slip0.

12、5 & hit.collider.tag=dusty) grounddusteffect.position=hit.point; grounddusteffect.particleemitter.worldvelocity=rigidbody.velocity*0.5; grounddusteffect.particleemitter.minemission=(slip-0.5)*3; grounddusteffect.particleemitter.maxemission=(slip-0.5)*3; grounddusteffect.particleemitter.emit(); /

13、and skid marks /*if(slip0.75 & skidmarks != null) w.lastskidmark=skidmarks.addskidmark(hit.point,hit.normal,(slip-0.75)*2,w.lastskidmark); else w.lastskidmark=-1; */ / else w.lastskidmark=-1; totalslip/=wheels.length; /automatically shift gears function automatictransmission() if(gear0) if(engin

14、erpmshiftuprpm&geargearratios.length-1) gear+; if(enginerpm1) gear-; /calculate engine acceleration force for current rpm and trottle function calcengine() : float /no engine when braking if(brake+handbrake0.1) motor=0.0; /if car is airborne, just rev engine if(!onground) enginerpm += (motor-0.3

15、)*25000.0*time.deltatime; enginerpm = mathf.clamp(enginerpm,minrpm,maxrpm); return 0.0; else automatictransmission(); enginerpm=wheelrpm*gearratiosgear*finaldriveratio; if(enginerpmminrpm) enginerpm=minrpm; if(enginerpmmaxrpm) /fake a basic torque curve x = (2*(enginerpm/maxrpm)-1); torquecurve = 0.

16、5*(-x*x+2); torquetoforceratio = gearratiosgear*finaldriveratio/wheelradius; return motor*maxtorque*torquecurve*torquetoforceratio; else /rpm delimiter return 0.0; /car physics /the physics of this car are really a trial-and-error based extension of /basic asteriods physics - so you will get a prett

17、y arcade-like feel. /this may or may not be what you want, for a more physical approach research /the wheel colliders function handlephysics () var velo=rigidbody.velocity; wheelrpm=velo.magnitude*60.0*0.5; rigidbody.angularvelocity=new vector3(rigidbody.angularvelocity.x,0.0,rigidbody.angularveloci

18、ty.z); dir=transform.transformdirection(vector3.forward); flatdir=vector3.normalize(new vector3(dir.x,0,dir.z); flatvelo=new vector3(velo.x,0,velo.z); rev=mathf.sign(vector3.dot(flatvelo,flatdir); /when moving backwards or standing and brake is pressed, switch to reverse if(rev0|flatvelo.sqrmagnitud

19、e0.1) gear=0; if(gear=0) /when in reverse, flip brake and gas tmp=brake; brake=motor; motor=tmp; /when moving forward or standing and gas is pressed, switch to drive if(rev0|flatvelo.sqrmagnitude0.1) gear=1; engineforce=flatdir*calcengine(); totalbrake=brake+handbrake*0.5; if(totalbrake1.0)totalbrak

20、e=1.0; brakeforce=-flatvelo.normalized*totalbrake*rigidbody.mass*maxbrakeaccel; flatdir*=flatvelo.magnitude; flatdir=quaternion.angleaxis(steer*30.0,vector3.up)*flatdir; flatdir*=rev; diff=(flatvelo-flatdir).magnitude; corneraccel=maxcorneraccel; if(corneracceldiff)corneraccel=diff; cornerforce=-(fl

21、atvelo-flatdir).normalized*corneraccel*rigidbody.mass; cornerslip=mathf.pow(corneraccel/maxcorneraccel,3); rigidbody.addforceatposition(brakeforce+engineforce+cornerforce,transform.position+transform.up*wheely); handbrakefactor=1+handbrake*4; if(rev0) handbrakefactor=1; velosteer=(15/(2*velo.magnitu

22、de+1)+1)*handbrakefactor; steergrip=(1-handlingtendency*cornerslip); if(rev*steer*steervelo0) steergrip=1; maxrotsteer=2*time.fixeddeltatime*handbrakefactor*steergrip; fvelo=velo.magnitude; velofactor=fvelo1.0?fvelo:mathf.pow(velo.magnitude,0.3); steerveloinput=rev*steer*velofactor*0.5*time.fixeddeltatime*handbrakefactor; if(velo.magnitudesteervelo) steervelo+=0.02*time.fixeddeltatime*velosteer; if(steerveloinputsteervelo) steervelo=steerveloinput; steervelo=mathf.clamp(steervelo,-maxrotsteer,maxrotsteer); transform.rotate(vector3.up*steervelo*57.295788); function fixedupdate () /query inp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论