- Home /
JS to C# Translation?
Hey guys, just a quick question, I need to translate the following script for line creation, it's written in Java Script but I'm much more fluent in C#, could someone help me out?
I tried the online conversion system but obviously it can't handle var's.
pragma strict
var pos1 : Vector3; var pos2 : Vector3; var objectHeight = 2.0; // 2.0 for a cylinder, 1.0 for a cube
function Update () {
 if (Input.GetMouseButtonDown(0)) {
    pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
     pos1 = Camera.main.ScreenToWorldPoint(pos1); 
     pos2 = pos1;
 
 }
 
 if (Input.GetMouseButton(0)) {
    pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
     pos2 = Camera.main.ScreenToWorldPoint(pos2); 
 
 }
 
 if (pos2 != pos1) {
    var v3 = pos2 - pos1;
     transform.position = pos1 + (v3) / 2.0;
     transform.localScale.y = v3.magnitude/objectHeight;
     transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
 }
}
Thanks in advanced guys!
http://www.m2h.nl/files/js_to_c.php , just go to this link, paste your js code and get C# code.
Thanks sriram, but as I said, I tried that already but it doesn't handle var's, and that's my biggest problem
Answer by sriram90 · May 20, 2014 at 12:08 PM
Okay .. here you go ..
 using UnityEngine;
 using System.Collections;
 
 public class SampleClass: MonoBehaviour 
 {
     Vector3 pos1; 
     Vector3 pos2; 
     float objectHeight= 2.0f; // 2.0f for a cylinder, 1.0f for a cube
     
     void  Update ()
     {
         
         if (Input.GetMouseButtonDown(0)) 
         {
             pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
             pos1 = Camera.main.ScreenToWorldPoint(pos1); 
             pos2 = pos1;
         }
         
         if (Input.GetMouseButton(0)) 
         {
             pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
             pos2 = Camera.main.ScreenToWorldPoint(pos2); 
             
         }
         
         if (pos2 != pos1) 
         {
             Vector3 v3= pos2 - pos1;
             transform.position = pos1 + (v3) / 2.0f;
             transform.localScale = new Vector3(transform.localScale.x, v3.magnitude/objectHeight, transform.localScale.z);
             transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
         }
     }
     
     [RPC]
     void  Test ()
     {}
 }
Answer by softrare · May 20, 2014 at 12:10 PM
Use http://www.m2h.nl/files/js_to_c.php like sriram suggested and just replace FIXME_VAR_TYPE with the keyword var .
So this:
 FIXME_VAR_TYPE objectHeight= 2.0f;
Can become
 var objectHeight= 2.0f;
or
 float objectHeight= 2.0f;
Both is correct C# code. I used the converter many times, without problems.
Thanks, that's helpful to know, I didn't know var worked for c# too, I'll remember that. I upvoted your answer, I can't accept two answers though and the sriram posted a correct answer first so I just accepted that. But I really appreciate your time and effort, thanks!
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Render only on specific times of year 1 Answer
Distribute terrain in zones 3 Answers
Please Help Convert js to c# 1 Answer
Procedural Generation 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                