I have been working on converting a JS to C# for a while now and have been having some problems with this one. With some help I have managed to get it narrowed down to one line causing a problem now. My problem is I cant figure out how to fix it. I have found a site that trys to convert it for you and it was no help on this part.
The errors are ass follows.
Assets/Lightning.cs(26,8): error CS1502: The best overloaded method match for `Lightning.Randomize(UnityEngine.Vector3, float)’ has some invalid arguments
Assets/Lightning.cs(26,8): error CS1503: Argument #2' cannot convert
double’ expression to type `float’
Assets/Lightning.cs(27,16): error CS1502: The best overloaded method match for `UnityEngine.Random.Range(float, float)’ has some invalid arguments
Assets/Lightning.cs(27,16): error CS1503: Argument #1' cannot convert
double’ expression to type `float’
Assets/Lightning.cs(37,23): error CS0119: Expression denotes a type', where a
variable’, value' or
method group’ was expected
Here is the C# version of the script.
using UnityEngine;
using System.Collections;
public class Lightning : MonoBehaviour {
public GameObject target;
public LineRenderer LR;
public double arcLength = 2.0;
public double arcVariation = 2.0;
public double inaccuracy = 1.0;
public Vector3 fwd;
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
fwd = target.transform.position - lastPoint;//gives the direction to our target from the end of the last arc20.
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 to25.
i++;
lastPoint = fwd;//so we know where we are starting from for the next arc
}
}
private Vector3 Randomize(Vector3 v3 , float inaccuracy2)
{
v3 += Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * inaccuracy2;
v3.Normalize();
return v3;
}
}
This is the JS version I converted it from.
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;
}