- Home /
Converting to C#
I have been trying to convert this over to C# for a few days now with no luck could some one please help me.
var target : GameObject;
var LR : LineRenderer;
var arcLength = 2.0;
var arcVariation = 2.0;
var inaccuracy = 1.0;
function Update() {
var lastPoint = transform.position;
var i = 1;
LR.SetPosition(0, transform.position);//make the origin of the LR the same as the transform
while (Vector3.Distance(target.transform.position, lastPoint) >.5) {//was the last arc not touching the target?
LR.SetVertexCount(i + 1);//then we need a new vertex in our line renderer
var fwd = target.transform.position - lastPoint;//gives the direction to our target from the end of the last arc
fwd.Normalize();//makes the direction to scale
fwd = Randomize(fwd, inaccuracy);//we don't want a straight line to the target though
fwd *= Random.Range(arcLength * arcVariation, arcLength);//nature is never too uniform
fwd += lastPoint;//point + distance * direction = new point. this is where our new arc ends
LR.SetPosition(i, fwd);//this tells the line renderer where to draw to
i++;
lastPoint = fwd;//so we know where we are starting from for the next arc
}
}
function Randomize (v3 : Vector3, inaccuracy2 : float) {
v3 += Vector3(Random.Range(-1.0, 1.0), Random.Range(-1.0, 1.0), Random.Range(-1.0, 1.0)) * inaccuracy2;
v3.Normalize();
return v3;
}
Why don't you edit your question to post your attempt along with the code above. Someone on the list can then help you with any remaining issues.
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
Answer by programmrzinc · May 16, 2013 at 02:15 PM
Here I am not sure what you need to import, or declaring fwd
imports System.Collection;
imports Unity.Engine;
public GameObject target;
public LineRenderer LR;
public double arcLength = 2.0;
public double arcVariation = 2.0;
public double inaccuracy = 1.0;
void Update(){
Vector3 lastPoint = transform.position;
int i = 1;
LR.SetPosition(0, transform.position);//make the origin of the LR the same as the transform
while (Vector3.Distance(target.transform.position, lastPoint) >.5) {//was the last arc not touching the target?
LR.SetVertexCount(i + 1);//then we need a new vertex in our line renderer
public fwd = target.transform.position - lastPoint;//gives the direction to our target from the end of the last arc
fwd.Normalize();//makes the direction to scale
fwd = Randomize(fwd, inaccuracy);//we don't want a straight line to the target though
fwd *= Random.Range(arcLength * arcVariation, arcLength);//nature is never too uniform
fwd += lastPoint;//point + distance * direction = new point. this is where our new arc ends
LR.SetPosition(i, fwd);//this tells the line renderer where to draw to
i++;
lastPoint = fwd;//so we know where we are starting from for the next arc
}
}
It dont like this line "public fwd = target.transform.position - lastPoint;".
This is not yet a complete c# translation. The following are some additions to @programmrzinc's answer
import UnityEngine;
import System.Collections;
public class ClassName : $$anonymous$$onoBehaviour {
//variables
public GameObject target;
...
void Update() {
...
}
private Vector3 Randomize( ... ) {
...
}
}
Please note that the ClassName
should have the same name as your script, eg: XYZ.cs will have public class XYZ : $$anonymous$$onoBehaviour
. This is the default when you create a .cs from Unity.
It dont like this line "public fwd = target.transform.position - lastPoint;".