Search Unity

How do I convert this script to CSharp?

Discussion in 'Scripting' started by Treasureman, Nov 29, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a code that takes a certain Javascript script and lerps a certain variable in it. This is the code...
    Code (JavaScript):
    1. var aim : float = 20;          //determines amount of zoom capable. Larger number means further zoomed in
    2. var normal : float = 60;        //determines the default view of the camera when not zoomed in
    3. var smooth : float = 5;          //smooth determines speed of transition between zoomed in and default state
    4. var script : HeadBob;
    5. script = gameObject.GetComponent( HeadBob );
    6.    
    7. private var zoomedIn = false; //boolean that determines whether we are in zoomed in state or not
    8.  
    9. //This function toggles zoom capabilities with the Z key. If it's zoomed in, it will zoom out
    10. function Update()
    11. {
    12.         zoomedIn = false;
    13.     if (Input.GetButton("Fire2"))
    14.     {  
    15.         zoomedIn = true;
    16.     }
    17.    
    18.     //If "zoomedIn" is true, then it will not zoom in, but if it's false (not zoomed in) then it will zoom in.  
    19.     if( zoomedIn == true )
    20.     {
    21.     script.bobbingSpeed = Mathf.Lerp(script.bobbingSpeed, aim, Time.deltaTime*smooth );
    22.     }
    23.     else
    24.     {
    25.     script.bobbingSpeed = Mathf.Lerp(script.bobbingSpeed, normal, Time.deltaTime*smooth );
    26.     }
    27. }
    28.  
    But the thing is, it won't let me change variables in CSharp scripts unless THIS code is in CSharp, but the problem with that is, I'm really bad at CSharp. Could someone help me convert this code to CSharp? If so, thanks!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Have you made an attempt yet?
     
    BenZed likes this.
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Like I said, I'm near illiterate in CSharp
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Then learn and take some tutorials! For a script this simple, I doubt it would take you longer than an hour or two.
     
    Kiwasi likes this.
  5. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Well, I'm drunk and bored on transit, so I typed it into my phone.

    Absorb this into your brain:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BobHelper : MonoBehaviour {
    4.     [SerializeField] float aim = 20f;
    5.     [SerializeField] float normal = 20f;
    6.     [SerializeField] float smooth = 5f;
    7.  
    8.     HeadBob headBob;
    9.  
    10.     void Awake()
    11.     {
    12.         headBob = GetComponent<HeadBob>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.          bool firing = Input.GetButton("Fire2");
    18.  
    19.          headBob.bobbingSpeed = Mathf.Lerp(headBob.bobbingSpeed, firing ? aim : normal, Time.deltaTime * smooth);
    20.  
    21.     }
    22.  
    23. }
    24.  
    25.  
     
    Vaupell and cristo like this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This. You can look each function up in the API and find examples in both C# and JavaScript.

    Key differences
    • Variables are typed differently
    Code (CSharp):
    1. // JavaScript
    2. var myInt : int;
    3.  
    4. // C#
    5. int myInt;
    • Generics are different
    Code (CSharp):
    1. // JavaScript
    2. GetComponent(MyClass);
    3.  
    4. // C#
    5. GetComponent<MyClass>();
    • Functions return values are different
    Code (CSharp):
    1. // JavaScript
    2. function MyMethod (){}
    3.  
    4. // C#
    5. void MyMethod (){}
    That should cover it. There are some other differences, but they don't matter in this script.
     
  7. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    Here's a mini-tutorial...

    When you create a new C# script it will look something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class YourScriptName : MonoBehaviour {
    5.  
    6.     // put the variables up here
    7.  
    8.     void Start() {
    9.     }
    10.  
    11.     void Update() {
    12.     }
    13.  
    14.  
    15. }
    I included a line that shows where you'll put the variables -- you won't see that comment in your newly created script.

    In C# the variable declarations follow this format (in general):

    Code (CSharp):
    1. public|private variableType nameOfVariable [= defaultValue];
    Which means the first line of code:
    Code (Javascript):
    1. var aim : float=20;
    ...turns into this:
    Code (CSharp):
    1. public float aim = 20;
    (I *think* vars in Javascript are public by default, but if not you can ditch the public from the beginning of that line.)

    Now change the other variable declarations the same way.

    Line 5 in your code needs to be put into the Start() function and possibly tweaked a little. As a beginner in Unity I don't know all the ways something can be done, but I'd put that line of code in the function, like this:

    Code (CSharp):
    1. void Start()
    2. {
    3.    script = gameObject.GetComponent<HeadBob>();
    4. }
    Notice that I changed the format of that line of code a little bit to the way I generally do it.

    In line 10 of your code it shows that Javascript functions start with the function keyword, but that's not the case in C#, so you'll need to copy the lines of code from that function (lines 11 through 26) into the Update() function in your C# script.

    Most of those lines of code stay the same, but the two calls to Mathf.Lerp need to go inside a call to create a new Vector3, so instead of looking like this:

    Code (CSharp):
    1. Mathf.Lerp(script.bobbingSpeed, aim, Time.deltaTime*smooth )
    ...it will look like this:

    Code (CSharp):
    1. new Vector3( Mathf.Lerp(script.bobbingSpeed, aim, Time.deltaTime*smooth ) )
    Note that I only showed partial changes -- there are two lines where you'll have to add the new Vector3() around the Mathf.Lerp parts of the lines.

    Try making those changes and see what happens. (If I screwed something up I'm pretty sure someone will come along and straighten me out).

    Jay
     
    Kiwasi likes this.
  8. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    Crap, I type so freaking slow there are three posts before mine is done. Including one from a drunk guy on a phone! Bah!
     
    elmar1028, BenZed and Kiwasi like this.
  9. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Okay, cool, this really helped, but I got a parsing error on the very last line. How do I fix it (P.S. What's a parsing error)?
     
  10. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    sorry, forgot to add the script...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunAimY : MonoBehaviour {
    5.     public float aim = 0.0;
    6.     public float normal = 0.0;
    7.     public float smooh = 5;
    8.     public GunSway script;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         script = gameObject.GetComponent<HeadBob>();  
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update ()
    18. {
    19.  
    20.         zoomedIn = false;
    21.     if (Input.GetButton("Fire2"))
    22.     {    
    23.         zoomedIn = true;
    24.     }
    25.        
    26.     //If "zoomedIn" is true, then it will not zoom in, but if it's false (not zoomed in) then it will zoom in.    
    27.     if( zoomedIn == true )
    28.     {  
    29.             script.bobbingSpeed = new Vector3( Mathf.Lerp(script.bobbingSpeed, aim, Time.deltaTime*smooth));
    30.     }
    31.     else
    32.     {
    33.             script.bobbingSpeed = new Vector3( Mathf.Lerp(script.bobbingSpeed, aim, Time.deltaTime*smooth));
    34.  
    35.     }
    36. }
     
  11. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    whoops, made it for the wrong script. Read this one...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunAimY : MonoBehaviour {
    5.     public float aim = 0.0;
    6.     public float normal = 0.0;
    7.     public float smooh = 5;
    8.     public GunSway script;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         script = gameObject.GetComponent<GunSway>();  
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update ()
    18. {
    19.  
    20.         zoomedIn = false;
    21.     if (Input.GetButton("Fire2"))
    22.     {    
    23.         zoomedIn = true;
    24.     }
    25.        
    26.     //If "zoomedIn" is true, then it will not zoom in, but if it's false (not zoomed in) then it will zoom in.    
    27.     if( zoomedIn == true )
    28.     {  
    29.             script.defaultPosition.y = new Vector3( Mathf.Lerp(script.defaultPosition.y, aim, Time.deltaTime*smooth));
    30.     }
    31.     else
    32.     {
    33.             script.defaultPosition.y = new Vector3( Mathf.Lerp(script.defaultPosition.y, aim, Time.deltaTime*smooth));
    34.  
    35.     }
    36. }
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You need one more closing } on the end to close the class scope
     
    JayJennings likes this.
  13. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Okay, I did that, but now I'm getting errors on the variables. Why? They look right to me...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunAimY : MonoBehaviour {
    5.     public float aim = 0.0;
    6.     public float normal = 0.0;
    7.     public float smooh = 5;
    8.     public GunSway script;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         script = gameObject.GetComponent<GunSway>();  
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update ()
    18. {
    19.  
    20.         zoomedIn = false;
    21.     if (Input.GetButton("Fire2"))
    22.     {    
    23.         zoomedIn = true;
    24.     }
    25.        
    26.     //If "zoomedIn" is true, then it will not zoom in, but if it's false (not zoomed in) then it will zoom in.    
    27.     if( zoomedIn == true )
    28.     {  
    29.             script.defaultPosition.y = new Vector3( Mathf.Lerp(script.defaultPosition.y, aim, Time.deltaTime*smooth));
    30.     }
    31.     else
    32.     {
    33.             script.defaultPosition.y = new Vector3( Mathf.Lerp(script.defaultPosition.y, aim, Time.deltaTime*smooth));
    34.  
    35.     }
    36. }
    37. }
    Assets/GunAimY.cs(5,30): error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type

    also on line (6,33).
     
  14. a7BiT-psycho

    a7BiT-psycho

    Joined:
    Apr 25, 2014
    Posts:
    4
    Read the error message carefully and think about what to do :) I'm sure you can figure this one out for yourself.
     
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You should probably do this:

     
  16. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I don't get what it's it's talking about though. Add 'f'? Why would I add f?
     
  17. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    C# requires everything to be strongly typed. 1.6 is a double. 1.6f is a float. You can't assign a double to a float.

    Code (CSharp):
    1. public float aim = 0.0f;