Search Unity

Error with Arc LineRenderer Script in JS to C#

Discussion in 'Scripting' started by sandolkakos, May 3, 2010.

  1. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    Hi friends,
    i want to thank each of you for all the help they are always giving us here.

    My Question:
    I downloaded a JS Script to do ARC in LineRenderer, it is working, but now i want it in C#.

    I did a conversion of these two JS Scripts to C#, but in C#, the LineRenderer is not working equal at the JS Script.

    i put my Project in Attachment Files.

    Someone know tell me where is wrong?

    First Script:
    Code (csharp):
    1.  
    2. // Bezier.js
    3.  
    4. var sections : float = 10.0; // should be float for division purposes
    5. private var lineRenderer : LineRenderer;
    6.  
    7. function Start(){    
    8.     lineRenderer = GetComponent(LineRenderer);
    9.     lineRenderer.SetVertexCount(sections);
    10. }
    11.  
    12. function GetQuadraticCoordinates(t : float , p0 : Vector3 , c0 : Vector3 , p1 : Vector3 ) : Vector3 {
    13.     return Mathf.Pow(1-t,2)*p0 + 2*t*(1-t)*c0 + Mathf.Pow(t,2)*p1 ;;
    14. }
    15.  
    16. function Plot( p0 : Vector3 , c0 : Vector3 , p1 : Vector3 ) {
    17.    var t : float ;    
    18.    for (var i : int = 0 ; i < sections ; i++ ){
    19.       t = i/(sections-1) ;
    20.       lineRenderer.SetPosition (i ,GetQuadraticCoordinates(t , p0 , c0 , p1 ));
    21.    }
    22. }
    23.  
    Conversion:
    Code (csharp):
    1.  
    2. // My Conversion to C#
    3. // Bezier.cs
    4.  
    5. using UnityEngine;
    6. using System.Collections;
    7.  
    8. public class Bezier : MonoBehaviour {
    9.    
    10.     public int sections = 10; // should be float for division purposes
    11.    
    12.     private LineRenderer lineRenderer;
    13.  
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.         // lineRenderer = this.GetComponent("LineRenderer");
    18.         lineRenderer = (LineRenderer) GetComponent(typeof(LineRenderer));
    19.         lineRenderer.SetVertexCount(10);
    20.     }
    21.    
    22.     public Vector3 GetQuadraticCoordinates(float t, Vector3 p0, Vector3 c0, Vector3 p1)
    23.     {
    24.         return Mathf.Pow(1-t,2)*p0 + 2*t*(1-t)*c0 + Mathf.Pow(t,2)*p1 ;
    25.        
    26.     }
    27.  
    28.     public void Plot( Vector3 p0, Vector3 c0, Vector3 p1 )
    29.     {
    30.         float t;
    31.         for (int i = 0 ; i < 10 ; i++ )
    32.         {
    33.             t = i/(10-1) ;
    34.             lineRenderer.SetPosition (i ,GetQuadraticCoordinates(t , p0 , c0 , p1 ));
    35.         }
    36.     }
    37. }
    38.  
    39.  
    Second Script:
    Code (csharp):
    1.  
    2. // ArcBehaviour.js
    3.  
    4. var start   : Transform ;
    5. var middle  : Transform ;
    6. var end     : Transform ;
    7.  
    8. private var bezier : Bezier ;
    9. private var xLoopAsc : boolean;
    10. private var xLoopDesc : boolean;
    11.  
    12. public var speedCurve : float = 100;
    13.  
    14. function Start(){
    15.     bezier = GetComponent(Bezier);
    16.     xLoopAsc = true;
    17.     xLoopDesc = false;
    18. }
    19.  
    20. function Update(){
    21.    
    22.     if ( (middle.localPosition.x <= 50)  (xLoopAsc == true) )
    23.     {
    24.         middle.localPosition +=  Vector3.right * Time.deltaTime * speedCurve;
    25.        
    26.         if (middle.localPosition.x >= 50)
    27.         {
    28.             xLoopAsc = false;
    29.             xLoopDesc = true;
    30.         }
    31.     }
    32.    
    33.     if ( (middle.localPosition.x >= -50)  (xLoopDesc == true) )
    34.     {
    35.         middle.localPosition +=  Vector3.right * Time.deltaTime * speedCurve * -1;
    36.        
    37.         if (middle.localPosition.x <= -50)
    38.         {
    39.             xLoopDesc = false;
    40.             xLoopAsc = true;
    41.         }
    42.     }
    43.    
    44.     Debug.DrawLine(start.position,middle.position,Color.red);
    45.     Debug.DrawLine(middle.position,end.position,Color.red);
    46.     Debug.DrawLine(start.position,end.position,Color.red);
    47.     bezier.Plot(start.position , middle.position , end.position );
    48. }
    49.  
    50.  
    51.  
    Conversion:
    Code (csharp):
    1.  
    2. // Conversion to C#
    3. // ArcBehaviour.cs
    4.  
    5. using UnityEngine;
    6. using System.Collections;
    7.  
    8. public class ArcBehaviour : MonoBehaviour {
    9.  
    10.     public Transform start;
    11.     public Transform middle;
    12.     public Transform end;
    13.  
    14.     private Bezier bezier;
    15.     bool xLoopAsc;
    16.     bool xLoopDesc;
    17.  
    18.     public float speedCurve = 100f;
    19.    
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         bezier = (Bezier) FindObjectOfType(typeof(Bezier));
    24.         xLoopAsc = true;
    25.         xLoopDesc = false;
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update ()
    30.     {
    31.        
    32.         if ( (middle.localPosition.x <= 50)  (xLoopAsc == true) )
    33.         {
    34.             middle.localPosition +=  Vector3.right * Time.deltaTime * speedCurve ;
    35.            
    36.             if (middle.localPosition.x >= 50)
    37.             {
    38.                 xLoopAsc = false;
    39.                 xLoopDesc = true;
    40.             }
    41.         }
    42.        
    43.         if ( (middle.localPosition.x >= -50)  (xLoopDesc == true) )
    44.         {
    45.             middle.localPosition +=  Vector3.right * Time.deltaTime * speedCurve * -1 ;
    46.            
    47.             if (middle.localPosition.x <= -50)
    48.             {
    49.                 xLoopDesc = false;
    50.                 xLoopAsc = true;
    51.             }
    52.         }
    53.        
    54.         Debug.DrawLine(start.position,middle.position,Color.red);
    55.         Debug.DrawLine(middle.position,end.position,Color.red);
    56.         Debug.DrawLine(start.position,end.position,Color.red);
    57.         bezier.Plot(start.position , middle.position , end.position );
    58.  
    59.    
    60.     }
    61. }
    62.  
    63.  
     

    Attached Files:

  2. Birlouz

    Birlouz

    Joined:
    May 3, 2014
    Posts:
    4
    I know it's an old post, but in case someone want the correction for this example to work in C#

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bezier : MonoBehaviour {
    5.    
    6.     public int sections = 10; // should be float for division purposes
    7.    
    8.     private LineRenderer lineRenderer;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         // lineRenderer = this.GetComponent("LineRenderer");
    14.         lineRenderer = this.GetComponent(typeof(LineRenderer)) as LineRenderer;
    15.         lineRenderer.SetVertexCount(sections);
    16.     }
    17.  
    18.     public Vector3 GetQuadraticCoordinates(float t, Vector3 p0, Vector3 c0, Vector3 p1)
    19.     {
    20.         return Mathf.Pow(1f-t,2f)*p0 + 2f*t*(1f-t)*c0 + Mathf.Pow(t,2f)*p1 ;
    21.     }
    22.  
    23.     public void Plot( Vector3 p0, Vector3 c0, Vector3 p1 )
    24.     {
    25.         float t;
    26.         for (int i = 0 ; i < sections ; i++ )
    27.         {
    28.             t = (float)i/(sections-1) ;
    29.             lineRenderer.SetPosition(i ,GetQuadraticCoordinates(t , p0 , c0 , p1 ));
    30.         }
    31.     }
    32. }
     
  3. unidad

    unidad

    Joined:
    Feb 12, 2015
    Posts:
    1
    thanks birlouz! worked like a charm