- Home /
Need help converting Javascript to C#
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
private var spawnPos : Vector3;
private var spawnRot : Quaternion;
public Transform[] patrolPoints;
function Start ()
{
spawnPos = transform.position;
spawnRot = transform.rotation;
}
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
renderer.material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
patrol();
}
if (Distance < attackRange)
{
renderer.material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
function patrol ()
{
transform.position = spawnPos;
transform.rotation = spawnRot;
}
Hi i was wondering how much of this code would i have to change to make it into C#. i need this code in C# for technical reasons with my game, but it would be greatly appreciated if anyone could point me in the right direction. i tried to go lone walk but had trouble making my variable work in C#, sorry im a complete noob at C# so any help would be greatly appreciated. thanks.
Answer by Kiwasi · Oct 15, 2014 at 06:08 PM
Change var to the type.
Remove the : type
Put a f after your floats
Change function to void
Answer by Bunny83 · Oct 15, 2014 at 05:53 PM
You only need 4 steps:
Download ILSpy
build your game with the javascript as standalone build.
Open the UnityScript assembly in the data folder of your build in ILSpy and select C# as target language.
Copy the code over into a c# script and delete the US script
That's all
But you'll still be just as much of a complete noob at C# as you were when you started :(
@tanoshimi: Well, not necessarily. If you actually want to learn C# this is a great way to see how a US script would look in IL or in C#. But only if you actually try to understand what's happening.
However the way the question was asked i don't have the feeling that "learning C#" was the goal here... ;)
Answer by SnotE101 · Oct 15, 2014 at 05:57 PM
Try this free site. Just copy your JavaScript code and it will return in c#. http://www.m2h.nl/files/js_to_c.php
Your answer
Follow this Question
Related Questions
Converting a javascript to C# 2 Answers
Help with conversion from javascript to c# 3 Answers
press e to speek -1 Answers
Snow based questions 2 Answers
Convert Javascript to C# 1 Answer