using UnityEngine;
using System.Collections;
public class CarDrive : MonoBehaviour {
public WheelCollider wheelColLF;
public WheelCollider wheelColLB;
public WheelCollider wheelColRF;
public WheelCollider wheelColRB;
public Transform transWheelLF;
public Transform transWheelLB;
public Transform transWheelRF;
public Transform transWheelRB;
public float motorTorqueMax = 600;
float motorCurrentTorque = 0;//当前扭矩
public float steerAngleMax = 25;
public float brakeTorqueMax = 3000;
Rigidbody rigid;
//发动机最大转速
public float maxEngineRPM = 1500;
//发动机最小转速
public float minEngineRPM = 200;
//档位(车轮半径/发动机齿轮半径)
public float[] gearRatios;
//当前档位编号
int gearIndex = 0;
float dangousTime=0;
Vector3 safePosition;
Quaternion safeRotation;
void Start()
{
rigid = GetComponent<Rigidbody>();
Vector3 center = rigid.centerOfMass;//车的重心
center.y = 0.2f;
rigid.centerOfMass = center;
FinalPoint.instance.finalPointEvent += OnFinal;
CarNumber.instance.startRaceEvent += OnStart;
}
public void Drive (float v,float h,float b)
{
GearControl();
motorCurrentTorque = motorTorqueMax * gearRatios[gearIndex];
//前进
wheelColLF.motorTorque = motorCurrentTorque * v;
wheelColRF.motorTorque = motorCurrentTorque * v;
//转弯
wheelColLF.steerAngle = steerAngleMax * h;
wheelColRF.steerAngle = steerAngleMax * h;
//刹车
wheelColLF.brakeTorque = brakeTorqueMax * b;
wheelColLB.brakeTorque = brakeTorqueMax * b;
wheelColRF.brakeTorque = brakeTorqueMax * b;
wheelColRB.brakeTorque = brakeTorqueMax * b;
SetWheelPose(transWheelLF, wheelColLF);
SetWheelPose(transWheelLB, wheelColLB);
SetWheelPose(transWheelRF, wheelColRF);
SetWheelPose(transWheelRB, wheelColRB);
DangousCheck();
}
void SetWheelPose(Transform transWheel,WheelCollider wheelCollider)
{
Vector3 pos;
Quaternion quat;
wheelCollider.GetWorldPose(out pos, out quat);
transWheel.position = pos;
transWheel.rotation = quat;
}
void GearControl()
{
if (wheelColLF.rpm*gearRatios[gearIndex]>maxEngineRPM)
{
gearIndex++;
gearIndex = Mathf.Clamp(gearIndex, 0, gearRatios.Length - 1);
}
if (wheelColLF.rpm*gearRatios[gearIndex]<minEngineRPM)
{
gearIndex--;
gearIndex = Mathf.Clamp(gearIndex, 0, gearRatios.Length - 1);
}
}
void DangousCheck()
{
int countWheelOnGround = 0;
if(wheelColLF.isGrounded)
countWheelOnGround++;
if(wheelColLB.isGrounded)
countWheelOnGround++;
if(wheelColRF.isGrounded)
countWheelOnGround++;
if(wheelColRB.isGrounded)
countWheelOnGround++;
if(countWheelOnGround==4)
{
safePosition = transform.position;
safeRotation = transform.rotation;
}
if (countWheelOnGround <= 2)
dangousTime += Time.deltaTime;
else
dangousTime = 0;
if (dangousTime>5)
{
transform.position = safePosition + Vector3.up * 0.5f;
transform.rotation = safeRotation;
dangousTime = 0;
}
}
}
using UnityEngine;
using System.Collections;
public class UserInput : MonoBehaviour {
CarDrive carDrive;
void Start () {
carDrive = GetComponent<CarDrive>();
}
float accelerator = 0;//油门
float accelAddSpeed = 0.2f;
void Update () {
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
float b = Input.GetAxis("Jump");//刹车
if (Mathf.Abs(v)>0.5f)
{
accelerator += v * accelAddSpeed*Time.deltaTime;
}
else
{
accelerator = 0;
}
accelerator = Mathf.Clamp(accelerator, -0.2f, 1);
carDrive.Drive(accelerator, h, b);
}
}