Help Me Convert C# Into JavaScript Please

Hello,

I'm trying to convert a C# script into a JavaScript, and I am running into a few difficulties. As I have no idea about C# its kind of tedious. If you could look at the code below and read the comments then that would be great:

//using UnityEngine; // << insert ;
//using System.Collections; // << insert ;

//public class LightningBolt : MonoBehaviour
//{
    var target : Transform;
    private var zigs : int = 100;
    private var speed : float = 0.8f;
    private var scale : float = 0.4f;
    var startLight : Light;
    var endLight : Light;

    //Perlin noise; // << having a problem with this
    var oneOverZigs : float;

    private var particles : Particle[];

    function Start()
    {
        oneOverZigs = 1f / zigs;
        particleEmitter.emit = false;

        particleEmitter.Emit(zigs);
        particles = particleEmitter.particles;
    }

    function Update ()
    {
        if (noise == null) // << Unknown identifier
            noise = new Perlin();

        timex = Time.time * speed * 0.1365143f;
        timey = Time.time * speed * 1.21688f;
        timez = Time.time * speed * 2.5564f;

        for (i=0; i < particles.Length; i++)
        {
            Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * i); // << insert ;
            Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z), // << insert ;
                                        noise.Noise(timey + position.x, timey + position.y, timey + position.z),
                                        noise.Noise(timez + position.x, timez + position.y, timez + position.z));
            position += (offset * scale * (i * oneOverZigs));

            particles*.position = position;*
 _particles*.color = Color.white;*_
 <em>_particles*.energy = 1f;*_</em>
 <em>_*}*_</em>
 <em>_*particleEmitter.particles = particles;*_</em>
 <em>_*if (particleEmitter.particleCount >= 2)*_</em>
 <em>_*{*_</em>
 <em>_*if (startLight)*_</em>
 <em>_*startLight.transform.position = particles[0].position;*_</em>
 <em>_*if (endLight)*_</em>
 <em>_*endLight.transform.position = particles[particles.Length - 1].position;*_</em>
 <em>_*}*_</em>
 <em>_*}*_</em> 
<em>_*//}*_</em>
<em>_*```*_</em>

Hi oliver,

This is all related with the Perlin class. In case you have the class in your project you could define the variable as:

var noise : Perlin;
noise = new Perlin();

But I think Perlin is not in .NET libraries, so maybe you have the class Perlin. Indeed I found all this info, which I guess you alredy have ;) (though new in unity to me, not in signal processing). Link1, link2, link3 I will take a look to the last link when I will have some free time.

About the imports, you should

import System.Collections;

cheers

The script you're working with assumes that you have the Perlin script in your assets. It can be found in the procedural shader example project, which you can download here.

For the most part you've got the script right.

Perlin noise;

is just another C# declaration of variable. in javascript it would be

var noise : Perlin;

This is the declaration of the variable noise. Right now your script hasn't declared it (commented out in c# format), so thats why you're getting the unknown identifier problem.

Your semicolon issues are there because those Vector3s are also being declared in the c# form.

Instead of

Vector3 position = ...
Vector3 offset = ...

these should say

var position : Vector3 = ...
var offset : Vector3 = ...

You also don't need to use new when creating a new Vector3.

Beyond that, you should be good, providing you have the Perlin script from the example project in the proper place in your assets.