- Home /
Help converting C# to Javascript.
I found a script floating around in C# and I need help converting it. I have tried to change as much as I could from the original but It comes up with millions of errors, anyone willing to help?
The Most Messy Script You Will See In Your Existence!!!:
var Started : boolean;
var RotationSpeed : float;
var MaxSpeed : float;
var Accel : float;
var InvAccel : float;
var IdleInvAccel : float;
var SideEnergyLoss : float;
var FrontEnergyLoss : float;
var FrontAngle : float;
var MovingSpeed : float;
var Energy : float;
var MaxEnergy : int;
var Yconstant : float;
var Exploded : boolean;
var Explosion : Transform;
{
this.MaxEnergy == 1000;
}
function Start(){
this.Started = false;
this.Exploded = false;
this.MovingSpeed = (float)0;
this.Energy = (float)this.MaxEnergy;
this.Yconstant = this.transform.position.y;
}
function Update(){
{
if (Input.GetAxis("Vertical"))
{
this.MovingSpeed += this.Accel;
this.MovingSpeed = Mathf.Clamp(this.MovingSpeed, -this.MaxSpeed, this.MaxSpeed);
}
else
{
if (Input.GetAxis("Vertical"))
{
this.MovingSpeed -= this.InvAccel;
this.MovingSpeed = Mathf.Clamp(this.MovingSpeed, -this.MaxSpeed, this.MaxSpeed);
}
else
{
if (this.MovingSpeed >= (float)0)
{
this.MovingSpeed -= this.IdleInvAccel;
this.MovingSpeed = Mathf.Clamp(this.MovingSpeed, (float)0, this.MaxSpeed);
}
else
{
this.MovingSpeed += this.IdleInvAccel;
this.MovingSpeed = Mathf.Clamp(this.MovingSpeed, -this.MaxSpeed, (float)0);
}
}
}
CharacterController characterController = (CharacterController)this.GetComponent(typeof(CharacterController));
characterController.Move(this.transform.forward * this.MovingSpeed * Time.deltaTime);
float y = this.Yconstant;
Vector3 position = this.transform.position;
float num = position.y = y;
Vector3 vector = this.transform.position = position;
if (Mathf.Abs(Input.GetAxis("Horizontal")))
{
this.transform.Rotate(new Vector3((float)0, Input.GetAxis("Horizontal") * this.RotationSpeed * Time.deltaTime, (float)0), Space.Self);
}
}
}
function OnControllerColliderHit(ControllerColliderHit hit)
{
GameObject gameObject = hit.gameObject;
float f = Vector3.Angle(this.transform.forward, hit.point - this.transform.position);
if (gameObject.tag == "Walls")
{
float num = 0f;
float num2 = Mathf.Abs(this.MovingSpeed);
if (Mathf.Abs(f) <= this.FrontAngle)
{
num = this.FrontEnergyLoss;
}
else
{
num = this.SideEnergyLoss;
}
this.energy -= num * (num2 / this.MaxSpeed) * Time.deltaTime;
if (this.energy < (float)0)
{
this.energy = (float)0;
}
if (this.energy == (float)0)
{
{
this.Exploded = true;
this.Started = false;
int i = 0;
Component[] componentsInChildren = this.GetComponentsInChildren(typeof(MeshRenderer));
int length = componentsInChildren.Length;
while (i < length)
{
((MeshRenderer)componentsInChildren[i]).enabled = false;
i++;
}
int j = 0;
Component[] componentsInChildren2 = this.GetComponentsInChildren(typeof(ParticleSystemRenderer));
int length2 = componentsInChildren2.Length;
while (j < length2)
{
((ParticleSystemRenderer)componentsInChildren2[j]).particleSystem.Stop();
j++;
}
UnityEngine.Object.Instantiate(this.PrefabExplosion, this.transform.position, this.transform.localRotation);
}
}
}
}
function getEnergyLevel()
{
return this.Energy;
}
function Main()
{
}
}
The VAR are easy to change. For example:
public bool started = false;
I don't even think you have to use this.started becouse they are in the same script. The functions are a bit trickier because of the different ways of coding. You probably need to change for example
function Start()
to:
void Start()
For the float numbers, ins$$anonymous$$d of (float)0, you can use: 0f
Your main problem is that you haven't changed local variable type definitions. You're still using a lot of lines like this:
int i = 0;
Which is C# syntax, and you should be using Javascript syntax:
var i : int = 0;
As far as I understand it, you also don't need the new
keyword when defining objects in Javascript, though I'm not sure if it's still valid or not (I don't actually use UnityScript, because compared to C# it's a really unsuitable language for game program$$anonymous$$g).
Answer by Imagineer · Apr 06, 2014 at 08:32 AM
I hope this will help you out a bit: If you get any errors, please let me know :)
public bool started = false;
public float rotationSpeed;
public float maxSpeed;
public float accel;
public float invAccel;
public float idleInvAccel;
public float sideEnergyLoss;
public float frontEnergyLoss;
public float frontAngle;
public float movingSpeed = 0f;
public float energy;
public int maxEnergy = 1000;
public float yConstant;
public bool exploded = false;
public Transform explosion;
void Start(){
energy = maxEnergy;
yConstant = this.transform.position.y;
}
void Update()
{
if (Input.GetAxis("Vertical"))
{
movingSpeed += accel;
movingSpeed = Mathf.Clamp(movingSpeed, -maxSpeed, maxSpeed);
}
else if (Input.GetAxis("Vertical"))
{
movingSpeed -= invAccel;
movingSpeed = Mathf.Clamp(movingSpeed, -maxSpeed, maxSpeed);
}
else if (movingSpeed >= 0f)
{
movingSpeed -= idleInvAccel;
movingSpeed = Mathf.Clamp(movingSpeed, 0f, maxSpeed);
}
else
{
movingSpeed += idleInvAccel;
movingSpeed = Mathf.Clamp(movingSpeed, -maxSpeed, 0f);
}
}
}
CharacterController characterController = (CharacterController)this.GetComponent(typeof(CharacterController));
characterController.Move(this.transform.forward * movingSpeed * Time.deltaTime);
float y = yConstant;
Vector3 position = this.transform.position;
float num = position.y = y;
Vector3 vector = this.transform.position = position;
if (Mathf.Abs(Input.GetAxis("Horizontal")))
{
this.transform.Rotate(new Vector3((float)0, Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime, 0f), Space.Self);
}
}
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
GameObject gameObject = hit.gameObject;
float f = Vector3.Angle(this.transform.forward, hit.point - this.transform.position);
if (gameObject.tag == "Walls")
{
float num = 0f;
float num2 = Mathf.Abs(movingSpeed);
if (Mathf.Abs(f) <= frontAngle)
{
num = frontEnergyLoss;
}
else
{
num = sideEnergyLoss;
}
energy -= num * (num2 / maxSpeed) * Time.deltaTime;
if (energy < 0f)
{
energy = 0f;
}
if (energy == 0f)
{
{
exploded = true;
started = false;
int i = 0;
Component[] componentsInChildren = this.GetComponentsInChildren(typeof(MeshRenderer));
int length = componentsInChildren.Length;
while (i < length)
{
((MeshRenderer)componentsInChildren[i]).enabled = false;
i++;
}
int j = 0;
Component[] componentsInChildren2 = this.GetComponentsInChildren(typeof(ParticleSystemRenderer));
int length2 = componentsInChildren2.Length;
while (j < length2)
{
((ParticleSystemRenderer)componentsInChildren2[j]).particleSystem.Stop();
j++;
}
UnityEngine.Object.Instantiate(this.PrefabExplosion, this.transform.position, this.transform.localRotation);
}
}
}
}
void getEnergyLevel()
{
return energy;
}
}
Ok, this is a little awkward. :/ $$anonymous$$y script i posted above was my attempt at changing it to js not back to c#. shoulda kinda mentioned that (Oops).
Ah okay. Then you were on the right track i suppose.. But I don't know much about js.. I saw you made some spelling errors, so maybe you can have a look at it. Can you post the errors that you get??
Here is an error (more will come): Line 17 : expecting :, found ';'
where does this ':' belong?
That's probably because of the syntax. Use something like: var i : int = 0;
I fixed my problem and I encountered another million errors, So I have just decided to give up this script and find another method. This script was supposed to control a basic hover car (which is the one stupid thing I cant find any info on anywhere!). Thanks for helping me Imagineer :) (Its awesome when people on unity answers even consider helping)
Your answer
Follow this Question
Related Questions
Need help converting javascript to c# script 2 Answers
Can someone convert this into javascript? :) 1 Answer
How can I change this C# code into Javascript? 0 Answers
Help me Convert JS to C# 2 Answers
Why not Boo? 1 Answer