- Home /
Can this be converted to C#?
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[i] < 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 ;)
For starters you have to wrap your code inside a class and import related libraries. Take a look at the first $$anonymous$$utes of this video: http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes . And it would really help if you'd posted the errors here.
It 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.centerOf$$anonymous$$ass.y = -1.5f;
Will have to be
Vector3 com = rigidbody.centerOf$$anonymous$$ass;
com.y = -1.5f;
rigidbody.centerOf$$anonymous$$ass = com;
Just from looking at it I can't tell what else would throw an error though
Answer by Julien-Lynge · Nov 30, 2013 at 06:02 PM
@numberkruncher had it exactly right: the answer is "Yes!". You can totally convert this from JS to C#.
This is a question that's asked so often I've put together a blog post to answer it:
http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/
Good luck!
playercar.cs(54,17): error CS0103: The name `AppropriateGear' does not exist in the current context
this is the only remaining error that i get. I still have no idea what to do.
You need to read the link that Julien.Lynge posted, and put two and two together. You shouldn't mix languages if you want the two classes to talk to each other.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Need help converting js to C# -- yield return WaitForSeconds 3 Answers
JS to C#, anyone? 1 Answer