public Rigidbody L;
public Rigidbody R;
//Our speed
public float mediumSpeed=400F;
//Rotation speed
public float mediumRotSpeed=150F;
//Rotating koefficient, then greather, then faster
public float rotatingspeedKoefficient=1F;
//Debugging output
public float curEnginePowerL;
//To hide them change 'public' to 'private'
public float curEnginePowerR;
//Is we currently rotating left?
public bool RotatingLeft;
//Same as above, but reversed
public bool RotatingRight;
//Accelerating speed
public float step=5F;
//Decellerating speed
public float minusStep=10F;
//Output to GUI
private GUIText Gtext;
//Low rotating force
public float lowRotating=5F;
//High rotating force
public float highRotating=150F;
//Info about current L power
private float lastL;
//Same as above, inversed.
private float lastR;
// Use this for initialization
void Start () {
//Initialization of engines
curEnginePowerL=curEnginePowerR=0F;
if(L==null||R==null){
//Just for lulz
Application.Quit();
}
//Searching for output
if(Gtext==null){
//No output. Bad.
}
}
// Update is called once per frame
void Update () {
//No keys pressed
if(!Input.GetKey(KeyCode.W)&&!Input.GetKey(KeyCode.A)&&!Input.GetKey(KeyCode.D)&&!Input.GetKey(KeyCode.S)){
}
if(Input.GetKey(KeyCode.Space)){
//You can simply add Joystick input, by calling these functions with koefficients
Forward(1);
}
if(Input.GetKey(KeyCode.LeftAlt)){
//For lulz: You can add back moving by calling Forward(-1)(for ex.)
if(curEnginePowerL>0){
curEnginePowerL-=minusStep;
}
if(curEnginePowerR>0){
curEnginePowerR-=minusStep;
}
}
if(!Input.GetKey(KeyCode.A)){
RotatingLeft=false;
}
if(!Input.GetKey(KeyCode.D)){
RotatingRight=false;
}
if(curEnginePowerL<0){
//Stabilization
curEnginePowerL+=step*3;
}
if(curEnginePowerR<0){
curEnginePowerR+=step*3;
}
if(!Input.GetKey(KeyCode.W)){
if(RotatingLeft==true&&curEnginePowerR>mediumRotSpeed){
curEnginePowerR-=minusStep;
}
if(RotatingRight==true&&curEnginePowerL>mediumRotSpeed){
curEnginePowerL-=minusStep;
}
}
}
void Forward(float Fpower){
if(curEnginePowerL<mediumSpeed&&RotatingLeft==false){
//Simply add power
curEnginePowerL+=step*Fpower;
}
if(curEnginePowerR<mediumSpeed&&RotatingRight==false){
curEnginePowerR+=step*Fpower;
}
}
void Left(float Lpower){
RotatingLeft=true;
RotatingRight=false;
if(curEnginePowerR>highRotating){
if(curEnginePowerL<mediumRotSpeed){
curEnginePowerL+=step*Lpower;
}
if(curEnginePowerL>mediumRotSpeed){
curEnginePowerL-=minusStep*Lpower;
}
}
else{
if(curEnginePowerL>lowRotating){
curEnginePowerL-=minusStep*Lpower;
}
if(curEnginePowerL<lowRotating){
curEnginePowerL+=step*Lpower;
}
if(curEnginePowerR<highRotating){
curEnginePowerR+=step*Lpower;
}
}
}
void Right(float Rpower){
RotatingRight=true;
RotatingLeft=false;
if(curEnginePowerL>highRotating){
if(curEnginePowerR<mediumRotSpeed){
curEnginePowerR+=step*Rpower;
}
if(curEnginePowerR>mediumRotSpeed){
curEnginePowerR-=minusStep*Rpower;
}
}
else{
if(curEnginePowerR>lowRotating){
curEnginePowerR-=minusStep*Rpower;
}
if(curEnginePowerR<lowRotating){
curEnginePowerR+=step*Rpower;
}
if(curEnginePowerL<highRotating){
curEnginePowerL+=step*Rpower;
}
}
}
void FixedUpdate(){
//Apply forces
//We use physics, so we use FixedUpdate, so we call AddForce.
L.AddForce(transform.forward*curEnginePowerL,ForceMode.Acceleration);
R.AddForce(transform.forward*curEnginePowerR,ForceMode.Acceleration);
}
void OnGUI(){//Draw output
if(lastL!=curEnginePowerL||lastR!=curEnginePowerR){
GUI.Label(new Rect(Screen.width / 2,Screen.height / 2, 200, 20), curEnginePowerL.ToString());
lastL=curEnginePowerL;
lastR=curEnginePowerR;
}
}
}