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
-1
Question by S_Byrnes · May 20, 2014 at 11:54 AM · c#translation

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!

Comment
Add comment · Show 3
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 sriram90 · May 20, 2014 at 11:58 AM 0
Share

http://www.m2h.nl/files/js_to_c.php , just go to this link, paste your js code and get C# code.

avatar image S_Byrnes · May 20, 2014 at 12:03 PM 0
Share

Thanks sriram, but as I said, I tried that already but it doesn't handle var's, and that's my biggest problem

avatar image sriram90 · May 20, 2014 at 12:09 PM 0
Share

Do you need only var datatype ?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 ()
     {}
 }
Comment
Add comment · Show 1 · 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 S_Byrnes · May 20, 2014 at 12:15 PM 0
Share

Thanks a lot, that worked, I really appreciate that!

avatar image
1

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.

Comment
Add comment · Show 2 · 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 S_Byrnes · May 20, 2014 at 12:16 PM 0
Share

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!

avatar image softrare · May 21, 2014 at 12:34 PM 0
Share

You can accept multiple answers AFAI$$anonymous$$.

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

22 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

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


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