Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 cy123 · Aug 07, 2011 at 08:17 AM · conversionlanguage-comparison

Need help with C# to Unityscript

Hi, I've just started using Unity for a month and I'm Using Unityscripts as my my scripting choice. I found a script in the net which I desperately tried converting to .js with no avail. This is the script :

 using UnityEngine;

using System.Collections;

public class RotationScript : MonoBehaviour { bool rotate;

 private float fromAngle;
 private float toAngle = 90f;

 private float yVel = 0.0f;
 public float smooth = 0.3F;
 private float yAngle;


 void FixedUpdate() 
 {

     if (Input.GetKeyDown(KeyCode.A))
     {
         rotate = true;
     }

     doRotation(); // see? OUTSIDE the if() block

  }

  void doRotation()
  {

      do
      {
         //set fromAngle as the y eulerAngles of the current transform.
         fromAngle = transform.eulerAngles.y;

         //set toAngle to fromAngle + 90
         toAngle = fromAngle + 90;

         //set yAngle to smooth from fromAngle to toAngle, over 5 seconds
         yAngle = Mathf.SmoothDampAngle(fromAngle, toAngle, ref yVel, 5 * Time.deltaTime, Mathf.Infinity, Time.deltaTime);

         //rotate
         transform.eulerAngles = new Vector3(0, yAngle,0);

         rotate = false;

    } while (rotate == true);
  }

}

Thanks a bunch for helping and if possible, can anyone give me pointers on difference between .js and C#?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GuyTidhar · Aug 07, 2011 at 09:06 AM

Call the javascript file by the name of the class (RotationScript).

This is your class in javascript:

 public var rotate : boolean;
 public var smooth : float = 0.3F; 
 
 private var fromAngle : float;
 private var toAngle : float = 90;
 private var yVel : float = 0.0f; 
 private var yAngle : float;
 
 function FixedUpdate() 
 {
     if (Input.GetKeyDown(KeyCode.A))
     {
         rotate = true;
     }
 
     doRotation(); // see? OUTSIDE the if() block
  }
 
 function doRotation()
 {
 
  do
  {
     //set fromAngle as the y eulerAngles of the current transform.
 
     fromAngle = transform.eulerAngles.y;
 
     //set toAngle to fromAngle + 90
     toAngle = fromAngle + 90;

     //set yAngle to smooth from fromAngle to toAngle, over 5 seconds
 
     yAngle = Mathf.SmoothDampAngle(fromAngle, toAngle, yVel, 5 * Time.deltaTime, Mathf.Infinity, Time.deltaTime);
 
     //rotate
     transform.eulerAngles = new Vector3(0, yAngle,0);
 
     rotate = false;
 
  } while (rotate == true);
 
 }
Comment
Add comment · Show 3 · 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 SisterKy · Aug 07, 2011 at 09:15 AM 0
Share

oh, js has do {} while (); ? I thought that was a syntax-difference...? Greetz, $$anonymous$$y.

avatar image SisterKy · Aug 07, 2011 at 09:16 AM 0
Share

ah, sorry... a quick google found the answer http://answers.unity3d.com/questions/24820/is-there-a-do-while-equivalent-in-java-script.html?sort=newest

avatar image GuyTidhar · Aug 07, 2011 at 09:22 AM 0
Share

You can also do the following in js: transform.eulerAngles.y = yAngle; (You don't need to set a new vector3 like in c#, as the read only is only valid in c#).

avatar image
0

Answer by SisterKy · Aug 07, 2011 at 09:05 AM

http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

Just a quick shot... not sure if I did this correct... esp. the do-while...? Just try it... or someone else can take this as a start with the nitty-gritty variable-declaration out of the way...

var rotate : boolean;

private var fromAngle : float; private var toAngle : float = 90f;

private var yVel : float = 0.0f; var smooth : float = 0.3F; private var float yAngle;

function FixedUpdate() {

 if (Input.GetKeyDown(KeyCode.A))
 {
     rotate = true;
 }

 DoRotation();

}

function DoRotation() {

  while (rotate == true)
  {
     //set fromAngle as the y eulerAngles of the current transform.
     fromAngle = transform.eulerAngles.y;

     //set toAngle to fromAngle + 90
     toAngle = fromAngle + 90;

     //set yAngle to smooth from fromAngle to toAngle, over 5 seconds
     yAngle = Mathf.SmoothDampAngle(fromAngle, toAngle, ref yVel, 5 * Time.deltaTime, Mathf.Infinity, Time.deltaTime);

     //rotate
     transform.eulerAngles = new Vector3(0, yAngle,0);

     rotate = false;

} }

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
avatar image
0

Answer by cy123 · Aug 07, 2011 at 09:21 AM

Awesome! Thanks guyt and SisterKy for the link!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Script translation 2 Answers

Slerp for x rotation only? 1 Answer

Js to c# conversion 1 Answer

Prevent implicit cast from Vector3-Vector2 1 Answer

I have global rotations for an object, how do I convert to local rotations? 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