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 smirlianos · Jul 22, 2013 at 09:48 AM · c#javascriptraycastconvertc# to javascript

Convert c# to Js?

Hello,

I have this C# script. Can someone translate it to Js? I'm afraid I can't do it myself :(

     // calculate desired camera position
      
                 Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));
      
          
      
                 // check for collision using the true target's desired registration point as set by user using height
      
                 RaycastHit collisionHit;
      
                 Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);
      
          
      
                 // if there was a collision, correct the camera position and calculate the corrected distance
      
                 bool isCorrected = false;
      
                 if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
      
                 {
      
                     position = collisionHit.point;
      
                     correctedDistance = Vector3.Distance(trueTargetPosition, position);
      
                     isCorrected = true;
      
                 }
      
                
      
                 // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
      
                 currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
      
          
      
                 // recalculate position based on the new currentDistance
      
                 position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight, 0));
      
      
                 transform.position = position;
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 Graham-Dunnett ♦♦ · Jul 22, 2013 at 09:49 AM 0
Share

What goes wrong when you try and convert it yourself? Why are you trying to convert it?

avatar image softrare · Jul 22, 2013 at 10:38 AM 0
Share

Why would you want to convert the script in the first place? C# is a much more professional and maintainable language.

avatar image smirlianos · Jul 22, 2013 at 10:39 AM 0
Share

I want to convert it because I work with Js

1 Reply

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

Answer by smtabatabaie · Jul 22, 2013 at 09:52 AM

most of it are the same . you just need to change the definition of the variables . in C# it's like

 Vector3 position = ...

but in javascript you can do this like :

 var position:Vector3;

or simply :

 var position;

Comment
Add comment · Show 4 · 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 smirlianos · Jul 22, 2013 at 10:15 AM 1
Share

Thanks! :)

avatar image clunk47 · Jul 22, 2013 at 03:48 PM 1
Share

This should definitely point you in the right direction. I started w/ JS, then had to learn how to convert all my code to C# because I simply wanted to know C#, and it was pretty easy. Once you get the hang of it, it's second nature.

avatar image AlucardJay · Jul 22, 2013 at 04:02 PM 1
Share

From my notepad of generic posts :

Here's some links I found useful in converting between C# and JS :

  • http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

  • http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

  • http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/

avatar image clunk47 · Jul 22, 2013 at 04:09 PM 0
Share

@alucardj Thank you sir!

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

21 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

Related Questions

Changing Value in JavaScript from C# 1 Answer

Is there eval() for C#? 2 Answers

Create new plane and Raycast in C#? 1 Answer

Convert this string formatting from C# to JS 1 Answer

Anyone able to convert this script to c#? 3 Answers


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