Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by NinjaRubberBand · Oct 11, 2016 at 02:07 PM · javascriptscripting problemconverting

Help with converting JS to c#

I followed this tutorial, but its in javascript and i need it in c#. https://www.youtube.com/watch?v=JQiknAoC66A

Its a tutorial on how to move a car a path. I know both javascript and a little c#, but i have never really tried converting a script, so would be nice if someone could help. The script that i need converted to c# vvvv

 var centerOfMass : Vector3;
 var path : Array;
 var pathGroup : Transform;
 var maxSteer : float = 15.0;
 var wheelFL : WheelCollider; 
 var wheelFR : WheelCollider;
 var wheelRL : WheelCollider; 
 var wheelRR : WheelCollider;
 var currentPathObj : int;
 var distFromPath : float = 20;
 var maxTorque : float = 50;
 var currentSpeed : float;
 var topSpeed : float = 150;
 var decellarationSpeed : float = 10;
 
 
 function Start () {
 rigidbody.centerOfMass = centerOfMass;
 GetPath();
 }
 
 function GetPath (){
 var path_objs : Array = pathGroup.GetComponentsInChildren(Transform);
 path = new Array();
 
 for (var path_obj : Transform in path_objs){
  if (path_obj != pathGroup)
   path [path.length] = path_obj;
 }
 }
 
 
 function Update () {
 GetSteer();
 Move();
 }
 
 function GetSteer(){
 var steerVector : Vector3 = transform.InverseTransformPoint(Vector3(path[currentPathObj].position.x,transform.position.y,path[currentPathObj].position.z));
 var newSteer : float = maxSteer * (steerVector.x / steerVector.magnitude);
 wheelFL.steerAngle = newSteer;
 wheelFR.steerAngle = newSteer;
 
 if (steerVector.magnitude <= distFromPath){
 currentPathObj++;
 if (currentPathObj >= path.length)
 currentPathObj = 0;
 }
 
 }
 
 function Move (){
 currentSpeed = 2*(22/7)*wheelRL.radius*wheelRL.rpm * 60 / 1000;
 currentSpeed = Mathf.Round (currentSpeed);
 if (currentSpeed <= topSpeed){
 wheelRL.motorTorque = maxTorque;
 wheelRR.motorTorque = maxTorque;
 wheelRL.brakeTorque = 0;
 wheelRR.brakeTorque = 0;
 }
 else {
 wheelRL.motorTorque = 0;
 wheelRR.motorTorque = 0;
 wheelRL.brakeTorque = decellarationSpeed;
 wheelRR.brakeTorque = decellarationSpeed;
 }
 }



My attempt

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CarEngine : MonoBehaviour
 {
 
     public Vector3 centerOfMass;
     public GameObject[] path;
     public Transform pathGroup;
     public float maxSteer = 15.0f;
     public WheelCollider wheelFL;
     public WheelCollider wheelFR;
     public WheelCollider wheelRL;
     public WheelCollider wheelRR;
     public int currentPathObj;
     public float distFromPath = 20f;
     public float maxTorque = 50f;
     public float currentSpeed;
     public float topSpeed = 150f;
     public float decellarationSpeed = 10f;
 
 
 void Start()
 {
     rigidbody.centerOfMass = centerOfMass;
     GetPath();
 }
 
 void GetPath()
 {
     public path_objs : Array = pathGroup.GetComponentsInChildren(Transform);
     path = new Array();
 
     for (public path_obj : Transform in path_objs)
     {
         if (path_obj != pathGroup)
             path[path.length] = path_obj;
     }
 }
 
 
 void Update()
 {
     GetSteer();
     Move();
 }
 
 void GetSteer()
 {
     public steerVector : Vector3 = transform.InverseTransformPoint(Vector3(path[currentPathObj].position.x, transform.position.y, path[currentPathObj].position.z));
     public newSteer : float = maxSteer * (steerVector.x / steerVector.magnitude);
     wheelFL.steerAngle = newSteer;
     wheelFR.steerAngle = newSteer;
 
     if (steerVector.magnitude <= distFromPath)
     {
         currentPathObj++;
         if (currentPathObj >= path.length)
             currentPathObj = 0;
     }
 
 }
 
 void Move()
 {
     currentSpeed = 2 * (22 / 7) * wheelRL.radius * wheelRL.rpm * 60 / 1000;
     currentSpeed = Mathf.Round(currentSpeed);
     if (currentSpeed <= topSpeed)
     {
         wheelRL.motorTorque = maxTorque;
         wheelRR.motorTorque = maxTorque;
         wheelRL.brakeTorque = 0;
         wheelRR.brakeTorque = 0;
     }
     else
     {
         wheelRL.motorTorque = 0;
         wheelRR.motorTorque = 0;
         wheelRL.brakeTorque = decellarationSpeed;
         wheelRR.brakeTorque = decellarationSpeed;
     }
 }
 
 }


The errors i got:

Assets/FortheGame/CarEngine.cs(32,10): error CS1525: Unexpected symbol public' Assets/FortheGame/CarEngine.cs(35,15): error CS1525: Unexpected symbol public' Assets/FortheGame/CarEngine.cs(39,5): error CS1525: Unexpected symbol }' Assets/FortheGame/CarEngine.cs(47,1): error CS1525: Unexpected symbol }' Assets/FortheGame/CarEngine.cs(51,10): error CS1525: Unexpected symbol public' Assets/FortheGame/CarEngine.cs(52,10): error CS1525: Unexpected symbol public' Assets/FortheGame/CarEngine.cs(65,9): error CS1547: Keyword void' cannot be used in this context Assets/FortheGame/CarEngine.cs(65,10): error CS1525: Unexpected symbol (', expecting )', ,', ;', [', or `=' Assets/FortheGame/CarEngine.cs(83,1): error CS8032: Internal compiler error during parsing, Run with -v for details

Thank u, hope someone can help :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JScotty · Oct 11, 2016 at 02:28 PM

Hi,

I noticed you used (ex) public path_objs : Array.. Transform this is an JS method, C# is just Transform[] pathobjs = getcomponentsinchildren(); local variables are just defined by using it type (this ex, Transform) and Arrays are defined with [] or you can use generic List

I have not tested if all your calculations etc worked but here is an c# example:

  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  
  public class CarEngine : MonoBehaviour
  {
  
      public Vector3 centerOfMass;
      public GameObject[] path;
      public Transform pathGroup;
      public float maxSteer = 15.0f;
      public WheelCollider wheelFL;
      public WheelCollider wheelFR;
      public WheelCollider wheelRL;
      public WheelCollider wheelRR;
      public int currentPathObj;
      public float distFromPath = 20f;
      public float maxTorque = 50f;
      public float currentSpeed;
      public float topSpeed = 150f;
      public float decellarationSpeed = 10f;
  
  
  void Start()
  {
      rigidbody.centerOfMass = centerOfMass;
      GetPath();
  }
  
  void GetPath()
  {
      Transform[] path_objs = pathGroup.GetComponentsInChildren<Transform>();
  
      for (Transform path_obj in path_objs)
      {
          if (path_obj != pathGroup)
              path[path.length] = path_obj;
      }
  }
  
  
  void Update()
  {
      GetSteer();
      Move();
  }
  
  void GetSteer()
  {
      Vector3 steerVector = transform.InverseTransformPoint(Vector3(path[currentPathObj].position.x, transform.position.y, path[currentPathObj].position.z));
      float newSteer = maxSteer * (steerVector.x / steerVector.magnitude);
      wheelFL.steerAngle = newSteer;
      wheelFR.steerAngle = newSteer;
  
      if (steerVector.magnitude <= distFromPath)
      {
          currentPathObj++;
          if (currentPathObj >= path.length)
              currentPathObj = 0;
      }
  
  }
  
  void Move()
  {
      currentSpeed = 2 * (22 / 7) * wheelRL.radius * wheelRL.rpm * 60 / 1000;
      currentSpeed = Mathf.Round(currentSpeed);
      if (currentSpeed <= topSpeed)
      {
          wheelRL.motorTorque = maxTorque;
          wheelRR.motorTorque = maxTorque;
          wheelRL.brakeTorque = 0;
          wheelRR.brakeTorque = 0;
      }
      else
      {
          wheelRL.motorTorque = 0;
          wheelRR.motorTorque = 0;
          wheelRL.brakeTorque = decellarationSpeed;
          wheelRR.brakeTorque = decellarationSpeed;
      }
  }
  
  }


for more I recommend to read unity documentations in C# (toggle right corner)

Comment
Add comment · 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

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

99 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 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 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 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 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

JS error when transferring from Unity 4 to 5 1 Answer

Help change the subject of this Script 1 Answer

JS to C# converting issues.. 1 Answer

Setting Scroll View Width GUILayout 1 Answer

OnTriggerEnter called only one time?!! 5 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