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 Tofudude624 · Apr 17, 2013 at 06:59 AM ·

Converting C# to JS...

Hi there. I tried converting this script C# into JS (that is what I am more familiar with), but I ran into some problems. What did I do wrong? Thanks

Original C#

 public class SinWaveMover : MonoBehaviour
 
 {
 
     void Start()
 
     {
 
         m_centerPosition = transform.position;
 
     }
 
     
 
     void Update()
 
     {
 
         float deltaTime = Time.deltaTime;
 
         
 
         // Move center along x axis
 
         m_centerPosition.x += deltaTime * m_speed;
 
         
 
         // Update degrees
 
         float degreesPerSecond = 360.0f / m_period;
 
         m_degrees = Mathf.Repeat(m_degrees + (deltaTime * degreesPerSecond), 360.0f);
 
         float radians = m_degrees * Mathf.Deg2Rad;
 
         
 
         // Offset by sin wave
 
         Vector3 offset = new Vector3(0.0f, m_amplitude * Mathf.Sin(radians), 0.0f);
 
         transform.position = m_centerPosition + offset;
 
     }
 
     
 
     Vector3 m_centerPosition;
 
     float m_degrees;
 
     
 
     [SerializeField]
 
     float m_speed = 1.0f;
 
     
 
     [SerializeField]
 
     float m_amplitude = 1.0f;
 
     
 
     [SerializeField]
 
     float m_period = 1.0f;
 
 }



My JS

 function Update(){
 
         var m_centerPosition = transform.position;
 
         deltaTime = Time.deltaTime;
 
         // Move center along x axis
         m_centerPosition.x += deltaTime * m_speed;
 
         // Update degrees
         float degreesPerSecond = 360.0f / m_period;
         m_degrees = Mathf.Repeat(m_degrees + (deltaTime * degreesPerSecond), 360.0f);
 
  
 
        radians = m_degrees * Mathf.Deg2Rad;
         
         // Offset by sin wave
         Vector3 offset = new Vector3(0.0f, m_amplitude * Mathf.Sin(radians), 0.0f);
         transform.position = m_centerPosition + offset;
         Vector3 m_centerPosition;
         
         float m_degrees;
         [SerializeField]
         float m_speed = 1.0f;
         [SerializeField]
         float m_amplitude = 1.0f;
         [SerializeField]
         float m_period = 1.0f;
 
 }

Comment
Add comment · Show 2
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 Eric5h5 · Apr 17, 2013 at 07:01 AM 0
Share

To start with, you left out the Start function and the global variables. Translate the code exactly; don't stuff everything into Update.

avatar image Eric5h5 · Apr 17, 2013 at 07:51 AM 0
Share

I made that a comment because it doesn't really answer the question (as far as I know); I just saw a couple of things that immediately jumped out and left it at that. So don't convert to an answer again please. ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by deltamish · Apr 18, 2013 at 07:00 AM

Hi i have edited the script a bit hope it helps

  //Notes///
 
 ///Note in C# float x; //this is private variable i.e. it doent show up in inspector to make it show up use public float x;
 
 //in JavaScript var x:float;  //the same goes for all Transform,int,Vector3,Vector2 ..etc        Note this is already a public variable so "var" i as same as typing "public var"  
 
 //Inorder to make it into a private one just use private var
 
 
 function Start()
  
 {
  
 m_centerPosition = transform.position;
  
 }
  
  
  
 function Update()
  
 {
  
 //var deltaTime:float = Time.deltaTime;//notice the change i made
  
  
  
 // Move center along x axis
  
 //m_centerPosition.x += deltaTime * m_speed;// WHY DO YOU want to define a variable and use it instead directly use
 
 m_centerPosition.x += Time.deltaTime * m_speed;
  
  
  
 // Update degrees
  
 var degreesPerSecond:float = 360.0f / m_period;
  
 m_degrees = Mathf.Repeat(m_degrees + (deltaTime * degreesPerSecond), 360.0f);
  
 var radians:float = m_degrees * Mathf.Deg2Rad;
  
  
  
 // Offset by sin wave
  
 var offset:Vector3 = Vector3(0.0f, m_amplitude * Mathf.Sin(radians), 0.0f);
  
 transform.position = m_centerPosition + offset;
  
 }
  
  
 var m_centerPosition:Vector3;
  
 var m_degrees:float;
  
  
 @SerializeField
  
 private var m_speed:float = 1.0f;
  
  
  
 @SerializeField
  
 private var m_amplitude:float = 1.0f;
  
  
  
 @SerializeField
  
 private var m_period:float = 1.0f;
  
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

13 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

Related Questions

Script problems 1 Answer

C# to JS small error 2 Answers

On raycast hit spawn object and parent 1 Answer

How to set y-axis limits to mouselook script 1 Answer

Problem with Pause and Pause Menu Script 1 Answer


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