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
0
Question by Rush-Rage-Games · Jun 13, 2015 at 04:32 AM · c#javascriptgravityc# to javascriptfauxgravity

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

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

2 Replies

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

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!

Comment
Add comment · Show 1 · 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 Rush-Rage-Games · Jun 13, 2015 at 03:20 PM 0
Share

Whaaaa! Thank you so much for this! You are very kind to help out so much. Thank you!

avatar image
0

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.

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

23 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

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

Lower player's health from separate script. 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