- Home /
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#?
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);
}
oh, js has do {} while (); ? I thought that was a syntax-difference...? Greetz, $$anonymous$$y.
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#).
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;
} }
Your answer
Follow this Question
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