- Home /
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;
What goes wrong when you try and convert it yourself? Why are you trying to convert it?
Why would you want to convert the script in the first place? C# is a much more professional and maintainable language.
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;
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.
From my notepad of generic posts :
Here's some links I found useful in converting between C# and JS :
Your answer
Follow this Question
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