- Home /
Plz Help me ! How to tanslate JS to C#?
I want to translate JS to C#... But I got in trouble....
var Speed : float;
var Turn : float;
function Awake(){
}
function Start () {
}
function Update () {
var targets : GameObject[] = GameObject.FindGameObjectsWithTag("enemy");
var closest : GameObject;
var closestDist = Mathf.Infinity;
for(Target in targets){
var dist = (transform.position - Target.transform.position).sqrMagnitude;
if(dist < closestDist){
closestDist = dist;
closest = Target;
}
}
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(closest.transform.position-transform.position), Turn*Time.deltaTime);
transform.position += transform.forward * Speed * Time.deltaTime;
}
function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "enemy"){
Destroy(gameObject);
Destroy(hit.gameObject);
}
}
this is JS script I want to make C# Script;
using UnityEngine;
using System.Collections;
public class missile_contro_cs : MonoBehaviour {
float Turn;
float Speed;
// Use this for initialization
void Start () {
}
void Update () {
GameObject[] target = new GameObject[]{GameObject.FindGameObjectWithTag("target")};
GameObject closest;
float closestDist = Mathf.Infinity;
foreach(target Target in target){
float dist = (transform.position - Target.transform.position).sqrMagnitude;
if (dist < closestDist){
closestDist = dist;
closest = Target;
}
}
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(closest.transform.position - transform.postion), Turn*Time.deltaTime);
transform.position += transform.forward * Speed * Time.deltaTime;
}
void OnTriggerEnter(Collider hit){
Destroy(GameObject);
}
}
but ! it make many Errors.. like 1) Assets/Script/test_algo/missile_contro_cs.cs(19,25): error CS0246: The type or namespace name target' could not be found. Are you missing a using directive or an assembly reference? 2)Assets/Script/test_algo/missile_contro_cs.cs(32,25): error CS0119: Expression denotes a
type', where a variable',
value' or `method group' was expected
plz Help me !
PS : I really sorry to my poor English. I trust u guys can help me !
I didn't test it but should work
using System;
using UnityEngine;
public class missile_contro_cs : $$anonymous$$onoBehaviour
{
public float Speed;
public float Turn;
void Update()
{
GameObject[] array = GameObject.FindGameObjectsWithTag("enemy");
GameObject gameObject = null;
float num = float.PositiveInfinity;
int i = 0;
GameObject[] array2 = array;
int length = array2.Length;
while (i < length)
{
float sqr$$anonymous$$agnitude = (this.transform.position - array2[i].transform.position).sqr$$anonymous$$agnitude;
if (sqr$$anonymous$$agnitude < num)
{
num = sqr$$anonymous$$agnitude;
gameObject = array2[i];
}
i++;
}
this.transform.rotation = (Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(gameObject.transform.position - this.transform.position), this.Turn * Time.deltaTime));
this.transform.position = (this.transform.position + this.transform.forward * this.Speed * Time.deltaTime);
}
void OnTriggerEnter(Collider hit)
{
if (hit.gameObject.tag == "enemy")
{
Destroy(this.gameObject);
Destroy(hit.gameObject);
}
}
}
did u makes full script for me ? wow I was so impressed by your effort ! thanks u !
I tested this script just one $$anonymous$$utes ago, it works perfectly ! Thank !
Answer by Lovrenc · Jan 15, 2013 at 02:11 AM
Try to use meaningful names. Forach part translates like this:
//First you declare type, then name ov variable you wish to use next and as last,
//collection which you wish to iterate
foreach(GameObject goTarget in target){
float dist = (transform.position - goTarget.transform.position).sqrMagnitude;
if (dist < closestDist){
closestDist = dist;
closest = goTarget;
}
}
Addition to @Lovrenc's answer:
You are missing the public keyword for your variables float turn and float speed.
Line 32: It should be
Destroy(gameObject);
, notDestroy(GameObject)
;`
Thanks to Lovrec and Chronos-L ! I Solve this Errors ! your advice was so helpful!!
Answer by Karsnen_2 · Jan 15, 2013 at 02:40 PM
I have already seen a lot of answers above with the converted script. But just in case you need to do in the future,
http://files.m2h.nl//js_to_c.php
SPOILER ALERT : It does not produce the exact script, you might have to adjust a little bit after you receive the converted script.
It very helpful page for me ! but i have to study c# script !
Thank!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Transform.Position Collision Issue 0 Answers
can someone help me with my js to c# conversion error 4 Answers
Help with casting a raycast C# 1 Answer