- Home /
Javascript to C#: Vector 3 rigidbody
Hi... trying to convert a JS file to C#. I'm having issues with the rigidbody.velocity = Vector3... here's the code in JS:
function Start () {
rigidbody.velocity = Vector3(Random.Range(-2,2),Random.Range(3,5),0);
}
in conversion, I get the error, "Assets/Scripts/heartdrop.cs(8,38): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected" But I don't understand this! thanks!
Answer by robertbu · Mar 02, 2014 at 11:51 PM
For C# you need the 'new' keyword in front of the 'Vector3'. Also note your Random.Range() call is using the integer version of Random.Range(). The integer version is exclusive of the final value, and will only return integers. So Random.Range(-2, 2) will only return the values -2, -1, 0, and 1. I'm guessing you want the float version. So putting the two together:
rigidbody.velocity = new Vector3(Random.Range(-2.0,2.0),Random.Range(3.0,5.0),0.0);
Thanks! I'm having an issue with another section:
if(touched == true){
rigidbody.velocity.x = 0.0f;
}
and
if(rigidbody.velocity.y > 0.5){
rigidbody.velocity.x = playerDistance*1.5;
}
Still having issues with the float/Vector3 thing...