A while ago i made a mobile version of this:
http://www.gotow.net/andrew/blog/?page_id=78
And now i want to convert it to a C# script so that other C# scripts can access it.
I first tried to convert the mobile edit but it ended up in a mess and i decided to start over.
This is what i’ve got so far:
WheelCollider FrontRightWheel;
float[] GearRatio;
int CurrentGear = 0;
float EngineTorque = 600.0f;
float MaxEngineRPM = 3000.0f;
float MinEngineRPM = 1000.0f;
private float EngineRPM = 0.0f;
void Start (){
rigidbody.centerOfMass.y = -1.5f;
}
void Update (){
rigidbody.drag = rigidbody.velocity.magnitude / 250;
EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
ShiftGears();
audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0f ;
if ( audio.pitch > 2.0f ) {
audio.pitch = 2.0f;
}
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontLeftWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
}
void ShiftGears (){
if ( EngineRPM >= MaxEngineRPM ) {
int AppropriateGear = CurrentGear;
for ( int i= 0; i < GearRatio.length; i ++ ) {
if ( FrontLeftWheel.rpm * GearRatio *< MaxEngineRPM ) {*
-
AppropriateGear = i;* -
break;* -
}* -
}* -
CurrentGear = AppropriateGear;* -
}*
-
if ( EngineRPM <= MinEngineRPM ) {*
-
AppropriateGear = CurrentGear;* -
for ( int j= GearRatio.length-1; j >= 0; j -- ) {*
_ if ( FrontLeftWheel.rpm * GearRatio[j] > MinEngineRPM ) {_
-
AppropriateGear = j;* -
break;* -
}* -
}* -
CurrentGear = AppropriateGear;* -
}*
}
}
There are about 6 errors that i have no idea how to fix.
Any help would be appreciated
Topics like this are generally better discussed within the Unity forums since the question title and eventual answer is unlikely to be useful for future readers in Q&A style. i.e. the answer to your question "Can this be converted to C#?" is Yes ;)
– numberkruncherFor starters you have to wrap your code inside a class and import related libraries. Take a look at the first minutes of this video: http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes . And it would really help if you'd posted the errors here.
– bompi88It's always possible to concert from Unityscript to C#.
– iwaldropIt would really help if you posted all of the errors, but I don't think you can set individual axis values for "transform" values, like the center of mass or position in C#. So rigidbody.centerOfMass.y = -1.5f; Will have to be Vector3 com = rigidbody.centerOfMass; com.y = -1.5f; rigidbody.centerOfMass = com; Just from looking at it I can't tell what else would throw an error though
– Josh707@Josh707 why do you have to do it like that? You could just do it in one line.....
– MrProfessorTroll