- Home /
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 :)
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)
Your answer
![](https://koobas.hobune.stream/wayback/20220612094420im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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