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;
}
To start with, you left out the Start function and the global variables. Translate the code exactly; don't stuff everything into Update.
ā Eric5h5I 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. ;)
ā Eric5h5