Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using XInputDotNetPure;
  5. using UnityEngine.UI;
  6.  
  7. /*Bug list/to do list
  8. - Make all coins disappear
  9. - car currently rolls sometimes when resetting the position of the car.
  10. - brakes might need tweaking
  11. - wheels don't spin yet
  12. - make more adjustments to make car feel better
  13. - add deadzone to the sticks
  14. - adjust controls
  15. */
  16.  
  17.  
  18. [System.Serializable]
  19. public class AxleInfo2 //easily addable extra wheels
  20. {
  21. public WheelCollider leftWheel;
  22. public WheelCollider rightWheel;
  23. public bool motor;
  24. public bool steering;
  25. public bool brakes;
  26. }
  27.  
  28. public class CarController2 : MonoBehaviour
  29. {
  30. public List<AxleInfo2> axleInfos;
  31. public float maxMotorTorque;
  32. public float maxBrakeTorque;
  33. public float maxSteeringAngle;
  34. public float antirollbar = 1.0f; //helps to stop the car from rolling
  35. public Vector3 centreofmassOFFSET; //offsets the centre of mass to get a more realistic feel if needed
  36.  
  37. public int PlayNum; //set this to 1, 2, 3 and 4 for separated controls.
  38. public float contTriggerDeadzone = 0.0f; // set between 0.0f-0.3f to set deadzone well.
  39. public int coins = 0; //the amount of coins the player has
  40. public bool enableInput = false; // turn input on/off
  41.  
  42. public RawImage[] plus = new RawImage[11]; // displays +1 when a coin is collected
  43. public RawImage[] take = new RawImage[11]; // displays -1 when a trap is hit
  44. public int numToTake;
  45. public int numToDisp;
  46. public float TimePlus1OnScreen = 1.0f; //how long the +1 stays on screen
  47.  
  48. public float downforce;
  49.  
  50. public float boostPadIncrease = 10;
  51.  
  52. //public for debugging
  53. private float motor;
  54. private float brake;
  55. private float steering;
  56.  
  57. private bool starting = false;
  58. private Vector3 position;
  59. private Vector3 rotation;
  60. private float triggeroutput;
  61.  
  62. private float wheelradius;
  63. private float wheelrpm;
  64.  
  65. public void start()
  66. {
  67.  
  68. }
  69.  
  70. public void Update()
  71. {
  72. }
  73.  
  74. public void FixedUpdate()
  75. {
  76. if(Input.GetKeyDown(KeyCode.Z))
  77. {
  78. displayNeg1();
  79. }
  80. if (Input.GetKeyDown(KeyCode.X))
  81. {
  82. displayPlus1();
  83. }
  84.  
  85.  
  86. if (starting == false) //startup not working, fixed using this bool
  87. {
  88. GetComponent<Rigidbody>().centerOfMass += centreofmassOFFSET; //offsets the centre of mass
  89. position = transform.position; //setup respawn
  90. rotation = transform.rotation.eulerAngles;
  91.  
  92. foreach (AxleInfo2 axleInfo in axleInfos) //this helps to reduce weird wheelcollider jitters using settings found online
  93. {
  94. axleInfo.leftWheel.ConfigureVehicleSubsteps(5, 5, 1);
  95. axleInfo.rightWheel.ConfigureVehicleSubsteps(5, 5, 1);
  96. }
  97.  
  98. starting = true;
  99. }
  100. if (enableInput) // checks if input is enabled
  101. {
  102. doInput();
  103. }
  104. else
  105. {
  106. motor = 0; // if it's not turn off the motor.
  107. //*maybe enable vehicle brakes here to prevent rolling still occuring
  108. }
  109.  
  110. foreach (AxleInfo2 axleInfo in axleInfos) //apply steering, brakes and motor, get wheelradius and wheelrpm
  111. {
  112. wheelradius = axleInfo.rightWheel.radius;
  113. wheelrpm = axleInfo.leftWheel.rpm;
  114.  
  115. rollBar(axleInfo);
  116. if (axleInfo.steering)
  117. {
  118. axleInfo.leftWheel.steerAngle = steering;
  119. axleInfo.rightWheel.steerAngle = steering;
  120. }
  121. if (axleInfo.motor)
  122. {
  123. axleInfo.leftWheel.motorTorque = motor;
  124. axleInfo.rightWheel.motorTorque = motor;
  125. }
  126. if (axleInfo.brakes)
  127. {
  128. axleInfo.leftWheel.brakeTorque = brake;
  129. axleInfo.rightWheel.brakeTorque = brake;
  130. }
  131. }
  132. }
  133.  
  134. void doInput()
  135. {
  136. if (ControllerSetup.state[PlayNum].Triggers.Right >= contTriggerDeadzone
  137. && ControllerSetup.prevState[PlayNum].Buttons.LeftShoulder == ButtonState.Released) //drive forward if not pressing reverse button
  138. {
  139. motor = maxMotorTorque * ControllerSetup.state[PlayNum].Triggers.Right;
  140. }
  141. else
  142. {
  143. motor = 0;
  144. }
  145. if (ControllerSetup.state[PlayNum].Triggers.Left >= contTriggerDeadzone) //apply the brakes
  146. {
  147. brake = maxBrakeTorque * ControllerSetup.state[PlayNum].Triggers.Left;
  148. }
  149. else
  150. {
  151. brake = 0;
  152. }
  153. steering = maxSteeringAngle * ControllerSetup.state[PlayNum].ThumbSticks.Left.X; //steer car
  154.  
  155. if (ControllerSetup.state[PlayNum].Buttons.LeftShoulder == ButtonState.Pressed
  156. && ControllerSetup.prevState[PlayNum].Buttons.LeftShoulder == ButtonState.Pressed) //reverse the car
  157. {
  158. motor = maxMotorTorque * -ControllerSetup.state[PlayNum].Triggers.Right;
  159. }
  160.  
  161. if (Input.GetKeyDown(KeyCode.R)) // reset the car
  162. {
  163. GetComponent<Rigidbody>().velocity = Vector3.zero;
  164. GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
  165. transform.position = position;
  166. transform.localEulerAngles = rotation;
  167. }
  168. }
  169.  
  170. void rollBar(AxleInfo2 axleInfo)
  171. {
  172.  
  173. WheelHit hit;
  174.  
  175. float leftwheelTravel = 1.0f; //set the wheel travel to fully extended
  176. float rightwheelTravel = 1.0f;
  177.  
  178. bool leftwheelGrounded = axleInfo.leftWheel.GetGroundHit(out hit); //check if wheels are grounded
  179. bool rightwheelGrounded = axleInfo.rightWheel.GetGroundHit(out hit);
  180.  
  181. if (leftwheelGrounded)//sets this to the actual wheel travel
  182. {
  183. leftwheelTravel = (-axleInfo.leftWheel.transform.InverseTransformPoint(hit.point).y - axleInfo.leftWheel.radius) / axleInfo.leftWheel.suspensionDistance;
  184. }
  185. else
  186. {
  187. leftwheelTravel = 1.0f;
  188. }
  189.  
  190. if (rightwheelGrounded)
  191. {
  192. rightwheelTravel = (-axleInfo.rightWheel.transform.InverseTransformPoint(hit.point).y - axleInfo.rightWheel.radius) / axleInfo.rightWheel.suspensionDistance;
  193. }
  194. else
  195. {
  196. rightwheelTravel = 1.0f;
  197. }
  198.  
  199. float antirollforce = (leftwheelTravel - rightwheelTravel) * antirollbar; // this is where we do what an antirollbar actually does and applies the stiffness
  200.  
  201. if (leftwheelGrounded) //add the rollbar force to the wheels
  202. {
  203. GetComponent<Rigidbody>().AddForceAtPosition(axleInfo.leftWheel.transform.up * -antirollforce, axleInfo.leftWheel.transform.position);
  204. }
  205.  
  206. if (rightwheelGrounded)
  207. {
  208. GetComponent<Rigidbody>().AddForceAtPosition(axleInfo.rightWheel.transform.up * -antirollforce, axleInfo.rightWheel.transform.position);
  209. }
  210.  
  211.  
  212. }
  213.  
  214. public void resetpos() //reset the car
  215. {
  216. GetComponent<Rigidbody>().velocity = Vector3.zero;
  217. GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
  218. transform.position = position;
  219. transform.localEulerAngles = rotation;
  220. coins = 0;
  221.  
  222. }
  223.  
  224. public void displayPlus1() //display the +1
  225. {
  226. plus[1].enabled = true;
  227. coins += 1;
  228. Invoke("p1Disabled", TimePlus1OnScreen);
  229. }
  230. void p1Disabled()
  231. {
  232. plus[1].enabled = false;
  233. }
  234.  
  235. public void displayNeg1() //display the -1
  236. {
  237.  
  238. if (coins >= numToTake)
  239. {
  240. coins -= numToTake;
  241. numToDisp = numToTake;
  242. }
  243. else
  244. {
  245. numToDisp = coins;
  246. coins = 0;
  247. }
  248. take[numToDisp].enabled = true;
  249. StartCoroutine(Neg1Disabled());
  250. }
  251.  
  252. IEnumerator Neg1Disabled()
  253. {
  254. int x = numToDisp;
  255. yield return new WaitForSeconds(TimePlus1OnScreen);
  256. take[x].enabled = false;
  257. }
  258.  
  259. public void boostCar(Vector3 dir)
  260. {
  261. GetComponent<Rigidbody>().velocity += dir * boostPadIncrease;
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement