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#?
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);
}
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;
}
}