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

Help on converting this JS to C#

Discussion in 'Scripting' started by DE_Hizral, Jun 15, 2020.

  1. DE_Hizral

    DE_Hizral

    Joined:
    Feb 21, 2020
    Posts:
    19
    Hello all,

    Need help on converting this code that I did along time ago using JS to C#, seem I cannot find what is the correct conversion for the "var down =" from JS to C#

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public var posOrigin : Transform;
    4. private var direction : Vector3;
    5. public var isGrounded : boolean;
    6. public var line : float;
    7. public var hit : RaycastHit;
    8.  
    9. function Start ()
    10. {
    11.     direction = Vector3.zero;
    12. }
    13.  
    14. function Update ()
    15. {
    16.     Direction ();
    17.     DrawLine ();
    18. }
    19.  
    20. function Direction ()
    21. {
    22.     var down = Physics.Raycast (posOrigin.position, -direction.up, hit, line);
    23.    
    24.     if (down)
    25.     {
    26.         isGrounded = true;
    27.     }
    28.     else if (!down)
    29.     {
    30.         isGrounded = false;
    31.     }
    32.    
    33.    
    34. }
    35.  
    36. function DrawLine ()
    37. {
    38.     Debug.DrawRay (posOrigin.position, direction.up * -line, Color.red);
    39. }
    Thank you.
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
  3. DE_Hizral

    DE_Hizral

    Joined:
    Feb 21, 2020
    Posts:
    19
    Thank you. manage to find what is the correct conversion. I can used "bool". So from :
    Code (CSharp):
    1. var down = Physics.Raycast (posOrigin.position, -direction.up, hit, line);
    it will be :

    Code (CSharp):
    1. bool down = Physics.Raycast (posOrigin.position, -direction.up, hit, line);
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Just FYI, "var" valid in C# as well. It acts as an implicit type declaration based on what follows the "=".
    Personally though, I like being explicit and using the actual type instead.