You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.3 KiB

9 years ago
  1. using UnityEngine;
  2. using System.Collections;
  3. public class car_Control : MonoBehaviour {
  4. public GameObject forcePoint;
  5. public Suspension[] supensions;
  6. //public Suspension supFR;
  7. //public Suspension supFL;
  8. //public Suspension supBR;
  9. //public Suspension supBL;
  10. public float speed;
  11. public float torque;
  12. public float grip;
  13. public float driftGrip;
  14. private float curGrip;
  15. public Vector3 centreOfMass;
  16. public Rigidbody carRigid;
  17. // Use this for initialization
  18. void Start () {
  19. carRigid = GetComponent<Rigidbody>();
  20. carRigid.centerOfMass = centreOfMass;
  21. }
  22. // Update is called once per frame
  23. void FixedUpdate () {
  24. carRigid.centerOfMass = centreOfMass;
  25. float inputX = 1; //Input.GetAxis ("Player1 Movement Y");
  26. float inputY = 0; //Input.GetAxis("Player1 Movement X");
  27. applyAcceleration (inputX);
  28. applyTorque (inputY);
  29. driftCheck ();
  30. applyGrip ();
  31. }
  32. void applyAcceleration(float input){
  33. Vector3 force = transform.forward * input * speed;
  34. //Vector3 rayNormal = (supensions[0].supHit.normal + supensions[1].supHit.normal + supensions[2].supHit.normal + supensions[3].supHit.normal) / 4;
  35. Vector3 rayNormal = suspensionNormal ();
  36. rayNormal = Quaternion.AngleAxis(90, transform.right) * rayNormal;
  37. Vector3 projectedForce = Vector3.Project (force, rayNormal);
  38. //carRigid.AddForceAtPosition (projectedForce,forcePoint.transform.position);
  39. carRigid.AddForce(projectedForce);
  40. }
  41. void applyTorque (float input){
  42. carRigid.AddTorque(transform.up * torque * input);
  43. }
  44. void driftCheck(){
  45. if (Input.GetButton ("Player1 Jump")) {
  46. curGrip = driftGrip;
  47. } else {
  48. curGrip = grip;
  49. }
  50. }
  51. void applyGrip(){
  52. float localVelX = transform.InverseTransformDirection(carRigid.velocity).x;
  53. bool grounded = false;
  54. for (int i = 0; i<supensions.Length; i++) {
  55. if (supensions[i].supcompressionRatio >0)
  56. grounded = true;
  57. }
  58. Debug.Log ("Grounded: " + grounded);
  59. if (grounded) {
  60. for (int i = 0; i<supensions.Length; i++) {
  61. carRigid.AddForceAtPosition (transform.right * -localVelX * curGrip, supensions [i].transform.position);
  62. }
  63. }
  64. }
  65. Vector3 suspensionNormal(){
  66. Vector3 normal = supensions[0].supHit.normal;
  67. for (int i = 1;i< supensions.Length; i++){
  68. normal += supensions[i].supHit.normal;
  69. }
  70. normal /= supensions.Length;
  71. return normal;
  72. }
  73. }