Advertisement
Guest User

Wird unity code

a guest
Jul 6th, 2014
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.00 KB | None | 0 0
  1. private var wheelRadius : float = 0.4;
  2. var suspensionRange : float = 0.1;
  3. var suspensionDamper : float = 50;
  4. var suspensionSpringFront : float = 18500;
  5. var suspensionSpringRear : float = 9000;
  6.  
  7. public var brakeLights : Material;
  8.  
  9. var dragMultiplier : Vector3 = new Vector3(2, 5, 1);
  10.  
  11. var throttle : float = 0;
  12. private var steer : float = 0;
  13. private var handbrake : boolean = false;
  14.  
  15. var centerOfMass : Transform;
  16.  
  17. var frontWheels : Transform[];
  18. var rearWheels : Transform[];
  19.  
  20. private var wheels : Wheel[];
  21. wheels = new Wheel[frontWheels.Length + rearWheels.Length];
  22.  
  23. private var wfc : WheelFrictionCurve;
  24.  
  25. var topSpeed : float = 160;
  26. var numberOfGears : int = 5;
  27.  
  28. var maximumTurn : int = 15;
  29. var minimumTurn : int = 10;
  30.  
  31. var resetTime : float = 5.0;
  32. private var resetTimer : float = 0.0;
  33.  
  34. private var engineForceValues : float[];
  35. private var gearSpeeds : float[];
  36.  
  37. private var currentGear : int;
  38. private var currentEnginePower : float = 0.0;
  39.  
  40. private var handbrakeXDragFactor : float = 0.5;
  41. private var initialDragMultiplierX : float = 10.0;
  42. private var handbrakeTime : float = 0.0;
  43. private var handbrakeTimer : float = 1.0;
  44.  
  45. private var skidmarks : Skidmarks = null;
  46. private var skidSmoke : ParticleEmitter = null;
  47. var skidmarkTime : float[];
  48.  
  49. private var sound : SoundController = null;
  50. sound = transform.GetComponent(SoundController);
  51.  
  52. private var canSteer : boolean;
  53. private var canDrive : boolean;
  54.  
  55. class Wheel
  56. {
  57.     var collider : WheelCollider;
  58.     var wheelGraphic : Transform;
  59.     var tireGraphic : Transform;
  60.     var driveWheel : boolean = false;
  61.     var steerWheel : boolean = false;
  62.     var lastSkidmark : int = -1;
  63.     var lastEmitPosition : Vector3 = Vector3.zero;
  64.     var lastEmitTime : float = Time.time;
  65.     var wheelVelo : Vector3 = Vector3.zero;
  66.     var groundSpeed : Vector3 = Vector3.zero;
  67. }
  68.  
  69. function Start()
  70. {  
  71.     // Measuring 1 - 60
  72.     accelerationTimer = Time.time;
  73.    
  74.     SetupWheelColliders();
  75.    
  76.     SetupCenterOfMass();
  77.    
  78.     topSpeed = Convert_Miles_Per_Hour_To_Meters_Per_Second(topSpeed);
  79.    
  80.     SetupGears();
  81.    
  82.     SetUpSkidmarks();
  83.    
  84.     initialDragMultiplierX = dragMultiplier.x;
  85. }
  86.  
  87. function Update()
  88. {      
  89.     var relativeVelocity : Vector3 = transform.InverseTransformDirection(rigidbody.velocity);
  90.    
  91.     GetInput();
  92.    
  93.     Check_If_Car_Is_Flipped();
  94.    
  95.     UpdateWheelGraphics(relativeVelocity);
  96.    
  97.     UpdateGear(relativeVelocity);
  98. }
  99.  
  100. function FixedUpdate()
  101. {  
  102.     // The rigidbody velocity is always given in world space, but in order to work in local space of the car model we need to transform it first.
  103.     var relativeVelocity : Vector3 = transform.InverseTransformDirection(rigidbody.velocity);
  104.    
  105.     CalculateState();  
  106.    
  107.     UpdateFriction(relativeVelocity);
  108.    
  109.     UpdateDrag(relativeVelocity);
  110.    
  111.     CalculateEnginePower(relativeVelocity);
  112.    
  113.     ApplyThrottle(canDrive, relativeVelocity);
  114.    
  115.     ApplySteering(canSteer, relativeVelocity);
  116. }
  117.  
  118. /**************************************************/
  119. /* Functions called from Start()                  */
  120. /**************************************************/
  121.  
  122. function SetupWheelColliders()
  123. {
  124.     SetupWheelFrictionCurve();
  125.        
  126.     var wheelCount : int = 0;
  127.    
  128.     for (var t : Transform in frontWheels)
  129.     {
  130.         wheels[wheelCount] = SetupWheel(t, true);
  131.         wheelCount++;
  132.     }
  133.    
  134.     for (var t : Transform in rearWheels)
  135.     {
  136.         wheels[wheelCount] = SetupWheel(t, false);
  137.         wheelCount++;
  138.     }
  139. }
  140.  
  141. function SetupWheelFrictionCurve()
  142. {
  143.     wfc = new WheelFrictionCurve();
  144.     wfc.extremumSlip = 1;
  145.     wfc.extremumValue = 50;
  146.     wfc.asymptoteSlip = 2;
  147.     wfc.asymptoteValue = 25;
  148.     wfc.stiffness = 1;
  149. }
  150.  
  151. function SetupWheel(wheelTransform : Transform, isFrontWheel : boolean)
  152. {
  153.     var go : GameObject = new GameObject(wheelTransform.name + " Collider");
  154.     go.transform.position = wheelTransform.position;
  155.     go.transform.parent = transform;
  156.     go.transform.rotation = wheelTransform.rotation;
  157.        
  158.     var wc : WheelCollider = go.AddComponent(typeof(WheelCollider)) as WheelCollider;
  159.     wc.suspensionDistance = suspensionRange;
  160.     var js : JointSpring = wc.suspensionSpring;
  161.    
  162.     if (isFrontWheel)
  163.         js.spring = suspensionSpringFront;
  164.     else
  165.         js.spring = suspensionSpringRear;
  166.        
  167.     js.damper = suspensionDamper;
  168.     wc.suspensionSpring = js;
  169.        
  170.     wheel = new Wheel();
  171.     wheel.collider = wc;
  172.     wc.sidewaysFriction = wfc;
  173.     wheel.wheelGraphic = wheelTransform;
  174.     wheel.tireGraphic = wheelTransform.GetComponentsInChildren(Transform)[1];
  175.    
  176.     wheelRadius = wheel.tireGraphic.renderer.bounds.size.y / 2;
  177.     wheel.collider.radius = wheelRadius;
  178.    
  179.     if (isFrontWheel)
  180.     {
  181.         wheel.steerWheel = true;
  182.        
  183.         go = new GameObject(wheelTransform.name + " Steer Column");
  184.         go.transform.position = wheelTransform.position;
  185.         go.transform.rotation = wheelTransform.rotation;
  186.         go.transform.parent = transform;
  187.         wheelTransform.parent = go.transform;
  188.     }
  189.     else
  190.         wheel.driveWheel = true;
  191.        
  192.     return wheel;
  193. }
  194.  
  195. function SetupCenterOfMass()
  196. {
  197.     if(centerOfMass != null)
  198.         rigidbody.centerOfMass = centerOfMass.localPosition;
  199. }
  200.  
  201. function SetupGears()
  202. {
  203.     engineForceValues = new float[numberOfGears];
  204.     gearSpeeds = new float[numberOfGears];
  205.    
  206.     var tempTopSpeed : float = topSpeed;
  207.        
  208.     for(var i = 0; i < numberOfGears; i++)
  209.     {
  210.         if(i > 0)
  211.             gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1];
  212.         else
  213.             gearSpeeds[i] = tempTopSpeed / 4;
  214.        
  215.         tempTopSpeed -= tempTopSpeed / 4;
  216.     }
  217.    
  218.     var engineFactor : float = topSpeed / gearSpeeds[gearSpeeds.Length - 1];
  219.    
  220.     for(i = 0; i < numberOfGears; i++)
  221.     {
  222.         var maxLinearDrag : float = gearSpeeds[i] * gearSpeeds[i];// * dragMultiplier.z;
  223.         engineForceValues[i] = maxLinearDrag * engineFactor;
  224.     }
  225. }
  226.  
  227. function SetUpSkidmarks()
  228. {
  229.     if(FindObjectOfType(Skidmarks))
  230.     {
  231.         skidmarks = FindObjectOfType(Skidmarks);
  232.         skidSmoke = skidmarks.GetComponentInChildren(ParticleEmitter);
  233.     }
  234.     else
  235.         Debug.Log("No skidmarks object found. Skidmarks will not be drawn");
  236.        
  237.     skidmarkTime = new float[4];
  238.     for (var f : float in skidmarkTime)
  239.         f = 0.0;
  240. }
  241.  
  242. /**************************************************/
  243. /* Functions called from Update()                 */
  244. /**************************************************/
  245.  
  246. function GetInput()
  247. {
  248.     throttle = Input.GetAxis("Vertical");
  249.     steer = Input.GetAxis("Horizontal");
  250.    
  251.     if(throttle < 0.0)
  252.         brakeLights.SetFloat("_Intensity", Mathf.Abs(throttle));
  253.     else
  254.         brakeLights.SetFloat("_Intensity", 0.0);
  255.    
  256.     CheckHandbrake();
  257. }
  258.  
  259. function CheckHandbrake()
  260. {
  261.     if(Input.GetKey("space"))
  262.     {
  263.         if(!handbrake)
  264.         {
  265.             handbrake = true;
  266.             handbrakeTime = Time.time;
  267.             dragMultiplier.x = initialDragMultiplierX * handbrakeXDragFactor;
  268.         }
  269.     }
  270.     else if(handbrake)
  271.     {
  272.         handbrake = false;
  273.         StartCoroutine(StopHandbraking(Mathf.Min(5, Time.time - handbrakeTime)));
  274.     }
  275. }
  276.  
  277. function StopHandbraking(seconds : float)
  278. {
  279.     var diff : float = initialDragMultiplierX - dragMultiplier.x;
  280.     handbrakeTimer = 1;
  281.    
  282.     // Get the x value of the dragMultiplier back to its initial value in the specified time.
  283.     while(dragMultiplier.x < initialDragMultiplierX && !handbrake)
  284.     {
  285.         dragMultiplier.x += diff * (Time.deltaTime / seconds);
  286.         handbrakeTimer -= Time.deltaTime / seconds;
  287.         yield;
  288.     }
  289.    
  290.     dragMultiplier.x = initialDragMultiplierX;
  291.     handbrakeTimer = 0;
  292. }
  293.  
  294. function Check_If_Car_Is_Flipped()
  295. {
  296.     if(transform.localEulerAngles.z > 80 && transform.localEulerAngles.z < 280)
  297.         resetTimer += Time.deltaTime;
  298.     else
  299.         resetTimer = 0;
  300.    
  301.     if(resetTimer > resetTime)
  302.         FlipCar();
  303. }
  304.  
  305. function FlipCar()
  306. {
  307.     transform.rotation = Quaternion.LookRotation(transform.forward);
  308.     transform.position += Vector3.up * 0.5;
  309.     rigidbody.velocity = Vector3.zero;
  310.     rigidbody.angularVelocity = Vector3.zero;
  311.     resetTimer = 0;
  312.     currentEnginePower = 0;
  313. }
  314.  
  315. var wheelCount : float;
  316. function UpdateWheelGraphics(relativeVelocity : Vector3)
  317. {
  318.     wheelCount = -1;
  319.    
  320.     for(var w : Wheel in wheels)
  321.     {
  322.         wheelCount++;
  323.         var wheel : WheelCollider = w.collider;
  324.         var wh : WheelHit = new WheelHit();
  325.        
  326.         // First we get the velocity at the point where the wheel meets the ground, if the wheel is touching the ground
  327.         if(wheel.GetGroundHit(wh))
  328.         {
  329.             w.wheelGraphic.localPosition = wheel.transform.up * (wheelRadius + wheel.transform.InverseTransformPoint(wh.point).y);
  330.             w.wheelVelo = rigidbody.GetPointVelocity(wh.point);
  331.             w.groundSpeed = w.wheelGraphic.InverseTransformDirection(w.wheelVelo);
  332.            
  333.             // Code to handle skidmark drawing. Not covered in the tutorial
  334.             if(skidmarks)
  335.             {
  336.                 if(skidmarkTime[wheelCount] < 0.02 && w.lastSkidmark != -1)
  337.                 {
  338.                     skidmarkTime[wheelCount] += Time.deltaTime;
  339.                 }
  340.                 else
  341.                 {
  342.                     var dt : float = skidmarkTime[wheelCount] == 0.0 ? Time.deltaTime : skidmarkTime[wheelCount];
  343.                     skidmarkTime[wheelCount] = 0.0;
  344.  
  345.                     var handbrakeSkidding : float = handbrake && w.driveWheel ? w.wheelVelo.magnitude * 0.3 : 0;
  346.                     var skidGroundSpeed = Mathf.Abs(w.groundSpeed.x) - 15;
  347.                     if(skidGroundSpeed > 0 || handbrakeSkidding > 0)
  348.                     {
  349.                         var staticVel : Vector3 = transform.TransformDirection(skidSmoke.localVelocity) + skidSmoke.worldVelocity;
  350.                         if(w.lastSkidmark != -1)
  351.                         {
  352.                             var emission : float = UnityEngine.Random.Range(skidSmoke.minEmission, skidSmoke.maxEmission);
  353.                             var lastParticleCount : float = w.lastEmitTime * emission;
  354.                             var currentParticleCount : float = Time.time * emission;
  355.                             var noOfParticles : int = Mathf.CeilToInt(currentParticleCount) - Mathf.CeilToInt(lastParticleCount);
  356.                             var lastParticle : int = Mathf.CeilToInt(lastParticleCount);
  357.                            
  358.                             for(var i = 0; i <= noOfParticles; i++)
  359.                             {
  360.                                 var particleTime : float = Mathf.InverseLerp(lastParticleCount, currentParticleCount, lastParticle + i);
  361.                                 skidSmoke.Emit( Vector3.Lerp(w.lastEmitPosition, wh.point, particleTime) + new Vector3(Random.Range(-0.1, 0.1), Random.Range(-0.1, 0.1), Random.Range(-0.1, 0.1)), staticVel + (w.wheelVelo * 0.05), Random.Range(skidSmoke.minSize, skidSmoke.maxSize) * Mathf.Clamp(skidGroundSpeed * 0.1,0.5,1), Random.Range(skidSmoke.minEnergy, skidSmoke.maxEnergy), Color.white);
  362.                             }
  363.                         }
  364.                         else
  365.                         {
  366.                             skidSmoke.Emit( wh.point + new Vector3(Random.Range(-0.1, 0.1), Random.Range(-0.1, 0.1), Random.Range(-0.1, 0.1)), staticVel + (w.wheelVelo * 0.05), Random.Range(skidSmoke.minSize, skidSmoke.maxSize) * Mathf.Clamp(skidGroundSpeed * 0.1,0.5,1), Random.Range(skidSmoke.minEnergy, skidSmoke.maxEnergy), Color.white);
  367.                         }
  368.                    
  369.                         w.lastEmitPosition = wh.point;
  370.                         w.lastEmitTime = Time.time;
  371.                    
  372.                         w.lastSkidmark = skidmarks.AddSkidMark(wh.point + rigidbody.velocity * dt, wh.normal, (skidGroundSpeed * 0.1 + handbrakeSkidding) * Mathf.Clamp01(wh.force / wheel.suspensionSpring.spring), w.lastSkidmark);
  373.                         sound.Skid(true, Mathf.Clamp01(skidGroundSpeed * 0.1));
  374.                     }
  375.                     else
  376.                     {
  377.                         w.lastSkidmark = -1;
  378.                         sound.Skid(false, 0);
  379.                     }
  380.                 }
  381.             }
  382.         }
  383.         else
  384.         {
  385.             // If the wheel is not touching the ground we set the position of the wheel graphics to
  386.             // the wheel's transform position + the range of the suspension.
  387.             w.wheelGraphic.position = wheel.transform.position + (-wheel.transform.up * suspensionRange);
  388.             if(w.steerWheel)
  389.                 w.wheelVelo *= 0.9;
  390.             else
  391.                 w.wheelVelo *= 0.9 * (1 - throttle);
  392.            
  393.             if(skidmarks)
  394.             {
  395.                 w.lastSkidmark = -1;
  396.                 sound.Skid(false, 0);
  397.             }
  398.         }
  399.         // If the wheel is a steer wheel we apply two rotations:
  400.         // *Rotation around the Steer Column (visualizes the steer direction)
  401.         // *Rotation that visualizes the speed
  402.         if(w.steerWheel)
  403.         {
  404.             var ea : Vector3 = w.wheelGraphic.parent.localEulerAngles;
  405.             ea.y = steer * maximumTurn;
  406.             w.wheelGraphic.parent.localEulerAngles = ea;
  407.             w.tireGraphic.Rotate(Vector3.right * (w.groundSpeed.z / wheelRadius) * Time.deltaTime * Mathf.Rad2Deg);
  408.         }
  409.         else if(!handbrake && w.driveWheel)
  410.         {
  411.             // If the wheel is a drive wheel it only gets the rotation that visualizes speed.
  412.             // If we are hand braking we don't rotate it.
  413.             w.tireGraphic.Rotate(Vector3.right * (w.groundSpeed.z / wheelRadius) * Time.deltaTime * Mathf.Rad2Deg);
  414.         }
  415.     }
  416. }
  417.  
  418. function UpdateGear(relativeVelocity : Vector3)
  419. {
  420.     currentGear = 0;
  421.     for(var i = 0; i < numberOfGears - 1; i++)
  422.     {
  423.         if(relativeVelocity.z > gearSpeeds[i])
  424.             currentGear = i + 1;
  425.     }
  426. }
  427.  
  428. /**************************************************/
  429. /* Functions called from FixedUpdate()            */
  430. /**************************************************/
  431.  
  432. function UpdateDrag(relativeVelocity : Vector3)
  433. {
  434.     var relativeDrag : Vector3 = new Vector3(   -relativeVelocity.x * Mathf.Abs(relativeVelocity.x),
  435.                                                 -relativeVelocity.y * Mathf.Abs(relativeVelocity.y),
  436.                                                 -relativeVelocity.z * Mathf.Abs(relativeVelocity.z) );
  437.    
  438.     var drag = Vector3.Scale(dragMultiplier, relativeDrag);
  439.        
  440.     if(initialDragMultiplierX > dragMultiplier.x) // Handbrake code
  441.     {          
  442.         drag.x /= (relativeVelocity.magnitude / (topSpeed / ( 1 + 2 * handbrakeXDragFactor ) ) );
  443.         drag.z *= (1 + Mathf.Abs(Vector3.Dot(rigidbody.velocity.normalized, transform.forward)));
  444.         drag += rigidbody.velocity * Mathf.Clamp01(rigidbody.velocity.magnitude / topSpeed);
  445.     }
  446.     else // No handbrake
  447.     {
  448.         drag.x *= topSpeed / relativeVelocity.magnitude;
  449.     }
  450.    
  451.     if(Mathf.Abs(relativeVelocity.x) < 5 && !handbrake)
  452.         drag.x = -relativeVelocity.x * dragMultiplier.x;
  453.        
  454.  
  455.     rigidbody.AddForce(transform.TransformDirection(drag) * rigidbody.mass * Time.deltaTime);
  456. }
  457.  
  458. function UpdateFriction(relativeVelocity : Vector3)
  459. {
  460.     var sqrVel : float = relativeVelocity.x * relativeVelocity.x;
  461.    
  462.     // Add extra sideways friction based on the car's turning velocity to avoid slipping
  463.     wfc.extremumValue = Mathf.Clamp(300 - sqrVel, 0, 300);
  464.     wfc.asymptoteValue = Mathf.Clamp(150 - (sqrVel / 2), 0, 150);
  465.        
  466.     for(var w : Wheel in wheels)
  467.     {
  468.         w.collider.sidewaysFriction = wfc;
  469.         w.collider.forwardFriction = wfc;
  470.     }
  471. }
  472.  
  473. function CalculateEnginePower(relativeVelocity : Vector3)
  474. {
  475.     if(throttle == 0)
  476.     {
  477.         currentEnginePower -= Time.deltaTime * 200;
  478.     }
  479.     else if( HaveTheSameSign(relativeVelocity.z, throttle) )
  480.     {
  481.         normPower = (currentEnginePower / engineForceValues[engineForceValues.Length - 1]) * 2;
  482.         currentEnginePower += Time.deltaTime * 200 * EvaluateNormPower(normPower);
  483.     }
  484.     else
  485.     {
  486.         currentEnginePower -= Time.deltaTime * 300;
  487.     }
  488.    
  489.     if(currentGear == 0)
  490.         currentEnginePower = Mathf.Clamp(currentEnginePower, 0, engineForceValues[0]);
  491.     else
  492.         currentEnginePower = Mathf.Clamp(currentEnginePower, engineForceValues[currentGear - 1], engineForceValues[currentGear]);
  493. }
  494.  
  495. function CalculateState()
  496. {
  497.     canDrive = false;
  498.     canSteer = false;
  499.    
  500.     for(var w : Wheel in wheels)
  501.     {
  502.         if(w.collider.isGrounded)
  503.         {
  504.             if(w.steerWheel)
  505.                 canSteer = true;
  506.             if(w.driveWheel)
  507.                 canDrive = true;
  508.         }
  509.     }
  510. }
  511.  
  512. function ApplyThrottle(canDrive : boolean, relativeVelocity : Vector3)
  513. {
  514.     if(canDrive)
  515.     {
  516.         var throttleForce : float = 0;
  517.         var brakeForce : float = 0;
  518.        
  519.         if (HaveTheSameSign(relativeVelocity.z, throttle))
  520.         {
  521.             if (!handbrake)
  522.                 throttleForce = Mathf.Sign(throttle) * currentEnginePower * rigidbody.mass;
  523.         }
  524.         else
  525.             brakeForce = Mathf.Sign(throttle) * engineForceValues[0] * rigidbody.mass;
  526.        
  527.         rigidbody.AddForce(transform.forward * Time.deltaTime * (throttleForce + brakeForce));
  528.     }
  529. }
  530.  
  531. function ApplySteering(canSteer : boolean, relativeVelocity : Vector3)
  532. {
  533.     if(canSteer)
  534.     {
  535.         var turnRadius : float = 3.0 / Mathf.Sin((90 - (steer * 30)) * Mathf.Deg2Rad);
  536.         var minMaxTurn : float = EvaluateSpeedToTurn(rigidbody.velocity.magnitude);
  537.         var turnSpeed : float = Mathf.Clamp(relativeVelocity.z / turnRadius, -minMaxTurn / 10, minMaxTurn / 10);
  538.        
  539.         transform.RotateAround( transform.position + transform.right * turnRadius * steer,
  540.                                 transform.up,
  541.                                 turnSpeed * Mathf.Rad2Deg * Time.deltaTime * steer);
  542.        
  543.         var debugStartPoint = transform.position + transform.right * turnRadius * steer;
  544.         var debugEndPoint = debugStartPoint + Vector3.up * 5;
  545.        
  546.         Debug.DrawLine(debugStartPoint, debugEndPoint, Color.red);
  547.        
  548.         if(initialDragMultiplierX > dragMultiplier.x) // Handbrake
  549.         {
  550.             var rotationDirection : float = Mathf.Sign(steer); // rotationDirection is -1 or 1 by default, depending on steering
  551.             if(steer == 0)
  552.             {
  553.                 if(rigidbody.angularVelocity.y < 1) // If we are not steering and we are handbraking and not rotating fast, we apply a random rotationDirection
  554.                     rotationDirection = Random.Range(-1.0, 1.0);
  555.                 else
  556.                     rotationDirection = rigidbody.angularVelocity.y; // If we are rotating fast we are applying that rotation to the car
  557.             }
  558.             // -- Finally we apply this rotation around a point between the cars front wheels.
  559.             transform.RotateAround( transform.TransformPoint( ( frontWheels[0].localPosition + frontWheels[1].localPosition) * 0.5),
  560.                                                                 transform.up,
  561.                                                                 rigidbody.velocity.magnitude * Mathf.Clamp01(1 - rigidbody.velocity.magnitude / topSpeed) * rotationDirection * Time.deltaTime * 2);
  562.         }
  563.     }
  564. }
  565.  
  566. /**************************************************/
  567. /*               Utility Functions                */
  568. /**************************************************/
  569.  
  570. function Convert_Miles_Per_Hour_To_Meters_Per_Second(value : float) : float
  571. {
  572.     return value * 0.44704;
  573. }
  574.  
  575. function Convert_Meters_Per_Second_To_Miles_Per_Hour(value : float) : float
  576. {
  577.     return value * 2.23693629; 
  578. }
  579.  
  580. function HaveTheSameSign(first : float, second : float) : boolean
  581. {
  582.     if (Mathf.Sign(first) == Mathf.Sign(second))
  583.         return true;
  584.     else
  585.         return false;
  586. }
  587.  
  588. function EvaluateSpeedToTurn(speed : float)
  589. {
  590.     if(speed > topSpeed / 2)
  591.         return minimumTurn;
  592.    
  593.     var speedIndex : float = 1 - (speed / (topSpeed / 2));
  594.     return minimumTurn + speedIndex * (maximumTurn - minimumTurn);
  595. }
  596.  
  597. function EvaluateNormPower(normPower : float)
  598. {
  599.     if(normPower < 1)
  600.         return 10 - normPower * 9;
  601.     else
  602.         return 1.9 - normPower * 0.9;
  603. }
  604.  
  605. function GetGearState()
  606. {
  607.     var relativeVelocity : Vector3 = transform.InverseTransformDirection(rigidbody.velocity);
  608.     var lowLimit : float = (currentGear == 0 ? 0 : gearSpeeds[currentGear-1]);
  609.     return (relativeVelocity.z - lowLimit) / (gearSpeeds[currentGear - lowLimit]) * (1 - currentGear * 0.1) + currentGear * 0.1;
  610. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement