Hello everybody, I have got the script of how to draw a bezier curve but this script is js. I have tried to make the C# version but i got many difficulties because the use of function are not the same. Someone can help me please ?
@script ExecuteInEditMode()
#pragma strict
public var start : GameObject;
public var middle : GameObject;
public var end : GameObject;
public var color : Color = Color.white;
public var width : float = 0.2;
public var numberOfPoints : int = 20;
function Start()
{
// initialize line renderer component
var lineRenderer : LineRenderer =
GetComponent(LineRenderer);
if (null == lineRenderer)
{
gameObject.AddComponent(LineRenderer);
}
lineRenderer = GetComponent(LineRenderer);
lineRenderer.useWorldSpace = true;
lineRenderer.material = new Material(
Shader.Find("Particles/Additive"));
}
function Update()
{
// check parameters and components
var lineRenderer : LineRenderer =
GetComponent(LineRenderer);
if (null == lineRenderer || null == start
|| null == middle || null == end)
{
return; // no points specified
}
// update line renderer
lineRenderer.SetColors(color, color);
lineRenderer.SetWidth(width, width);
if (numberOfPoints > 0)
{
lineRenderer.SetVertexCount(numberOfPoints);
}
// set points of quadratic Bezier curve
var p0 : Vector3 = start.transform.position;
var p1 : Vector3 = middle.transform.position;
var p2 : Vector3 = end.transform.position;
var t : float;
var position : Vector3;
for(var i : int = 0; i < numberOfPoints; i++)
{
t = i / (numberOfPoints - 1.0);
position = (1.0 - t) * (1.0 - t) * p0
+ 2.0 * (1.0 - t) * t * p1
+ t * t * p2;
lineRenderer.SetPosition(i, position);
}
}
-
Change “function” to “void”. Unless it says “function x() : int”, in which case change it to int (or whatever type is there)
-
Change “var x : int = …” to “int x = …”
-
Surround the whole thing with:
using UnityEngine;
using System.Collections;
public class ThisScriptFileName : MonoBehaviour {
.... existing code ....
}
-
For AddComponent and GetComponent, use syntax like this: gameObject.AddComponent();
-
Any floating point numbers (e.g. 1.0, 3.14) should have “f” after them (1f, 3.14f)
These steps will get you 90% of the way to converting most JS to C#. Start with those, and if there are still things you need help converting, come back with any questions.
3 Likes
This ought to do it, but it’s off the top of my head & isn’t tested… That said, StarManta’s breakdown is solid, as you’ll see when comparing the two Normally I wouldn’t do the work for you, but I’m in a good mood and enjoy the practice these conversions gives me, haha. Also, please note that this is a direct conversion & I didn’t go out of my way to correct or alter anything beyond that.
[ExecuteInEditMode]
public GameObject start;
public GameObject middle;
public GameObject end;
public Color color = Color.white;
public float width = 0.2f;
public int numberOfPoints = 20;
void Start()
{
// initialize line renderer component
LineRenderer lineRenderer = GetComponent<LineRenderer>();
if (lineRenderer == null)
{
gameObject.AddComponent<LineRenderer>();
}
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.useWorldSpace = true;
lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
}
void Update()
{
// check parameters and components
lineRenderer = GetComponent<LineRenderer>();
if (lineRenderer == null || start == null || middle == null || end == null)
{
return; // no points specified
}
// update line renderer
lineRenderer.SetColors(color, color);
lineRenderer.SetWidth(width, width);
if (numberOfPoints > 0)
{
lineRenderer.SetVertexCount(numberOfPoints);
}
// set points of quadratic Bezier curve
Vector3 p0 = start.transform.position;
Vector3 p1 = middle.transform.position;
Vector3 p2 = end.transform.position;
float t ;
Vector3 position;
for(int i = 0; i < numberOfPoints; i++)
{
t = i / (numberOfPoints - 1.0);
position = (1.0 - t) * (1.0 - t) * p0 + 2.0 * (1.0 - t) * t * p1 + t * t * p2;
lineRenderer.SetPosition(i, position);
}
}