- Home /
This is a sloppy conversion from javascript to C#, I cannot seem to get it working...
So this is my current revision as I try to rewrite unityscript to C#, can someone give me a detailed explanation of why it's wrong? Possibly how to fix it?
for (var t = 0.0; t < 1.0);
t += Time.deltaTime;
transform.position = Vector3.Lerp (orgPos, dstPos, t);
transform.rotation = Quaternion.Slerp (orgRot, dstRot, t);
yield; // return here next frame
I've never used the for statement, so I have no idea of the correct syntax when doing something like this. Any 2 cents you put in is appreciated, thanks again.
Answer by perchik · Jul 26, 2013 at 08:59 PM
Ok. You should really try to learn some programming on your own before trying to convert other games without knowing what you're doing.
With that said, a for loop looks like this, and has three parts:
for ( var t=0; t< 1.0; t=t+1){
}
The first part var t=0;
tells the computer what to start at; The second part t<1.0;
tells the computer when to stop; The third part, the piece you are missing, t=t+1
, tells the computer how to increment the variable.
EDIT: C# does allow you to place the third part inside of the loop. Your original code, that looks like this, is fine:
for (var t = 0.0; t < 1.0;)
{
t += Time.deltaTime;
}
That's how to use a for loop. Unfortunately, I'm not entirely sure what your code is trying to do.
Now, since you've never seen a for loop before, it seems like you're probably pretty new to programming. I would suggest CodeAcademy as a great tool to learn javascript programming. Javascript can be used in unity, but more than that, it will teach you the right concepts. Once you're really proficient in javascript, then converting to C# isn't too bad.
Thanks for your advice, but I'm not converting another game, I am re-writing some Javascript in C# for my game. The rest of the script won't fit here so I'll pastebin it if you want to look. I do know some program$$anonymous$$g, I was just unaware of the "for" loop and how to write it in C#, because writing this in javascript looks like:
for (var t: float = 0.0; t < 1.0; ) {
So I just didn't know how to write it in C#, thank you again for the help. Here's the rest of the script:
I've also been using Codeacademy, it's quite nice. I just started with C# so I keep trying to write in C#. Thanks again for your answer!
That's a sloppy way to write the for loop in javascript too. You'd probably be better off using a while loop.
You're also going to run into problems using Yield in C# outside of an IEnumerator block.
$$anonymous$$ay I ask what you are trying to actually do with this snippet?
Answer by gregzo · Jul 26, 2013 at 09:15 PM
Hi! Here's the commented, corrected c# port :
for (float t = 0f; t < 1f;) //In C#, var has a different meaning. Use the type to declare a variable. Also, 0f means 0 as a float. Also, no ";" here please! And brackets are nice...
{
t += Time.deltaTime;
transform.position = Vector3.Lerp (orgPos, dstPos, t);
transform.rotation = Quaternion.Slerp (orgRot, dstRot, t);
yield return null; // JS doesn't need you to specify what to return ( compiler doest it for you ). In C#, yield return null in Coroutines, and yield return StartCoroutine ( FooRoutine() ) instead of yield FooRoutine;
}
Oooh, thanks for the answer to yield. And yea haha, I understand float just enough to get by. If you go up to to comments on the answer above you'll find the pastebin link if you want to look at all of the source for this.
False. Var means the exact same thing in C# You don't have to call it a float, but you do need a semicolon after the condition
for (var i = 0; i < 5; )
{
i+=1;
}
is correct
Hmmm, sorry to disagree, but in c# var is a way of declaring an anonymous type. In UnityScript, it is required to declare a variable. Quite different, really.