Search Unity

Translating js to c#

Discussion in 'Scripting' started by Dawnreaver, May 12, 2013.

  1. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Hey Folks ^_^

    recently I've been working only with c# so I started "translating" the js scripts of one of my projects into c#. However I'm stuck with this one bit:
    JS
    Code (csharp):
    1.  
    2. function MoveTo(obj: Transform, dest: Vector3, duration: float)
    3.     {
    4.       if (isMoving) return; // abort call if already moving
    5.       isMoving = true; // set flag to indicate "I'm moving"
    6.       var orig: Vector3 = obj.position; // save the initial position
    7.       var t: float = 0; // start position indicator (0=orig, 1=dest)
    8.       while (t < 1)
    9.         {
    10.               t += Time.deltaTime / duration; // increment proportionally the indicator
    11.               obj.position = Vector3.Lerp(orig, dest, t); // set new position
    12.               yield; // let Unity free until next frame
    13.         }
    14.  
    15.         isMoving = false; // clear flag to show the coroutine has ended
    16.     }
    17.  
    and here my C# version
    Code (csharp):
    1.  
    2. void MoveTo(Transform obj,Vector3 dest, float duration)
    3.     {
    4.         if (isMoving) return;// abort call if already moving
    5.         isMoving = true; // set flag to indicate "I'm moving"
    6.         Vector3 orig = obj.position; // save the initial position
    7.         float t = 0.0f; // start position indicator (0=orig, 1=dest)
    8.         while (t < 1.0f)
    9.         {
    10.             t += Time.deltaTime / duration; // increment proportionally the indicator
    11.             obj.position = Vector3.Lerp(orig, dest, t); // set new position
    12.             yield return null;
    13.         }
    14.         isMoving = false; // clear flag to show the coroutine has ended
    15.     }
    16.  
    I receive this error message: error CS1624: The body of `PlayerControlls.MoveTo(UnityEngine.Transform, UnityEngine.Vector3, float)' cannot be an iterator block because `void' is not an iterator interface type

    I tried to replace the "yield" with a function that acts as a co-routine. but that only cause the object to move as fast as possible into the direction that is fed into the initial function (dest).

    Does any of you guys have a suggestion on how I can solve ths issue ?

    Cheers!

    Dwnreaver
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Instead of "void MoveTo", "IEnumerator MoveTo" is needed at the beginning.
     
  3. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Yes I tried that before ( and again just to make sure) but the object is still not moving. This is the code I feed into "void MoveTo":

    Code (csharp):
    1.  
    2. MoveTo(transform, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), moveTime);
    3.  
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You want MoveTo to be a coroutine. That means, you can't just call it. You need to start it with

    Code (csharp):
    1. StartCoroutine (MoveTo (...));
     
  5. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Cheers! That worked a treat :) Would you be able to explain why it worked in JS but not in C# ? I just would like to understand it.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It's an ugly behaviour of Unity's JavaScript. It automatically performs StartCoroutine to make it look easier I guess. But after all, everyone who codes should understand what is going one and explicitly use StartCoroutine. Though, that's just my opinion. It often produces more confusion than making it simpler.