Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eminem428s · Jan 15, 2013 at 02:08 AM · c#translate

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 !

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image prototype7 · Jan 15, 2013 at 05:11 AM 0
Share

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);
         }
     }
 }
 
avatar image eminem428s · Jan 16, 2013 at 01:04 AM 0
Share

did u makes full script for me ? wow I was so impressed by your effort ! thanks u !

avatar image eminem428s · Jan 16, 2013 at 01:08 AM 0
Share

I tested this script just one $$anonymous$$utes ago, it works perfectly ! Thank !

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
     }
  }
 
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chronos-L · Jan 15, 2013 at 02:20 AM 0
Share

Addition to @Lovrenc's answer:

  1. You are missing the public keyword for your variables float turn and float speed.

  2. Line 32: It should be Destroy(gameObject);, not Destroy(GameObject);`

avatar image eminem428s · Jan 16, 2013 at 12:58 AM 0
Share

Thanks to Lovrec and Chronos-L ! I Solve this Errors ! your advice was so helpful!!

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eminem428s · Jan 16, 2013 at 01:02 AM 0
Share

It very helpful page for me ! but i have to study c# script !

Thank!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges