Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Javascript script to c#

Discussion in 'Scripting' started by Adam_Benko, Dec 10, 2018.

  1. Adam_Benko

    Adam_Benko

    Joined:
    Jun 16, 2018
    Posts:
    103
    Hi. I came across this script, that does perfectly what I need. Only problem is, that it is written in JS. Could somebody help me convert this script into c# one ?
    Thank you very much
    From this post https://answers.unity.com/questions...use-and-object-top-down-2d-c.html#answer-form

    Code (JavaScript):
    1.  #pragma strict
    2. var Parent : Transform; // Whatever you want to camera locked to
    3. var Obj : Transform; // The object to place the camera on
    4. var Radius = 4.5;
    5. var Dist : float;
    6. var MousePos1 : Vector3;
    7. var MousePos2 : Vector3;
    8. var ScreenMouse : Vector3;
    9. var MouseOffset : Vector3;
    10.  
    11. function Update() {
    12.      MousePos1 = Input.mousePosition;
    13.      ScreenMouse = GetComponent.<Camera>().main.ScreenToWorldPoint(Vector3(MousePos1.x, MousePos1.y, Obj.position.z-GetComponent.<Camera>().main.transform.position.z));
    14.      MouseOffset = ScreenMouse - Parent.position;
    15.      MousePos2 = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, -transform.position.z));
    16.      Obj.position.y = ((MousePos2.y - Parent.position.y)/2.0)+Parent.position.y;
    17.      Obj.position.x = ((MousePos2.x - Parent.position.x)/2.0)+Parent.position.x;
    18.      
    19.      Dist = Vector2.Distance(Vector2(Obj.position.x, Obj.position.y), Vector2(Parent.position.x, Parent.position.y));
    20.      
    21.      if(Dist > Radius){
    22.          var norm = MouseOffset.normalized;
    23.          Obj.position.x = norm.x*Radius + Parent.position.x;
    24.          Obj.position.y = norm.y*Radius + Parent.position.y;
    25.      }
    26. }
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Adam_Benko

    If you google for "unity js to C#" you'll find some sites that do this for you, at least to some extent.
     
  3. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Alright I had a couple mins to kill so here ya go. Untested. File name needs to match the class name (CamScript) in case you don't know that!

    Code (CSharp):
    1. public class CamScript : MonoBehaviour
    2. {
    3.     Transform Parent; // Whatever you want to camera locked to
    4.     Transform Obj; // The object to place the camera on
    5.     float Radius = 4.5f;
    6.     float Dist;
    7.     Vector3 MousePos1, MousePos2, ScreenMouse, MouseOffset;
    8.     public void Update()
    9.     {
    10.         MousePos1 = Input.mousePosition;
    11.         // the below line assumes this script is attached to a camera object
    12.         ScreenMouse = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(MousePos1.x, MousePos1.y, Obj.position.z - GetComponent<Camera>().transform.position.z));
    13.         MouseOffset = ScreenMouse - Parent.position;
    14.         MousePos2 = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -transform.position.z));
    15.         //Obj.position.y = ((MousePos2.y - Parent.position.y) / 2.0) + Parent.position.y;
    16.         //Obj.position.x = ((MousePos2.x - Parent.position.x) / 2.0) + Parent.position.x;
    17.         Obj.position = new Vector3((MousePos2.x - Parent.position.x) / 2.0f + Parent.position.x, (MousePos2.y - Parent.position.y) / 2.0f + Parent.position.y, Obj.position.z);
    18.  
    19.         Dist = Vector2.Distance(new Vector2(Obj.position.x, Obj.position.y), new Vector2(Parent.position.x, Parent.position.y));
    20.  
    21.         if (Dist > Radius)
    22.         {
    23.             var norm = MouseOffset.normalized;
    24.             //Obj.position.x = norm.x * Radius + Parent.position.x;
    25.             //Obj.position.y = norm.y * Radius + Parent.position.y;
    26.             Obj.position = new Vector3(norm.x * Radius + Parent.position.x, norm.y * Radius + Parent.position.y, Obj.position.z);
    27.         }
    28.     }
    29. }
     
    Adam_Benko and Joe-Censored like this.
  4. Adam_Benko

    Adam_Benko

    Joined:
    Jun 16, 2018
    Posts:
    103
    Thank you very much :) . I have tried the code but I am getting an error in the console: "Object reference not set to an instance of an object", for the line 17. I've tried to use Camera camera obj instead of GetComponent<>(); but that did not help. Where do you think that the problem is ?
     
    Last edited: Dec 11, 2018
  5. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    Adam_Benko likes this.
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,615
    change
    Transform Obj;
    to
    public Transform Obj;


    Then when you look at the component in the inspector, you should see a UI widget that you can drag the desired object in to. You might have to do the same thing with the Parent variable as well.
     
    Adam_Benko likes this.