- Home /
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
To start with, you left out the Start function and the global variables. Translate the code exactly; don't stuff everything into Update.
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. ;)
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;
Your answer
![](https://koobas.hobune.stream/wayback/20220613100457im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script problems 1 Answer
C# to JS small error 2 Answers
On raycast hit spawn object and parent 1 Answer