- Home /
How can I change this C# code into Javascript?
I'm pretty new to Unity and I'm trying to convert the following code into Javascript (it's currently C#) so I can understand it. Can someone help me out?
using UnityEngine; using System.Collections;
public class CarControl : MonoBehaviour
{
public GameObject RightWheel;
public GameObject LeftWheel;
void Start()
{
Vector3 center = rigidbody.centerOfMass;
center.y -= 1;
rigidbody.centerOfMass = center;
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw("Horizontal");
JointMotor motor = LeftWheel.hingeJoint.motor;
motor.force = (h!=0?1:0) * 30;
motor.freeSpin = true;
motor.targetVelocity = -h * 4000;
LeftWheel.hingeJoint.useMotor = h != 0;
LeftWheel.hingeJoint.motor = motor;
RightWheel.hingeJoint.useMotor = h != 0;
RightWheel.hingeJoint.motor = motor;
rigidbody.AddRelativeTorque(0, 0, -h * 3000);
// flip the car
if (Input.GetButton("Fire1"))
{
float deltaAngle = Mathf.DeltaAngle(transform.eulerAngles.z, 0);
if (Mathf.Abs(deltaAngle) > 10)
{
rigidbody.AddTorque(0, 0, deltaAngle * deltaAngle * deltaAngle, ForceMode.VelocityChange);
}
}
}
}
The changes needed are fairly simple...how variables are declared and how functions are declared. Learning how to do basic translation between the two languages is a good skill to have and almost a necessity when researching topics on UA. I suggest you take a stab at it yourself. If you get stuck, post your code. Here is a reference for the syntax differences:
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
Good find there, but no one asking how to convert a script from C# to JS really wants to understand it, they just want something they can copypaste into their existing copypasted script that they don't understand either.
@$$anonymous$$iloblargh That is such a wrong approach it makes me sick! Learning C# compared to javascript (or vice versa) is easy. It's just different syntax. I learned to convert from and to C# by just reading a C# script.
I'm sorry to say, but if you're not able to "understand" converting between the two languages, then you probably shouldn't be!
Your answer
Follow this Question
Related Questions
Help converting C# to Javascript. 1 Answer
Convert Javascript class to a string 0 Answers
Can someone convert this into javascript? :) 1 Answer
Need help converting javascript to c# script 2 Answers
C# into JavaScript 1 Answer