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 davidflynn2 · Apr 24, 2013 at 03:49 PM · c#js to c#

Convert JS to C#

I have been working on converting this Js to C# but i got a few last bugs I cant get out some one please help me.

JS version

 var driveForce: float;
 
 var hoverHeight: float;
 var thrustForce: float;
 var steerForce: float;
 var steerPosZ: float;
 var damping: float;
 
 var thrusters: Vector3[];
 
 
 
 
 function FixedUpdate () {
     var hit: RaycastHit;
     
     for (i = 0; i < thrusters.Length; i++) {
         var wdThruster: Vector3 = transform.TransformPoint(thrusters[i]);
         
         if (Physics.Raycast(wdThruster, -transform.up, hit)) {
             var discrep: float = hoverHeight - hit.distance;
             var upVel: float = rigidbody.GetRelativePointVelocity(wdThruster).y;
             rigidbody.AddForceAtPosition(transform.up * (thrustForce * discrep - upVel * damping), wdThruster);
         }
     }
     
     var fwd: float = Input.GetAxis("Vertical");
     rigidbody.AddForce(transform.forward * (driveForce * fwd));
     
     var steer: float = -Input.GetAxis("Horizontal");
     rigidbody.AddForceAtPosition(transform.right * (steerForce * steer), transform.TransformPoint(Vector3.forward * steerPosZ));
 }
 
 
 function OnDrawGizmos() {
     for (i = 0; i < thrusters.Length; i++) {
         Gizmos.DrawWireSphere(transform.TransformPoint(thrusters[i]), 0.1);
     }
 }


C# version so far:

 using UnityEngine;
 using System.Collections;
 
 public class HoverBoard : MonoBehaviour {
 
 float driveForce;
 
 float hoverHeight;
 float thrustForce;
 float steerForce;
 float steerPosZ;
 float damping;
 
 Vector3[] thrusters;
 
 
 
 
 void  FixedUpdate (){
     RaycastHit hit;
     
     for (i = 0; i < thrusters.Length; i++) {
         Vector3 wdThruster = transform.TransformPoint(thrusters[i]);
         
         if (Physics.Raycast(wdThruster, -transform.up, hit)) {
             float discrep = hoverHeight - hit.distance;
             float upVel = rigidbody.GetRelativePointVelocity(wdThruster).y;
             rigidbody.AddForceAtPosition(transform.up * (thrustForce * discrep - upVel * damping), wdThruster);
         }
     }
     
     float fwd = Input.GetAxis("Vertical");
     rigidbody.AddForce(transform.forward * (driveForce * fwd));
     
     float steer = -Input.GetAxis("Horizontal");
     rigidbody.AddForceAtPosition(transform.right * (steerForce * steer), transform.TransformPoint(Vector3.forward * steerPosZ));
 }
 
 
 void  OnDrawGizmos (){
     for (i = 0; i < thrusters.Length; i++) {
         Gizmos.DrawWireSphere(transform.TransformPoint(thrusters[i]), 0.1f);
     }
 }
 }




I am getting there errors :

error CS0103: The name i' does not exist in the current context error CS0165: Use of unassigned local variable hit'

error CS1502: The best overloaded method match for UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, out UnityEngine.RaycastHit)' has some invalid arguments error CS1620: Argument #3' is missing `out' modifier

Comment
Add comment · Show 1
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 AlucardJay · Apr 24, 2013 at 04:15 PM 2
Share

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/

1 Reply

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

Answer by Dave-Carlile · Apr 24, 2013 at 03:53 PM

You need to declare the variable i. For for loops, this is often done like this:

 for (int i = 0; ...)


For the raycast, you need to add an out qualifier when passing hit, so the compiler knows the variable will be filled in by the function, not by you. This should fix the remaining 3 errors.

 if (Physics.Raycast(wdThruster, -transform.up, out hit))  <-- add "out"



 
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 davidflynn2 · Apr 24, 2013 at 04:12 PM 0
Share

Ok its giving me one last error I am not sure about thanks for your help.

NullReferenceException: Object reference not set to an instance of an object HoverBoard.OnDrawGizmos () (at Assets/HoverBoard.cs:41) UnityEditor.DockArea:OnGUI()

avatar image davidflynn2 · Apr 24, 2013 at 04:28 PM 0
Share

NV$$anonymous$$ glitch in Unity no erros thanks so much for your help.

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

13 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

Related Questions

Multiple Cars not working 1 Answer

problem sending information from js script to c# script 1 Answer

Please Help Convert js to c# 1 Answer

Spawning portal 1 Answer

Making a Jetpack 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