- Home /
can i convert c sharp to javascript
Can I convert a C# script to a javascript. This script:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Targetting : MonoBehaviour { public List<Transform> targets; public Transform selectedTarget; private Transform myTransform;
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy)
{
targets.Add(enemy);
}
private void SortTargetsByDistance()
{
targets.Sort(delegate(Transform t1, Transform t2){
return Vector3.Distance(t1.position,myTransform.position).CompareTo(Vector3.Distance(t2.position,myTransform.position));
});
}
private void TargetEnemy()
{
if(selectedTarget == null)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
else
{
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1)
{
index++;
}
else
{
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget()
{
selectedTarget.renderer.material.color = Color.red;
}
private void DeselectTarget()
{
selectedTarget.renderer.material.color = Color.blue;
selectedTarget = null
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
}
I'm confused by what you mean by "not all converts into code". If you provide a detail of which idiom you're having problems translating, it'll be easier for people to help you.
Sorry for that but Peter G has solved that and my english is $%#$^ again sorry for that :(
Answer by Julian-Glenn · Aug 08, 2010 at 05:19 PM
Yes you can convert it to COBOL if you want ;)
If you mean "Convert my code for me"? That's not what Answers is really about.
At least try to convert it yourself and then if you get stuck on a particular area I'm sure lots of the folks here can help.
Answer by SceneCloth · Oct 07, 2010 at 10:06 PM
hehe, I have Converted most of this... I am still missing some things, I am trying to figure out how you would delegate in JS, basically want to sort out enemies from furthest to closest.my code works in its current state you need to drag and drop the enemies as i am not entirely sure how to add it via a tag.
Is it worth mixing your code JS and C#? I'm very new to JS. as you will probably see in my code :P
// Exposes an float array in the inspector, // which you can edit there. var targets : Transform[]; var SelectedTarget : Transform ; private var myTransForm : Transform;
function Start() {
selectedTarget = null; myTransform = transform;
// iterate through the array for (var Transform in targets){ }
// Since we can't resize builtin arrays // we have to recreate the array to resize it
}
function TargetEnemy(){ SelectedTarget = targets[0]; }
function SortTargetsByDistance() { targets.sort(FocusDelegate(Transform t1, Transform t2) }
function Update(){ if(Input.GetKeyDown(KeyCode.Tab)){ TargetEnemy();
}
}
Your answer
Follow this Question
Related Questions
Can someone convert this into javascript? :) 1 Answer
Convert String into KeyCode (UnityScript) 0 Answers
java to C# conversion 1 Answer
Convert Javascript class to a string 0 Answers
Converting C# script to javascript 0 Answers