Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-2
Question by J-F · Nov 28, 2013 at 06:20 PM · c#

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

Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image numberkruncher · Nov 28, 2013 at 06:24 PM 1
Share

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 ;)

avatar image bompi88 · Nov 28, 2013 at 10:24 PM 0
Share

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.

avatar image iwaldrop · Nov 28, 2013 at 10:48 PM 0
Share

It's always possible to concert from Unityscript to C#.

avatar image Josh707 · Nov 28, 2013 at 11:31 PM 1
Share

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

avatar image iwaldrop · Nov 29, 2013 at 12:33 AM 1
Share

Would you care to share how you would do it?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image J-F · Nov 30, 2013 at 06:17 PM 0
Share
 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.

avatar image iwaldrop · Nov 30, 2013 at 09:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Need help converting js to C# -- GUIStyle 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges