Search Unity

Wheel Collider being edited by script but where?

Discussion in 'Scripting' started by Tyler Ridings, Mar 5, 2011.

  1. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    Ok guys.Im trying to improve on this physic script.The car uses unitys wheel colliders.When i press play the wheel models each get a wheel collider if you click that wheel collider while the game is playing you can see that there are some settings in there like sideways friction and forward friction.I wanna edit these settings but i cant find out how the script below is giving the wheel colliders their properties.

    This is part 1 of 2 parts of the script(all one script) it was so big i had to split it into 2 posts.
    Code (csharp):
    1. //maximal corner and braking acceleration capabilities
    2. var maxCornerAccel=10.0;
    3. var maxBrakeAccel=10.0;
    4.  
    5. //Mass
    6. var mass = 1500;
    7.  
    8. //coefficient of drag
    9. var drag = 0.05;
    10.  
    11. //center of gravity height - effects tilting in corners
    12. var cogY = 0.0;
    13.  
    14. //engine powerband
    15. var minRPM = 700;
    16. var maxRPM = 6000;
    17.  
    18. //maximum Engine Torque
    19. var maxTorque = 400;
    20.  
    21. //automatic transmission shift points
    22. var shiftDownRPM = 2500;
    23. var shiftUpRPM = 5500;
    24.  
    25. //gear ratios
    26. var gearRatios = [-2.66, 2.66, 1.78, 1.30, 1.00];
    27. var finalDriveRatio = 3.4;
    28.  
    29. //a basic handling modifier:
    30. //1.0 understeer
    31. //0.0 oversteer
    32. var handlingTendency = 0.5;
    33.  
    34. //graphical wheel objects
    35. var wheelFR : Transform;
    36. var wheelFL : Transform;
    37. var wheelMR : Transform;
    38. var wheelML : Transform;
    39. var wheelBR : Transform;
    40. var wheelBL : Transform;
    41.  
    42. //suspension setup
    43. var suspensionDistance = 0.3;
    44. var springs = 1000;
    45. var dampers = 200;
    46. var wheelRadius = 0.45;
    47.  
    48. //particle effect for ground dust
    49. var groundDustEffect : Transform;
    50.  
    51. private var queryUserInput = true;
    52. private var engineRPM : float;
    53. private var steerVelo = 0.0;
    54. private var brake = 0.0;
    55. private var handbrake = 0.0;
    56. private var steer = 0.0;
    57. private var motor = 0.0;
    58. private var skidTime = 0.0;
    59. private var onGround = false;
    60. private var cornerSlip = 0.0;
    61. private var driveSlip = 0.0;
    62. private var wheelRPM : float;
    63. private var gear = 1;
    64. private var skidmarks;
    65. private var wheels : WheelData[];
    66. private var wheelY = 0.0;
    67.  
    68. //Functions to be used by external scripts
    69. //controlling the car if required
    70. //===================================================================
    71.  
    72. //return a status string for the vehicle
    73. function GetStatus(gui : GUIText) {
    74.     gui.text="v="+(rigidbody.velocity.magnitude * 3.6).ToString("f1") + " km/h\ngear= "+gear+"\nrpm= "+engineRPM.ToString("f0");
    75. }
    76.  
    77. //return an information string for the vehicle
    78. function GetControlString(gui : GUIText) {
    79.     gui.text="Use arrow keys to control the jeep,\nspace for handbrake.";
    80. }
    81.  
    82. //Setup main camera to follow vehicle
    83. function SetupCamera() {
    84.     if(Camera.main.GetComponent(SmoothFollow)!=null)
    85.     {
    86.         Camera.main.GetComponent(SmoothFollow).enabled=true;
    87.         Camera.main.GetComponent(SmoothFollow).target=transform;
    88.         Camera.main.GetComponent(SmoothFollow).distance=7;
    89.         Camera.main.GetComponent(SmoothFollow).height=3;
    90.     }
    91.     Camera.main.transform.parent=null;
    92. }
    93.  
    94. //Enable or disable user controls
    95. function SetEnableUserInput(enableInput)
    96. {
    97.     queryUserInput=enableInput;
    98. }
    99.  
    100. //Car physics
    101. //===================================================================
    102.  
    103. //some whee calculation data
    104. class WheelData{
    105.     var rotation = 0.0;
    106.     var coll : WheelCollider;
    107.     var graphic : Transform;
    108.     var maxSteerAngle = 0.0;
    109.     var lastSkidMark = -1;
    110.     var powered = false;
    111.     var handbraked = false;
    112.     var originalRotation;
    113. };
    114.  
    115. function Start () {
    116.     //Destroy existing rigidbody, we don't want anyone to mess with it.
    117.     if(rigidbody)
    118.         Destroy(rigidbody);
    119.    
    120.     //setup rigidbody  
    121.     gameObject.AddComponent(Rigidbody);
    122.     rigidbody.mass = mass;
    123.     rigidbody.drag = drag;
    124.     rigidbody.angularDrag = 0.8;
    125.     rigidbody.centerOfMass.y = cogY;
    126.     rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
    127.  
    128.     //start engine noise
    129.     audio.loop = true;
    130.     audio.Play();
    131.  
    132.     //setup wheels
    133.     wheels=new WheelData[6];
    134.     for(i=0;i<6;i++)
    135.         wheels[i] = new WheelData();
    136.        
    137.     wheels[0].graphic = wheelFL;
    138.     wheels[1].graphic = wheelFR;
    139.     wheels[2].graphic = wheelML;
    140.     wheels[3].graphic = wheelMR;
    141.     wheels[4].graphic = wheelBL;
    142.     wheels[5].graphic = wheelBR;
    143.  
    144.     wheels[0].maxSteerAngle=30.0;
    145.     wheels[1].maxSteerAngle=30.0;
    146.     wheels[2].powered=true;
    147.     wheels[3].powered=true;
    148.     wheels[4].powered=true;
    149.     wheels[5].powered=true;
    150.     wheels[2].handbraked=true;
    151.     wheels[3].handbraked=true;
    152.     wheels[4].handbraked=true;
    153.     wheels[5].handbraked=true;
    154.  
    155.     for(w in wheels)
    156.     {
    157.         if(w.graphic==null)
    158.             Debug.Log("You need to assign all four wheels for the car script!");
    159.         if(!w.graphic.transform.IsChildOf(transform))  
    160.             Debug.Log("Wheels need to be children of the Object with the car script");
    161.            
    162.         w.originalRotation=w.graphic.localRotation;
    163.  
    164.         //create collider
    165.         colliderObject = new GameObject("WheelCollider");
    166.         colliderObject.transform.parent = transform;
    167.         colliderObject.transform.localPosition = w.graphic.localPosition;
    168.         w.coll = colliderObject.AddComponent(WheelCollider);
    169.         w.coll.suspensionDistance = suspensionDistance;
    170.         w.coll.suspensionSpring.spring = springs;
    171.         w.coll.suspensionSpring.damper = dampers;
    172.         //no grip, as we simulate handling ourselves
    173.         w.coll.forwardFriction.stiffness = 0;
    174.         w.coll.sidewaysFriction.stiffness = 0;
    175.         w.coll.radius = wheelRadius;
    176.     }  
    177.  
    178.     //get wheel height (height forces are applied on)
    179.     wheelY=wheels[0].graphic.localPosition.y;
    180.    
    181.     //find skidmark object
    182.     skidmarks = FindObjectOfType(typeof(Skidmarks));
    183.    
    184.     //shift to first
    185.     gear=1;
    186. }
    187.  
    188. //update wheel status
    189. function UpdateWheels()
    190. {
    191.     //calculate handbrake slip for traction gfx
    192.     handbrakeSlip=handbrake*rigidbody.velocity.magnitude*0.1;
    193.     if(handbrakeSlip>1)
    194.         handbrakeSlip=1;
    195.        
    196.     totalSlip=0.0;
    197.     onGround=false;
    198.     for(w in wheels)
    199.     {      
    200.         //rotate wheel
    201.         w.rotation += wheelRPM / 60.0 * 360.0* Time.fixedDeltaTime;
    202.         w.rotation = Mathf.Repeat(w.rotation, 360.0);      
    203.         w.graphic.localRotation= Quaternion.Euler( w.rotation, w.maxSteerAngle*steer, 0.0 ) * w.originalRotation;
    204.  
    205.         //check if wheel is on ground
    206.         if(w.coll.isGrounded)
    207.             onGround=true;
    208.            
    209.         slip = cornerSlip+(w.powered?driveSlip:0.0)+(w.handbraked?handbrakeSlip:0.0);
    210.         totalSlip += slip;
    211.        
    212.         var hit : WheelHit;
    213.         var c : WheelCollider;
    214.         c = w.coll;
    215.         if(c.GetGroundHit(hit))
    216.         {
    217.             //if the wheel touches the ground, adjust graphical wheel position to reflect springs
    218.             w.graphic.localPosition.y-=Vector3.Dot(w.graphic.position-hit.point,transform.up)-w.coll.radius;
    219.            
    220.  
    221.  
     
  2. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    Part 2

    Code (csharp):
    1.             //create dust on ground if appropiate
    2.             if(slip>0.5  hit.collider.tag=="Dusty")
    3.             {
    4.                 groundDustEffect.position=hit.point;
    5.                 groundDustEffect.particleEmitter.worldVelocity=rigidbody.velocity*0.5;
    6.                 groundDustEffect.particleEmitter.minEmission=(slip-0.5)*3;
    7.                 groundDustEffect.particleEmitter.maxEmission=(slip-0.5)*3;
    8.                 groundDustEffect.particleEmitter.Emit();                               
    9.             }
    10.            
    11.             //and skid marks               
    12.             if(slip>0.75  skidmarks != null)
    13.                 w.lastSkidMark=skidmarks.AddSkidMark(hit.point,hit.normal,(slip-0.75)*2,w.lastSkidMark);
    14.             else
    15.                 w.lastSkidMark=-1;
    16.         }
    17.         else w.lastSkidMark=-1;
    18.     }
    19.     totalSlip/=wheels.length;
    20. }
    21.  
    22. //Automatically shift gears
    23. function AutomaticTransmission()
    24. {
    25.     if(gear>0)
    26.     {
    27.         if(engineRPM>shiftUpRPM&gear<gearRatios.length-1)
    28.             gear++;
    29.         if(engineRPM<shiftDownRPM&gear>1)
    30.             gear--;
    31.     }
    32. }
    33.  
    34. //Calculate engine acceleration force for current RPM and trottle
    35. function CalcEngine() : float
    36. {
    37.     //no engine when braking
    38.     if(brake+handbrake>0.1)
    39.         motor=0.0;
    40.    
    41.     //if car is airborne, just rev engine
    42.     if(!onGround)
    43.     {
    44.         engineRPM += (motor-0.3)*25000.0*Time.deltaTime;
    45.         engineRPM = Mathf.Clamp(engineRPM,minRPM,maxRPM);
    46.         return 0.0;
    47.     }
    48.     else
    49.     {
    50.         AutomaticTransmission();
    51.         engineRPM=wheelRPM*gearRatios[gear]*finalDriveRatio;
    52.         if(engineRPM<minRPM)
    53.             engineRPM=minRPM;
    54.         if(engineRPM<maxRPM)
    55.         {
    56.             //fake a basic torque curve
    57.             x = (2*(engineRPM/maxRPM)-1);
    58.             torqueCurve = 0.5*(-x*x+2);
    59.             torqueToForceRatio = gearRatios[gear]*finalDriveRatio/wheelRadius;
    60.             return motor*maxTorque*torqueCurve*torqueToForceRatio;
    61.         }
    62.         else
    63.             //rpm delimiter
    64.             return 0.0;
    65.     }
    66. }
    67.  
    68. //Car physics
    69. //The physics of this car are really a trial-and-error based extension of
    70. //basic "Asteriods" physics -- so you will get a pretty arcade-like feel.
    71. //This may or may not be what you want, for a more physical approach research
    72. //the wheel colliders
    73. function HandlePhysics () {
    74.     var velo=rigidbody.velocity;
    75.     wheelRPM=velo.magnitude*60.0*0.5;
    76.  
    77.     rigidbody.angularVelocity=new Vector3(rigidbody.angularVelocity.x,0.0,rigidbody.angularVelocity.z);
    78.     dir=transform.TransformDirection(Vector3.forward);
    79.     flatDir=Vector3.Normalize(new Vector3(dir.x,0,dir.z));
    80.     flatVelo=new Vector3(velo.x,0,velo.z);
    81.     rev=Mathf.Sign(Vector3.Dot(flatVelo,flatDir));
    82.     //when moving backwards or standing and brake is pressed, switch to reverse
    83.     if((rev<0||flatVelo.sqrMagnitude<0.5)&brake>0.1)
    84.         gear=0;
    85.     if(gear==0)
    86.     {  
    87.         //when in reverse, flip brake and gas
    88.         tmp=brake;
    89.         brake=motor;
    90.         motor=tmp;
    91.        
    92.         //when moving forward or standing and gas is pressed, switch to drive
    93.         if((rev>0||flatVelo.sqrMagnitude<0.5)&brake>0.1)
    94.             gear=1;
    95.     }
    96.     engineForce=flatDir*CalcEngine();
    97.     totalbrake=brake+handbrake*0.5;
    98.     if(totalbrake>1.0)totalbrake=1.0;
    99.     brakeForce=-flatVelo.normalized*totalbrake*rigidbody.mass*maxBrakeAccel;
    100.  
    101.     flatDir*=flatVelo.magnitude;
    102.     flatDir=Quaternion.AngleAxis(steer*30.0,Vector3.up)*flatDir;
    103.     flatDir*=rev;
    104.     diff=(flatVelo-flatDir).magnitude;
    105.     cornerAccel=maxCornerAccel;
    106.     if(cornerAccel>diff)cornerAccel=diff;
    107.     cornerForce=-(flatVelo-flatDir).normalized*cornerAccel*rigidbody.mass;
    108.     cornerSlip=Mathf.Pow(cornerAccel/maxCornerAccel,3);
    109.    
    110.     rigidbody.AddForceAtPosition(brakeForce+engineForce+cornerForce,transform.position+transform.up*wheelY);
    111.    
    112.     handbrakeFactor=1+handbrake*4;
    113.     if(rev<0)
    114.         handbrakeFactor=1;
    115.     veloSteer=((15/(2*velo.magnitude+1))+1)*handbrakeFactor;
    116.     steerGrip=(1-handlingTendency*cornerSlip);
    117.     if(rev*steer*steerVelo<0)
    118.         steerGrip=1;
    119.     maxRotSteer=2*Time.fixedDeltaTime*handbrakeFactor*steerGrip;
    120.     fVelo=velo.magnitude;
    121.     veloFactor=fVelo<1.0?fVelo:Mathf.Pow(velo.magnitude,0.3);
    122.     steerVeloInput=rev*steer*veloFactor*0.5*Time.fixedDeltaTime*handbrakeFactor;
    123.     if(velo.magnitude<0.1)
    124.         steerVeloInput=0;
    125.     if(steerVeloInput>steerVelo)
    126.     {
    127.         steerVelo+=0.02*Time.fixedDeltaTime*veloSteer;
    128.         if(steerVeloInput<steerVelo)
    129.             steerVelo=steerVeloInput;
    130.     }
    131.     else
    132.     {
    133.         steerVelo-=0.02*Time.fixedDeltaTime*veloSteer;
    134.         if(steerVeloInput>steerVelo)
    135.             steerVelo=steerVeloInput;
    136.     }
    137.     steerVelo=Mathf.Clamp(steerVelo,-maxRotSteer,maxRotSteer); 
    138.     transform.Rotate(Vector3.up*steerVelo*57.295788);
    139. }
    140.  
    141. function FixedUpdate () {
    142.     //query input axes if necessarry
    143.     if(queryUserInput)
    144.     {
    145.         brake = Mathf.Clamp01(-Input.GetAxis("Vertical"));
    146.         handbrake = Input.GetButton("Jump")?1.0:0.0;
    147.         steer = Input.GetAxis("Horizontal");
    148.         motor = Mathf.Clamp01(Input.GetAxis("Vertical"));
    149.     }
    150.     else
    151.     {
    152.         motor = 0;
    153.         steer = 0;
    154.         brake = 0;
    155.         handbrake = 0;
    156.     }
    157.  
    158.  
    159.     //if car is on ground calculate handling, otherwise just rev the engine
    160.     if(onGround)
    161.         HandlePhysics();
    162.     else
    163.         CalcEngine();  
    164.        
    165.     //wheel GFX
    166.     UpdateWheels();
    167.  
    168.     //engine sounds
    169.     audio.pitch=0.5+0.2*motor+0.8*engineRPM/maxRPM;
    170.     audio.volume=0.5+0.8*motor+0.2*engineRPM/maxRPM;
    171. }
    172.  
    173. //Called by DamageReceiver if boat destroyed
    174. function Detonate()
    175. {
    176.     //destroy wheels
    177.     for( w in wheels )
    178.         w.coll.gameObject.active=false;
    179.  
    180.     //Mark object no longer a target for homing missiles.
    181.     if(tag=="MissileTarget")
    182.         tag="";
    183.  
    184.     //no more car physics
    185.     enabled=false;
    186. }
    187.  
    188. @script RequireComponent (AudioSource)
     
  3. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    Well i couldnt find how the script was adding the properties so i just coded it in my self so i could edit the wheel colliders from the script.I got it fixed.
     
  4. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    In the first post, where the comment says

    Code (csharp):
    1. // Setup Wheels
    would be my guess.