- Home /
Trying to figure out how to translate these gravity script to Javascript
Hey guys, I'm trying to get back into Unity but need some help getting started. One of the core elements of a project I want to start is faux gavity (think Mario Galaxy). I found some really nice scripts to use as a foundation but the problem is that they are in C# (the only language I know is Javascript).
So could I get some help basicaly breaking down what these simple scripts mean in C# (so I can then try to recreate it in Javascript)? Thanks for your help!
Attractor
public class FauxGravityAttractor : MonoBehaviour {
public float gravity = -12;
public void Attract(Transform body) {
Vector3 gravityUp = (body.position - transform.position).normalized;
Vector3 localUp = body.up;
body.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
Quaternion targetRotation = Quaternion.FromToRotation(localUp,gravityUp) * body.rotation;
body.rotation = Quaternion.Slerp(body.rotation,targetRotation,50f * Time.deltaTime );
}
}
Body
public class FauxGravityBody : MonoBehaviour {
public FauxGravityAttractor attractor;
private Transform myTransform;
void Start () {
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
myTransform = transform;
}
void FixedUpdate () {
if (attractor){
attractor.Attract(myTransform);
}
}
}
Answer by Eno-Khaon · Jun 13, 2015 at 08:17 AM
Translating from C# to Javascript really is quite easy to do. It's almost entirely a matter of simple semantic changes.
First off, C# requires a class declaration at the beginning, while Javascript does not. You can simply remove the "public class" lines from both scripts and the curly braces associated with them. In the event you declare a class within your class, or wish to change the script yours is derived from (Javascript uses MonoBehaviour by default), you'll simply need to rearrange the formatting. In this case, however, that is not actually necessary.
// C#
public class MyScript: MonoBehaviour
{
// Script goes here
}
// Javascript
// Note that a class declaration cares a little more about whether it's public or private. This is the exception, if anything.
public class MyScript extends MonoBehaviour
{
// Script goes here
}
Next, Javascript variables default to public. C# variables default to private. This means that a "public float varName" variable would become "var varName: float" where the public variable status is assumed. If the variable has no listing, such as "float varName" then you would instead translate that to "private var varName: float". Additionally, any float values typed in manually must be appended with the letter "f" in order to signify that they are float values. An additional note is that boolean variables use a different naming convention, being shortened to "bool" in C#.
// C#
public float myPublicFloat = 3.0f;
float myPrivateFloat;
bool boolExample;
// Javascript
var myPublicFloat: float = 3.0;
private var myPublicFloat: float;
private var boolExample: boolean;
The same goes for functions. The function declaration begins with the return type (void being none), so "public void Attract()" would become "function Attract()". If the function had a return type, such as "float TimesTwo()", this would be written as "function TimesTwo(): float" instead.
// C#
public float TimesTwo(float input)
{
return input * 2.0f;
}
// Javascript
function TimesTwo(input: float): float
{
return input * 2.0;
}
The last thing you should probably need to handle this conversion is a detail on how Unity's "GetComponent()" is handled. This is where things become a bit more specific, however, so I'll save the introduction this time and offer a rather common situation instead:
// C#
Rigidbody rb = gameObject.GetComponent<Rigidbody>();
// Javascript
private var rb: Rigidbody = gameObject.GetComponent(Rigidbody);
Once you have those variables, they'd both be used in generally the same way. Calling common functions will often be handled the same way, but there is one more little quirk to keep in mind to start: "new" -- You'll see this come up frequently in C# and it's simply the format to follow when assigning a new, complex value type to something that doesn't already have one. To give an example:
// C#
public Vector3 weirdVector = new Vector3(0.0237f, 1.296f, -22.779f);
// Javascript
var weirdVector: Vector3 = Vector3(0.0237, 1.296, -22.779);
Well, at the very least, I think this should be good for a start. From here, I would recommend taking a look at Unity's Scripting API pages (many of the links I put in this post) and comparing the C# and Javascript examples. You may find more in common between them than you might expect!
Whaaaa! Thank you so much for this! You are very kind to help out so much. Thank you!
Answer by rajavamsidhar_gvs · Jun 13, 2015 at 07:02 PM
if u want to convert that script into javascript just go this site
i hope it will help you.
Your answer
Follow this Question
Related Questions
Convert c# to Js? 1 Answer
Distribute terrain in zones 3 Answers
Having a problem translating part of a script which uses C# delegates into JS 0 Answers
Changing Value in JavaScript from C# 1 Answer