- Home /
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
Here's some links I found useful in converting between C# and JS :
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"
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()
NV$$anonymous$$ glitch in Unity no erros thanks so much for your help.
Your answer
Follow this Question
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