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

Updating an old project from 5.3.3 to 2018 and I do know how to translate a javascript to c#

Discussion in 'Scripting' started by Ruckrova, Oct 31, 2019.

  1. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    Can some please convert this to C# please i can't write code Thanks


    Code (JavaScript):
    1.  
    2. pragma strict
    3. var eagle : GameObject;
    4. var speed = 0.0;
    5. var fastspeed = 10.0;
    6. var slowspeed = 0.0;
    7. var anim: Animation;
    8. function Start () {
    9. anim = eagle.GetComponent.<Animation>();
    10. }
    11.  
    12. function Update () {
    13. transform.Translate(0,0,speed*Time.deltaTime);
    14. if (transform.eulerAngles.x > 250){
    15.     anim.CrossFade("let"); }
    16.         else    if (transform.eulerAngles.x > 10 &&transform.eulerAngles.x < 250){
    17. //transform.Translate(0,0,fastspeed*Time.deltaTime);
    18. speed = Mathf.Lerp(speed,fastspeed,Time.deltaTime);
    19.         anim.CrossFade("spust");}
    20.     else{
    21. speed = Mathf.Lerp(speed,slowspeed,Time.deltaTime);  
    22.     anim.CrossFade("idle");}
    23.  
    24. }
    pragma strict
    var eagle : GameObject;
    var speed = 0.0;
    var fastspeed = 10.0;
    var slowspeed = 0.0;
    var anim: Animation;
    function Start () {
    anim = eagle.GetComponent.<Animation>();
    }

    function Update () {
    transform.Translate(0,0,speed*Time.deltaTime);
    if (transform.eulerAngles.x > 250){
    anim.CrossFade("let"); }
    else if (transform.eulerAngles.x > 10 &&transform.eulerAngles.x < 250){
    //transform.Translate(0,0,fastspeed*Time.deltaTime);
    speed = Mathf.Lerp(speed,fastspeed,Time.deltaTime);
    anim.CrossFade("spust");}
    else{
    speed = Mathf.Lerp(speed,slowspeed,Time.deltaTime);
    anim.CrossFade("idle");}

    }
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,196
    Your post is a nightmare!

    There's a conversion tool you can use, here.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    and tutorial here,


    it should be simple only small changes required.
     
  4. DreamingInsanity

    DreamingInsanity

    Joined:
    Nov 5, 2017
    Posts:
    101
    I can't test it but I think this should work.
    edit: I was beaten to it - it's a better idea you do it yourself anyway so you can learn.
     
  5. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    Thanks people

    It's not my code i was working with a programmer online a few years back .

    I'm a Mac user i don't think the converter works on a mac ?
     
    Last edited: Oct 31, 2019
  6. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    Code (CSharp):
    1. public GameObject eagle;
    2. public float speed;
    3.  
    4. float fastspeed = 10.0f;
    5. float slowspeed = 0.0f;
    6. Animation anim;
    7. enum animState { idle, let, spust }
    8. animState curAnimState = animState.idle;
    9.  
    10. private void Start()
    11. {
    12.     anim = eagle.GetComponent<Animation>();
    13. }
    14.  
    15. private void Update()
    16. {
    17.     transform.Translate(0f, 0f, speed*Time.deltaTime);
    18.     if (transform.eulerAngles.x > 250f)
    19.     {
    20.         if (curAnimState != animState.let)
    21.         {
    22.             anim.CrossFade("let");
    23.             curAnimState = animState.let;
    24.         }
    25.     }
    26.     else if (transform.eulerAngles.x >10f && transform.eulerAngles.x < 250f)
    27.     {
    28.         //transform.Translate(0f, 0f, fastspeed*Time.deltaTime);
    29.         speed = Mathf.Lerp(speed, fastspeed, Time.deltaTime);
    30.      
    31.         if (curAnimState != animState.spust)
    32.         {
    33.             anim.CrossFade("spust");
    34.             curAnimState = animState.spust;
    35.         }
    36.     }
    37.     else
    38.     {
    39.         speed = Mathf.Lerp(speed, slowspeed, Time.deltaTime);
    40.  
    41.         if (curAnimState != animState.idle)
    42.         {
    43.             anim.CrossFade("idle");
    44.             curAnimState = animState.idle;
    45.         }
    46.     }
    47. }
    EDIT: I added a state check because you would call anim.CrossFade multiple times when only one time is needed.
     
    Last edited: Oct 31, 2019
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    The use of Euler angles in this script is problematic. It's one of those "It might work now, but it's likely to break following any inconsequential change later" sort of issues. See here for more information.

    Also, the Animation class is considered obsolete at this point. It'll still work, just be aware that it's a legacy component. (The new system, which uses the Animator class, wouldn't need the "state check" code added by @Team2Studio)