- Home /
Lightning Script Js To C# Conversion errors
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;
}
Answer by Chronos-L · Jun 03, 2013 at 02:00 PM
You should know that by default, 2.0
is considered to be a float
in uJS. So you should change all the related numbers to a float when you convert it to C#. By the way, if you read the error carefully, you will know how to fix it:
Assets/Lightning.cs(26,8): error CS1502: The best overloaded method match for
Lightning.Randomize(UnityEngine.Vector3, float)' has some invalid arguments**: One or more of the parameter is not of the correct type - **Assets/Lightning.cs(26,8): error CS1503: Argument #2' cannot convertdouble' expression to type
float': Argument/Parameter #2 should be a double and the current value for the said parameter cannot be converted to a doubleCorrection
using UnityEngine; using System.Collections;
public class Lightning : MonoBehaviour {
public GameObject target; public LineRenderer LR; //Just change all these to float public float arcLength = 2.0f; public float arcVariation = 2.0f; public float inaccuracy = 1.0f; public Vector3 fwd; void Update() { ... } private Vector3 Randomize(Vector3 v3 , float inaccuracy2) { //You need the new keyword in C# v3 += new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * inaccuracy2; v3.Normalize(); return v3; } }
Answer by SubatomicHero · Jun 03, 2013 at 02:00 PM
You need to change all your variables to floats like so:
public float arcLength = 2.0f;
public float arcVariation = 2.0f;
public float inaccuracy = 1.0f;
Hope this helps.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Please Help Convert js to c# 1 Answer
Converting to C# 1 Answer
Spawn Meteors At random location C# 1 Answer
how can convert this in js? 1 Answer