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 TH3HCK3ABL3 · Jul 26, 2013 at 08:54 PM · c#jumping

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.

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 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.

Comment
Add comment · Show 2 · 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 TH3HCK3ABL3 · Jul 26, 2013 at 09:09 PM 0
Share

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:

Pastebin Link

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!

avatar image perchik · Jul 26, 2013 at 09:13 PM 0
Share

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?

avatar image
1

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;
     }
Comment
Add comment · Show 6 · 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 TH3HCK3ABL3 · Jul 26, 2013 at 09:19 PM 0
Share

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.

avatar image perchik · Jul 26, 2013 at 09:20 PM 0
Share

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

avatar image TH3HCK3ABL3 · Jul 26, 2013 at 09:23 PM 0
Share

I love this community. Thanks both of you.

avatar image gregzo · Jul 26, 2013 at 09:47 PM 0
Share

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.

avatar image perchik · Jul 26, 2013 at 09:48 PM 0
Share

in javascript, var is an an anonymous type as well

Show more comments

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to get around adding loads of "if" commands in my script 2 Answers

Have null errors 1 Answer

Error: error CS0029: Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject 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