Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MrRandomFella · Apr 06, 2014 at 07:25 AM · c#javascriptconvertracinghovercraft

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()
     {
     }
 }
 
Comment
Add comment · Show 2
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 Imagineer · Apr 06, 2014 at 08:32 AM 0
Share

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

avatar image Hoeloe · Apr 06, 2014 at 09:21 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 }
Comment
Add comment · Show 5 · 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 MrRandomFella · Apr 06, 2014 at 08:53 AM 0
Share

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

avatar image Imagineer · Apr 06, 2014 at 09:06 AM 0
Share

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

avatar image MrRandomFella · Apr 06, 2014 at 09:25 AM 0
Share

Here is an error (more will come): Line 17 : expecting :, found ';'

  • where does this ':' belong?

avatar image Imagineer · Apr 06, 2014 at 09:30 AM 0
Share

That's probably because of the syntax. Use something like: var i : int = 0;

avatar image MrRandomFella · Apr 06, 2014 at 09:48 AM 0
Share

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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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