Advertisement
Guest User

CarScript

a guest
Jul 27th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public var PlayerCar : Transform;
  2. public var Grounded : boolean = false;
  3. public var CarGravity : float = -20;
  4. private var accel : Vector3;
  5. public var throttle : float;
  6. private var deadZone : float = .001;
  7. private var myRight : Vector3;
  8. private var velo : Vector3;
  9. private var flatVelo : Vector3;
  10. private var relativeVelocity : Vector3;
  11. private var dir: Vector3;
  12. private var flatDir : Vector3;
  13. private var carUp : Vector3;
  14. private var carTransform : Transform;
  15. private var carRigidbody : Rigidbody;
  16. private var engineForce : Vector3;
  17.  
  18. private var turnVec : Vector3;
  19. private var imp : Vector3;
  20. private var rev : float;
  21. private var actualTurn : float;
  22. private var carMass : float;
  23. private var wheelTransform : Transform[] = new Transform[4];
  24. public var actualGrip : float;
  25. public var horizontal : float;
  26. private var maxSpeedToTurn : float = .2;
  27.  
  28. public var frontLeftWheel : Transform;
  29. public var frontRightWheel : Transform;
  30. public var rearLeftWheel : Transform;
  31. public var rearRightWheel : Transform;
  32.  
  33. public var LFWheelTransform : Transform;
  34. public var RFWheelTransform : Transform;
  35.  
  36. public var power : float = 300;
  37. public var maxSpeed : float = 50;
  38. public var carGrip : float = 70;
  39. public var turnSpeed : float = 3.0;
  40.  
  41. private var slideSpeed : float;
  42. public var mySpeed : float;
  43.  
  44. private var carRight : Vector3;
  45. private var carFwd : Vector3;
  46. private var tempVEC : Vector3;
  47.  
  48. private var hit : RaycastHit;
  49.  
  50. function Start()
  51. {
  52.     Initialize();
  53.     Physics.gravity.y = CarGravity;
  54. }
  55.  
  56. function Initialize()
  57. {
  58.         carTransform = transform;
  59.         carRigidbody = rigidbody;
  60.         carUp = carTransform.up;
  61.         carMass = rigidbody.mass;
  62.         carFwd = Vector3.forward;
  63.         carRight = Vector3.up;
  64.         setUpWheels();
  65.         carRigidbody.centerOfMass = Vector3(0,-0.7,.35);
  66. }
  67.  
  68. function OnCollisionEnter(collision : Collision)
  69. {
  70.     if(collision.gameObject.tag == "Terrain")
  71.     {
  72.         Grounded = true;
  73.     }
  74. }
  75.  
  76. function OnCollisionExit(collision : Collision)
  77. {
  78.     if(collision.gameObject.tag == "Terrain")
  79.     {
  80.         Grounded = false;
  81.         throttle = 0;
  82.     }
  83. }
  84.  
  85. function Update()
  86. {
  87.     carPhysicsUpdate();
  88.     if (Grounded && ((PlayerCar.localEulerAngles.z >= 0 && PlayerCar.localEulerAngles.z <= 50) || (PlayerCar.localEulerAngles.z >= 310 && PlayerCar.localEulerAngles.z <= 360) || (PlayerCar.localEulerAngles.z <= 0)))
  89.     {
  90.         checkInput();
  91.     }
  92.     else
  93.     {
  94.         throttle = 0;
  95.     }
  96.     if (Input.GetKeyDown(KeyCode.Return))
  97.     {
  98.         if (Grounded)
  99.         {
  100.             PlayerCar.localEulerAngles = Vector3.zero;
  101.         }
  102.     }
  103. }
  104.  
  105. function LateUpdate()
  106. {
  107.         rotateVisualWheels();
  108.         engineSound();
  109. }
  110.  
  111. function setUpWheels()
  112. {
  113.         if((null == frontLeftWheel || null == frontRightWheel || null == rearLeftWheel || null == rearRightWheel ))
  114.         {
  115.                 Debug.LogError("One or more of the wheel transforms have not been plugged in on the car");
  116.                 Debug.Break();
  117.         }
  118.         else
  119.         {
  120.                 wheelTransform[0] = frontLeftWheel;
  121.                 wheelTransform[1] = rearLeftWheel;
  122.                 wheelTransform[2] = frontRightWheel;
  123.                 wheelTransform[3] = rearRightWheel;
  124.         }
  125. }
  126.  
  127. private var rotationAmount : Vector3;
  128.  
  129. function rotateVisualWheels()
  130. {
  131.         LFWheelTransform.localEulerAngles.y = horizontal * 30;
  132.         RFWheelTransform.localEulerAngles.y = horizontal * 30;
  133.        
  134.         rotationAmount = carRight * (relativeVelocity.z * 1.6 * Time.deltaTime * Mathf.Rad2Deg);
  135.        
  136.         wheelTransform[0].Rotate(rotationAmount);
  137.         wheelTransform[1].Rotate(rotationAmount);
  138.         wheelTransform[2].Rotate(rotationAmount);
  139.         wheelTransform[3].Rotate(rotationAmount);
  140. }
  141.  
  142. private var deviceAccelerometerSensitivity : float = 2;
  143.  
  144. function checkInput()
  145. {
  146.         if (Application.platform == RuntimePlatform.IPhonePlayer ||(Application.platform == RuntimePlatform.Android))
  147.         {
  148.                 accel = Input.acceleration * deviceAccelerometerSensitivity;
  149.                
  150.                 if(accel.x > deadZone || accel.x < -deadZone)
  151.                 {
  152.                         horizontal = accel.x;
  153.                 }
  154.                 else
  155.                 {
  156.                         horizontal = 0;
  157.                 }
  158.                 throttle = 0;
  159.                
  160.                 for (var touch : Touch in Input.touches)
  161.                 {
  162.                         if(touch.position.x > Screen.width -Screen.width/3 && touch.position.y < Screen.height/3)
  163.                         {
  164.                                 throttle = 1;
  165.                         }
  166.                         else if(touch.position.x < Screen.width -Screen.width/3 && touch.position.y > Screen.height/3)
  167.                         {
  168.                                 throttle= -1;
  169.                         }
  170.                 }
  171.         }
  172.         else if (Application.platform == RuntimePlatform.WindowsEditor || RuntimePlatform.WindowsWebPlayer || RuntimePlatform.WindowsPlayer )
  173.         {
  174.                 horizontal = Input.GetAxis("Horizontal");
  175.                 throttle = Input.GetAxis("Vertical");
  176.         }
  177. }
  178.  
  179. function carPhysicsUpdate()
  180. {
  181.         myRight = carTransform.right;
  182.         velo = carRigidbody.velocity;
  183.         tempVEC = Vector3(velo.x,0,velo.z);
  184.         flatVelo = tempVEC;
  185.         dir = transform.TransformDirection(carFwd);
  186.         tempVEC = Vector3(dir.x,0,dir.z);
  187.         flatDir = Vector3.Normalize(tempVEC);
  188.         relativeVelocity = carTransform.InverseTransformDirection(flatVelo);
  189.         slideSpeed = Vector3.Dot(myRight,flatVelo);
  190.         mySpeed = flatVelo.magnitude;
  191.         rev = Mathf.Sign(Vector3.Dot(flatVelo,flatDir));
  192.         engineForce = ( flatDir * ( power * throttle ) * carMass);
  193.         actualTurn = horizontal;
  194.         if(rev < 0.1f)
  195.         {
  196.                 actualTurn =- actualTurn;
  197.         }
  198.         turnVec =((( carUp * turnSpeed ) * actualTurn ) * carMass )* 800;
  199.         actualGrip = Mathf.Lerp(100, carGrip, mySpeed * 0.02);
  200.         imp = myRight * ( -slideSpeed * carMass * actualGrip);
  201. }
  202.  
  203. function slowVelocity()
  204. {
  205.         carRigidbody.AddForce(-flatVelo * 0.8);
  206. }
  207.  
  208. function engineSound()
  209. {
  210.         audio.pitch = 0.30 + mySpeed * 0.025;
  211.         if (mySpeed > 30)
  212.         {
  213.                 audio.pitch = 0.25 + mySpeed * 0.015;
  214.         }
  215.         if (mySpeed > 40)
  216.         {
  217.                 audio.pitch = 0.20 + mySpeed * 0.013;
  218.         }
  219.         if (mySpeed > 49)
  220.         {
  221.                 audio.pitch = 0.15 + mySpeed * 0.011;
  222.         }
  223.         if ( audio.pitch > 2.0 )
  224.         {
  225.                 audio.pitch = 2.0;
  226.         }
  227. }
  228.  
  229. function FixedUpdate()
  230. {
  231.         if(mySpeed < maxSpeed)
  232.         {
  233.                 carRigidbody.AddForce( engineForce * Time.deltaTime);
  234.                
  235.                 if (mySpeed > maxSpeedToTurn)
  236.                 {
  237.                         carRigidbody.AddTorque ( turnVec * Time.deltaTime);
  238.                 }
  239.                 else if(mySpeed < maxSpeedToTurn)
  240.                 {
  241.                         return;
  242.                 }
  243.                 carRigidbody.AddForce( imp * Time.deltaTime );
  244.         }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement