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. Dismiss Notice

Converting Code from JavaScript to C#

Discussion in 'Scripting' started by DarrikM7, Mar 6, 2015.

  1. DarrikM7

    DarrikM7

    Joined:
    Mar 6, 2015
    Posts:
    10
    I am rather new to scripting but I have been learning a lot. After watching a tutorial I have an awesome touch control script, but I am trying to convert it over from JavaScript to C#.

    Here is what I have in JavaScript:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public  var gravityDown : float;
    4.  
    5. private var ray : Ray;
    6. private var hit : RaycastHit;
    7.  
    8. function Update ()
    9. {
    10.     if(Input.GetMouseButtonUp(0))
    11.     {
    12.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    13.         if(Physics.Raycast(ray,hit))
    14.         {
    15.             transform.position.x = hit.point.x;
    16.             transform.position.y = hit.point.y;
    17.             this.GetComponent.<Rigidbody2D>().gravityScale = gravityDown;
    18.         }
    19.     }      
    20. }
    Here is my attempt at re-writing it in C#:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     //gravity multiplier
    7.     public float gravityDown;
    8.  
    9.     //used for raycasting
    10.     public Ray ray;
    11.     public RaycastHit hit;
    12.  
    13.      // Update is called once per frame
    14.     void Update ()
    15.     {
    16.         if (Input.GetMouseButtonUp (0))
    17.         {
    18.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    19.             if(Physics.Raycast(ray,hit))
    20.             {
    21.                 transform.position.x = hit.point.x;
    22.                 transform.position.y = hit.point.y;
    23.                 this.rigidbody2D.gravityScale = gravityDown;
    24.             }
    25.         }
    26.     }
    27. }
    I get the following errors:

    Assets/Scripts/PlayerMovement.cs(21,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit)' has some invalid arguments

    Assets/Scripts/PlayerMovement.cs(23,43): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

    Assets/Scripts/PlayerMovement.cs(21,36): error CS1620: Argument `#2' is missing `out' modifier

    Assets/Scripts/PlayerMovement.cs(21,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit)' has some invalid arguments

    I have been pretty stuck on this, but I feel like it is very close. The JavaScript runs perfectly and all I am doing is trying to re-write it in C#. If someone could point me in the right direction that would be great!
     
  2. Anozireth

    Anozireth

    Joined:
    Feb 7, 2013
    Posts:
    71
    Try this:

    Code (csharp):
    1.  
    2.            if(Physics.Raycast(ray,out hit))  // Note the 'out' keyword here
    3.            {
    4.                 // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
    5.                 transform.position = new Vector3(hit.point.x, hit.point.y);
    6.                this.rigidbody2D.gravityScale = gravityDown;
    7.            }
    8.  
    This is just off the top of my head, so it might not be 100 correct. Hope that helps.
     
  3. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    A couple of things.
    The lines numbers posted don't match the lines in your source ...
    Assets/Scripts/PlayerMovement.cs(21,36) actually refers to line 19.

    In line 18 : remove the Type designator Ray because the variable ray has already been declared.

    In line 19 : Where is the variable hit assigned a value ??
    check the docs http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
  4. DarrikM7

    DarrikM7

    Joined:
    Mar 6, 2015
    Posts:
    10
    OK here is what I got and it returns one error:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     //gravity multiplier
    7.     public float gravityDown;
    8.  
    9.     //used for raycasting
    10.     public Ray ray;
    11.     public RaycastHit hit;
    12.  
    13.     void Update ()
    14.     {
    15.         if (Input.GetMouseButtonUp (0))
    16.         {
    17.             if(Physics.Raycast(transform.position,out hit))  // Note the 'out' keyword here
    18.             {
    19.                 // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
    20.                [B][I] transform.position = new Vector3(hit.point.x, hit.point.y,);[/I][/B]
    21.                 this.GetComponent<Rigidbody2D>().gravityScale = gravityDown;
    22.             }
    23.         }
    24.     }
    25. }
    Error: Assets/Scripts/PlayerMovement.cs(20,89): error CS0839: An argument is missing
     
  5. DarrikM7

    DarrikM7

    Joined:
    Mar 6, 2015
    Posts:
    10
    Sorry about the and , tried to bold and italicize the area but I guess you can't do that.
     
  6. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    230
    The errors usually give a good indication of what's wrong.
    "Error: Assets/Scripts/PlayerMovement.cs(20,89): error CS0839: An argument is missing"

    If you look on line 20, you'll notice this:
    new Vector3(hit.point.x, hit.point.y, );

    You can clearly see it's missing the 3rd argument. :)

    Edit: The 3rd argument is Z. Since you're dealing with 2D and likely do not care to modify the z, you can simply pass in it's own z:
    new Vector3(hit.point.x, hit.point.y, transform.position.z);
     
  7. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  8. DarrikM7

    DarrikM7

    Joined:
    Mar 6, 2015
    Posts:
    10
    I thought I had it as a 0, sorry. Even after adding a 0 into the "z" spot (or if I leave it blank and removing comma) I get the following errors:

    Assets/Scripts/PlayerMovement.cs(17,36): error CS10502: The best overloaded method match for Unity Engine. Physics.Raycast(UnityEngine.Vector3)' has some invalid arguments.

    Assets/Scripts/PlayerMovement.cs(17,36): error CS1615: Argument ' #2" does not require ' out' modifier. Consider removing ' out' modifier

    So does that mean I don't need that "out" modifier? When I remove the "out" modifier i get another error that states:

    Assets/Scripts/PlayerMovement.cs(17,36): error CS1503: Argument ' #2' cannot convert ' UnityEngine.RaycastHit' expression type ' UnityEngine.Vector3

    This line appears to be giving me the most issue. Should I use another type other than "out"?
     
  9. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    May we see the code after changes, please? Specific when you removed "out" (although i think is needed there).
     
  10. DarrikM7

    DarrikM7

    Joined:
    Mar 6, 2015
    Posts:
    10
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     //gravity multiplier
    7.     public float gravityDown;
    8.  
    9.     //used for raycasting
    10.     public Ray ray;
    11.     public RaycastHit hit;
    12.  
    13.     void Update ()
    14.     {
    15.         if (Input.GetMouseButtonUp (0))
    16.         {
    17.             if(Physics.Raycast(transform.position, hit))  // Note the 'out' keyword here
    18.             {
    19.                 // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
    20.                 transform.position = new Vector3(hit.point.x, hit.point.y,0);
    21.                 this.GetComponent<Rigidbody2D>().gravityScale = gravityDown;
    22.             }
    23.         }
    24.     }
    25. }
    Errors are as follows:

    Assets/Scripts/PlayerMovement.cs(17,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3)' has some invalid arguments

    Assets/Scripts/PlayerMovement.cs(17,36): error CS1615: Argument `#2' does not require `out' modifier. Consider removing `out' modifier

    I get the same errors if I remove the 0 from the "z" field.



     
  11. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    @DarrikM7
    The issue as I see it is as a result of your revisions --- the Physics.Raycast arguments are now incorrect.

    Essentially the Raycast should be told where to 'fire' from and in which direction ( the function has some default arguments that aren't essential)

    The original code used a Ray type variable which contains the to and from data.
    The current code only uses the 'from' data ie the Vector3 transform.position.

    These are the function signatures

    Code (CSharp):
    1.  
    2. public static bool Raycast(
    3. Ray ray);
    4.  
    5. public static bool Raycast(
    6. Ray ray,
    7. float distance);
    8.  
    9. public static bool Raycast(
    10. Ray ray,
    11. out RaycastHit hitInfo);
    12.  
    13. public static bool Raycast(
    14. Vector3 origin,
    15. Vector3 direction);
    16.  
    17. public static bool Raycast(
    18. Ray ray,
    19. [DefaultValue("Mathf.Infinity")]  float distance,
    20. [DefaultValue("DefaultRaycastLayers")]  int layerMask);
    21.  
    22. public static bool Raycast(
    23. Ray ray,
    24. out RaycastHit hitInfo,
    25. float distance);
    26.  
    27. public static bool
    28. Raycast(Vector3 origin,
    29. Vector3 direction,
    30. float distance);
    31.  
    32. public static bool Raycast(
    33. Vector3 origin,
    34. Vector3 direction,
    35. out RaycastHit hitInfo);
    36.  
    37. public static bool Raycast(
    38. Ray ray,
    39. out RaycastHit hitInfo,
    40. [DefaultValue("Mathf.Infinity")]  float distance,
    41. [DefaultValue("DefaultRaycastLayers")] int layerMask);
    42.  
    43. public static bool Raycast(
    44. Vector3 origin,
    45. Vector3 direction,
    46. [DefaultValue("Mathf.Infinity")]  float distance,
    47. [DefaultValue("DefaultRaycastLayers")]  int layerMask);
    48.  
    49. public static bool Raycast(
    50. Vector3 origin,
    51. Vector3 direction,
    52. out RaycastHit hitInfo,
    53. float distance);
    54.  
    55. public static bool Raycast(
    56. Vector3 origin,
    57. Vector3 direction,
    58. out RaycastHit hitInfo,
    59. [DefaultValue("Mathf.Infinity")]  float distance,
    60. [DefaultValue("DefaultRaycastLayers")]  int layerMask);
    61.  
    Regards, Kerry
     
  12. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    afterthought:
    IF the scene is 2D I'm wondering if the Physics.Raycast( ... ) could/should be Physics.Raycast2D( ... )
     
  13. DarrikM7

    DarrikM7

    Joined:
    Mar 6, 2015
    Posts:
    10
    It is a 2D scene. When I make the the code:

    transform.position=newVector2(hit.point.x, hit.point.y);

    I get the following errors:

    Assets/Scripts/PlayerMovement.cs(17,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3)' has some invalid arguments

    Assets/Scripts/PlayerMovement.cs(17,36): error CS1615: Argument `#2' does not require `out' modifier. Consider removing `out' modifier

    It is still showing the error with a Vector3 which seems strange to me, and that 'out' modifier message is back again saying that I do not need it. It seems strange that the syntax for this part seems to be a lot different than the Javascript code (the Javascript is assuming a lot it seems). Here is what I have for the code at this point (just changed to a Vector2):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     //gravity multiplier
    7.     public float gravityDown;
    8.  
    9.     //used for raycasting
    10.     public Ray ray;
    11.     public RaycastHit hit;
    12.  
    13.     void Update ()
    14.     {
    15.         if (Input.GetMouseButtonUp (0))
    16.         {
    17.             if(Physics.Raycast(transform.position,out hit))  // Note the 'out' keyword here
    18.             {
    19.                 // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
    20.                 transform.position = new Vector2(hit.point.x, hit.point.y);
    21.                 this.GetComponent<Rigidbody2D>().gravityScale = gravityDown;
    22.             }
    23.         }
    24.     }
    25. }
     
  14. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Which direction is your Raycast firing ?
     
  15. DarrikM7

    DarrikM7

    Joined:
    Mar 6, 2015
    Posts:
    10
    What is supposed to happen is I have that code attached to my player object, and when you click/tap the screen the object will jump to wherever you touched the screen (I am developing for Android). It works fine in the JavaScript version, just not in the C# version.
     
  16. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    According to the documentation, the best call for your use is this one:
    Code (CSharp):
    1. public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
    Here:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.   //gravity multiplier
    8.   public float gravityDown;
    9.  
    10.   //used for raycasting
    11.   public Ray ray;
    12.   public RaycastHit hit;
    13.  
    14.   void Update ()
    15.   {
    16.     if (Input.GetMouseButtonUp (0))
    17.     {
    18.       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    19.       if(Physics.Raycast(ray, out hit, 100)) // there is one more parameters (optional) that you specify the layer you firing the raycast
    20.       {
    21.         // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
    22.         transform.position = new Vector2(hit.point.x, hit.point.y);
    23.         this.GetComponent<Rigidbody2D>().gravityScale = gravityDown;
    24.       }
    25.     }
    26.   }
    27. }

    just wondering: since it is a touch-screen, why not use Input.GetTouch? I haven't use it yet, but in the documentation seems to have what you need. Have you tried?
     
  17. DarrikM7

    DarrikM7

    Joined:
    Mar 6, 2015
    Posts:
    10
    Hmm I may try the Input.GetTouch and see what happens, I will also try the code you provided and see if I can get it working, thanks!