Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Can someone convert these scripts to C#

Discussion in 'Scripting' started by thebest07111, Oct 27, 2014.

  1. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    hello,

    Can csoembody please convert these scripts to C#?
    healthbarenemy.js
    Code (JavaScript):
    1. ]var target : Transform;
    2. function Update ()
    3. {
    4.      var wantedPos = Camera.main.WorldToViewportPoint (target.position);
    5.      transform.position = wantedPos;
    6. }
    checkpoint.js
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var SpawnPoint : Transform;
    4.  
    5. function OnTriggerEnter(col : Collider)
    6. {
    7.     if(col.tag =="Player")
    8.     {
    9.         SpawnPoint.position = Vector3(transform.position.x, transform.position.y+5, transform.position.z);
    10.  
    11.     }
    12.  
    13. }
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var SpawnPoint: Transform;
    4.  
    5. var player : GameObject;
    6.  
    7. function OnTriggerEnter(col : Collider)
    8. {
    9.     if(col.tag == "Player")
    10.     {
    11.         player.transform.position = SpawnPoint.position;
    12.  
    13.     }
    14.  
    15. }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Yes, you can! You have the power.


    Code (CSharp):
    1. public Transform target;
    2. void Update ()
    3. {
    4.      Vector3 wantedPos = Camera.main.WorldToViewportPoint (target.position);
    5.      transform.position = wantedPos;
    6. }
    Code (CSharp):
    1. public Transform SpawnPoint;
    2.  
    3. void OnTriggerEnter(Collider col)
    4. {
    5.     if(col.tag == "Player")
    6.     {
    7.         SpawnPoint.position = new Vector3(transform.position.x, transform.position.y+5, transform.position.z);
    8.  
    9.     }
    10.  
    11. }
    Code (CSharp):
    1. public Transform SpawnPoint;
    2.  
    3. public GameObject player;
    4.  
    5. void OnTriggerEnter(Collider col)
    6. {
    7.     if(col.tag == "Player")
    8.     {
    9.         player.transform.position = SpawnPoint.position;
    10.  
    11.     }
    12.  
    13. }
     
  3. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    thanks it worked
     
  4. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    Hi agian.

    I have one more code i hope you can convert it for me

    Code (JavaScript):
    1. var radius : float = 5.0;    //provides a radius at which the explosive will effect rigidbodies
    2. var power : float = 10.0;    //provides explosive power
    3. var explosiveLift : float = 1.0; //determines how the explosion reacts. A higher value means rigidbodies will fly upward
    4. var explosiveDelay : float = 5.0; //adds a delay in seconds to our explosive object
    5. var explosionPrefab : Transform;
    6.  
    7. //var explosionSound : AudioClip;
    8. function Start()
    9. {
    10. }
    11. function Update () {
    12. Fire();
    13. }
    14. function Fire () {
    15.   yield WaitForSeconds(explosiveDelay);
    16.   var grenadeOrigin : Vector3 = transform.position;
    17.   var colliders : Collider[] = Physics.OverlapSphere (grenadeOrigin, radius); //any collider within the radius of our object will feel the explosion
    18.   var e : Transform = Instantiate(explosionPrefab,transform.position,transform.rotation);
    19.   Destroy(e.gameObject, 2);//destroys our particle system after 2 seconds
    20. for(var hit : Collider in colliders){  //for loop that says if we hit any colliders, then do the following below
    21. if (hit.rigidbody){
    22.      hit.rigidbody.AddExplosionForce(power, grenadeOrigin, radius, explosiveLift); //if we hit any rigidbodies then add force based off our power
    23.      //AudioSource.PlayClipAtPoint(explosionSound, transform.position, 1);
    24.      Destroy(gameObject);//the radius and finally the explosive lift. Afterwards destroy the game object.
    25.     }
    26. }
    27. }
    28. function OnCollisionEnter(col : Collision) {
    29.          var stickyBomb = gameObject.AddComponent(FixedJoint);
    30.          stickyBomb.connectedBody = col.rigidbody;
    31. }
    32.  
     
  5. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    you should really learn how to convert them yourself, that JS can more or less directly be ported to c# just with the syntax changes, with exception of the CoRoutine in it which you will need to use StartCoroutine() and a IEnumerator for
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Kogar likes this.
  7. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    yeah i know but then i get an error with the var stickybomb and i dont know what i need to type there
     
  8. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    When using the converter, your Javascript code should be as explicit as possible

    The reason the converter is not working in this scenario is because it doesn't know the variable type of stickyBomb.
    Code (JavaScript):
    1. var stickyBomb = gameObject.AddComponent(FixedJoint);
    Whereas In the fire function, it knows the variable type of grenadeOrigin which is Vector3:
    Code (JavaScript):
    1. var grenadeOrigin : Vector3 = transform.position;
    Do you know what the variable type that stickyBomb should be?
     
  9. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    How do you mean? This is the only script i got from js and i only need it to be converted
     
  10. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    I'm giving you a clue on what needs to be changed in order for the converter to work
     
  11. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    yeah i know but i dont really understand it. i'm not good in programming

    I have made this right now for the bottom code
    Code (JavaScript):
    1.     {
    2.             var joint = gameObject.AddComponent<FixedJoint> ();
    3.             joint.connectedBody = c.rigidbody;
    4.     }
    but i got the error cannot convert unityengine.object to unityengine transform.
     
    Last edited: Oct 28, 2014
  12. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Just a question. When you are not good in programming. Why do you want to convert the code to C#? You should be able to directly use the UnityScript versions that you got from wherever you found them.
     
  13. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Not good in programming -> Tries their best to not learn by asking for it to be done for them -> asks people to convert from an easy to understand dynamically typed language into a harder to understand strongly typed language -> Not to be mean, but you don't seem to have any logic skills either.

    Good luck!
     
  14. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    I fixed it already. but thanks for the help anyway
     
  15. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    Please describe how you fixed it so that others can also learn, too.