using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class car_Control : MonoBehaviour {
|
|
|
|
public GameObject forcePoint;
|
|
public Suspension[] supensions;
|
|
//public Suspension supFR;
|
|
//public Suspension supFL;
|
|
//public Suspension supBR;
|
|
//public Suspension supBL;
|
|
public float speed;
|
|
public float torque;
|
|
public float grip;
|
|
public float driftGrip;
|
|
|
|
private float curGrip;
|
|
|
|
public Vector3 centreOfMass;
|
|
|
|
|
|
public Rigidbody carRigid;
|
|
|
|
// Use this for initialization
|
|
void Start () {
|
|
carRigid = GetComponent<Rigidbody>();
|
|
carRigid.centerOfMass = centreOfMass;
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate () {
|
|
carRigid.centerOfMass = centreOfMass;
|
|
float inputX = 1; //Input.GetAxis ("Player1 Movement Y");
|
|
float inputY = 0; //Input.GetAxis("Player1 Movement X");
|
|
|
|
applyAcceleration (inputX);
|
|
applyTorque (inputY);
|
|
driftCheck ();
|
|
applyGrip ();
|
|
|
|
|
|
}
|
|
|
|
void applyAcceleration(float input){
|
|
Vector3 force = transform.forward * input * speed;
|
|
//Vector3 rayNormal = (supensions[0].supHit.normal + supensions[1].supHit.normal + supensions[2].supHit.normal + supensions[3].supHit.normal) / 4;
|
|
Vector3 rayNormal = suspensionNormal ();
|
|
|
|
rayNormal = Quaternion.AngleAxis(90, transform.right) * rayNormal;
|
|
Vector3 projectedForce = Vector3.Project (force, rayNormal);
|
|
//carRigid.AddForceAtPosition (projectedForce,forcePoint.transform.position);
|
|
carRigid.AddForce(projectedForce);
|
|
}
|
|
|
|
void applyTorque (float input){
|
|
carRigid.AddTorque(transform.up * torque * input);
|
|
}
|
|
|
|
void driftCheck(){
|
|
if (Input.GetButton ("Player1 Jump")) {
|
|
curGrip = driftGrip;
|
|
} else {
|
|
curGrip = grip;
|
|
}
|
|
}
|
|
|
|
void applyGrip(){
|
|
|
|
float localVelX = transform.InverseTransformDirection(carRigid.velocity).x;
|
|
bool grounded = false;
|
|
|
|
for (int i = 0; i<supensions.Length; i++) {
|
|
if (supensions[i].supcompressionRatio >0)
|
|
grounded = true;
|
|
}
|
|
|
|
Debug.Log ("Grounded: " + grounded);
|
|
if (grounded) {
|
|
for (int i = 0; i<supensions.Length; i++) {
|
|
carRigid.AddForceAtPosition (transform.right * -localVelX * curGrip, supensions [i].transform.position);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
Vector3 suspensionNormal(){
|
|
|
|
Vector3 normal = supensions[0].supHit.normal;
|
|
for (int i = 1;i< supensions.Length; i++){
|
|
normal += supensions[i].supHit.normal;
|
|
}
|
|
normal /= supensions.Length;
|
|
|
|
return normal;
|
|
}
|
|
|
|
|
|
|
|
}
|