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#?

3 Answers

3

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);

}

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

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

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

yeah i guess the value never really goes below zero....im glad you figured this out...you should convert your comment to an answer and accept it to effectively close this question now

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;

   }
 }

Awesome! Thanks guyt and SisterKy for the link!