Help erro!!
using UnityEngine;
using System.Collections;
public class CarEngine : MonoBehaviour {
public WheelCollider FrontLeftWheel;
public WheelCollider FrontRightWheel;
public WheelCollider BackLeftWheel;
public WheelCollider BackRightWheel;
public WheelCollider LightCollider;
public float SteerAngle;
public float[] GearRatio;
private int CurrentGear = 0;
public float EngineTorque = 600.0f;
public float MaxEngineRPM = 3000.0f;
public float MinEngineRPM = 1000.0f;
public float EngineRPM = 0.0f;
public GameObject RedLight1;
public GameObject RedLight2;
public GameObject BackwardLight1;
public GameObject BackwardLight2;
private Color redbright;
private Color reddark;
public AudioSource horn;
private static int speed;
public GameObject Smoke1;
public ParticleSystem Smoke1p;
private Color SMwhite;
private Color SMgrey;
void Start (){
GetComponent<Rigidbody>().centerOfMass.y = -1.5f;
}
IEnumerator BackParticle (){
yield return new WaitForSeconds(2);
Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMgrey);
}
void Update (){
//smoke particle effect
if(Input.GetKeyDown("w"))
{
Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMwhite);
Smoke1p.startSize = 2.5f;
BackParticle();
}
if(Input.GetKeyUp("w"))
{
Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMgrey);
Smoke1p.startSize = 1;
}
speed = GetComponent<Rigidbody>().velocity.magnitude*2.5f;
GetComponent<Rigidbody>().drag = GetComponent<Rigidbody>().velocity.magnitude / 250;
EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
//audio
ShiftGears();
GetComponent<AudioSource>().volume = Mathf.Abs(MaxEngineRPM / EngineRPM) - 0.5f;
GetComponent<AudioSource>().pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1;
if ( GetComponent<AudioSource>().pitch > 1.5f )
{
GetComponent<AudioSource>().pitch -= 0.5f;
}
//backup for backward
if((EngineRPM<=-200) && (speed>=25)){
BackLeftWheel.brakeTorque = 30;
BackRightWheel.brakeTorque = 30;
EngineRPM=-190;
}
else if((EngineRPM<=-200) && (speed<=25)){
BackLeftWheel.brakeTorque = 0;
BackRightWheel.brakeTorque = 0;
}
//backup for friction car
if(speed>90)
{
FrontLeftWheel.suspensionSpring.spring = 1000;
FrontRightWheel.suspensionSpring.spring = 1000;
BackLeftWheel.suspensionSpring.spring = 1000;
BackRightWheel.suspensionSpring.spring = 1000;
}
if(speed<90)
{
FrontLeftWheel.suspensionSpring.spring = 2000;
FrontRightWheel.suspensionSpring.spring = 2000;
BackLeftWheel.suspensionSpring.spring = 2000;
BackRightWheel.suspensionSpring.spring = 2000;
}
if(speed<70)
{
FrontLeftWheel.suspensionSpring.spring = 2500;
FrontRightWheel.suspensionSpring.spring = 2500;
BackLeftWheel.suspensionSpring.spring = 2500;
BackRightWheel.suspensionSpring.spring = 2500;
}
if(speed<20)
{
FrontLeftWheel.suspensionSpring.spring = 5500;
FrontRightWheel.suspensionSpring.spring = 5500;
BackLeftWheel.suspensionSpring.spring = 5500;
BackRightWheel.suspensionSpring.spring = 5500;
}
//value of steer if speed is higher steering is smaller.
if(speed<50)
{
SteerAngle = 30 + GetComponent<Rigidbody>().velocity.magnitude * -0.2f ;
}
if(speed>15)
{
SteerAngle = 15 + GetComponent<Rigidbody>().velocity.magnitude * -0.2f ;
}
if(Input.GetKeyDown("s"))
{
FrontLeftWheel.brakeTorque = 12;
FrontRightWheel.brakeTorque = 12;
RedLight1.GetComponent<Renderer>().material.color = redbright;
RedLight2.GetComponent<Renderer>().material.color = redbright;
}
if(Input.GetKeyUp("s"))
{
FrontLeftWheel.brakeTorque = 5;
FrontRightWheel.brakeTorque = 5;
RedLight1.GetComponent<Renderer>().material.color = reddark;
RedLight2.GetComponent<Renderer>().material.color = reddark;
}
//handbrake
if(Input.GetKeyDown(KeyCode.Space))
{
if(speed<15){
BackLeftWheel.brakeTorque = 200;
BackRightWheel.brakeTorque = 200;
}
BackLeftWheel.sidewaysFriction.extremumValue = 5;
BackRightWheel.sidewaysFriction.extremumValue = 5;
FrontLeftWheel.sidewaysFriction.extremumValue = 1000;
FrontRightWheel.sidewaysFriction.extremumValue = 1000;
}
if(Input.GetKeyUp(KeyCode.Space))
{
BackLeftWheel.brakeTorque = 0;
BackRightWheel.brakeTorque = 0;
BackLeftWheel.sidewaysFriction.extremumValue = 20000;
BackRightWheel.sidewaysFriction.extremumValue = 20000;
FrontLeftWheel.sidewaysFriction.extremumValue = 20000;
FrontRightWheel.sidewaysFriction.extremumValue = 20000;
}
//lights detect
if(EngineRPM<-2)
{
BackwardLight1.GetComponent<Renderer>().material.color = Color.white;
BackwardLight2.GetComponent<Renderer>().material.color = Color.white;
RedLight1.GetComponent<Renderer>().material.color = reddark;
RedLight2.GetComponent<Renderer>().material.color = reddark;
}
if(EngineRPM>-1)
{
BackwardLight1.GetComponent<Renderer>().material.color = Color.black;
BackwardLight2.GetComponent<Renderer>().material.color = Color.black;
}
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontLeftWheel.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
LightCollider.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
if(Input.GetKeyDown("h"))
{
horn.Play();
}
}
void ShiftGears()
{
int AppropriateGear;
// this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make
// the engine RPM fall within the desired range. The gear is then set to this "appropriate" value.
if (EngineRPM >= MaxEngineRPM) {
AppropriateGear = CurrentGear;
for (int i = 0; i < GearRatio.Length; i++) {
if (FrontLeftWheel.rpm * GearRatio [i] < MaxEngineRPM) {
AppropriateGear = i;
break;
}
}
CurrentGear = AppropriateGear;
}
if (EngineRPM <= MinEngineRPM) {
AppropriateGear = CurrentGear;
for (int j = 0; j < GearRatio.Length; j--) {
if (FrontLeftWheel.rpm * GearRatio [j] > MinEngineRPM) {
AppropriateGear = j;
break;
}
}
CurrentGear = AppropriateGear;
}
}
}