- Home /
Convert C# to JS
Hi good night
I was looking for an algorithm to make a grid on the ground, found in C # but I need to be in JS. I started the conversion but did not get a part, I would like somebody's help. Original algorithm:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Grid : MonoBehaviour {
public Terrain terrain; //terrain grid is attached to
public bool ShowGrid = false;
public int CellSize = 10;
private Vector3 terrainSize;
private Vector3 origin;
private int width;
private int height;
private List<GameObject> objects;
void Start ()
{
terrainSize = terrain.terrainData.size;
origin = terrain.transform.position;
width = (int)terrainSize.z / CellSize;
height = (int)terrainSize.x / CellSize;
objects = new List<GameObject>();
BuildGrid();
}
void Update ()
{
foreach(GameObject obj in objects)
obj.SetActive(ShowGrid);
}
void BuildGrid()
{
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
Vector3 pos = GetWorldPosition(new Vector2(x,y));
pos += new Vector3(CellSize / 2, terrain.SampleHeight(GetWorldPosition(new Vector2(x,y))), CellSize /2);
go.transform.position = pos;
if(x==0)
{
go.renderer.material.color = Color.red;
}
if(y==0)
go.renderer.material.color = Color.green;
go.transform.localScale = new Vector3(CellSize /2, CellSize /2, CellSize/2);
go.transform.parent = transform;
go.SetActive(false);
objects.Add(go);
}
}
}
public Vector3 GetWorldPosition(Vector2 gridPosition)
{
return new Vector3(origin.z + (gridPosition.x * CellSize), origin.y, origin.x + (gridPosition.y * CellSize));
}
public Vector2 GetGridPosition(Vector3 worldPosition)
{
return new Vector2(worldPosition.z / CellSize, worldPosition.x / CellSize);
}
}
Part I was unable to convert:
public Vector3 GetWorldPosition(Vector2 gridPosition)
{
return new Vector3(origin.z + (gridPosition.x * CellSize), origin.y, origin.x + (gridPosition.y * CellSize));
}
public Vector2 GetGridPosition(Vector3 worldPosition)
{
return new Vector2(worldPosition.z / CellSize, worldPosition.x / CellSize);
}
Thanks for the help already.
Answer by robertbu · Apr 28, 2014 at 12:06 AM
Looking just at the lines you had trouble with:
function GetWorldPosition(gridPosition : Vector2 ) : Vector3
{
return Vector3(origin.z + (gridPosition.x * CellSize), origin.y, origin.x + (gridPosition.y * CellSize));
}
function GetGridPosition(worldPosition : Vector3) : Vector2
{
return Vector2(worldPosition.z / CellSize, worldPosition.x / CellSize);
}
did not work, here the full algorithm, can you please take a look at me?
#pragma strict
var terrain : Terrain;
var CellSize : int;
var terrainSize : Vector3;
var origin : Vector3;
var width : int;
var height : int;
var pos : Vector3;
var go : GameObject;
function Start ()
{
CellSize = 10;
terrainSize = terrain.terrainData.size;
origin = terrain.transform.position;
width = terrainSize.z / CellSize;
height = terrainSize.x / CellSize;
BuildGrid();
}
function Update ()
{
}
function BuildGrid()
{
for(var x = 0; x < width; x++)
{
for(var y = 0; y < height; y++)
{
go = GameObject.CreatePrimitive(PrimitiveType.Cube);
pos = GetWorldPosition(new Vector2(x,y));
pos += new Vector3(CellSize / 2, terrain.SampleHeight(GetWorldPosition(new Vector2(x,y)), CellSize /2);
go.transform.position = pos;
go.transform.localScale = new Vector3(CellSize, CellSize, CellSize);
go.transform.parent = transform;
go.renderer.material.color = Color.blue;
}
}
function GetWorldPosition(gridPosition : Vector2 ) : Vector3
{
return Vector3(origin.z + (gridPosition.x * CellSize), origin.y, origin.x + (gridPosition.y * CellSize));
}
function GetGridPosition(worldPosition : Vector3) : Vector2
{
return Vector2(worldPosition.z / CellSize, worldPosition.x / CellSize);
}
}
I did a quick pass and fixed the problems that were preventing your code from compiling. I did not do a line-by-line comparison with the original C# source code. The two problems I fixed were:
On line 35, you had deleted a ')' for some reason.
You had the GetWorldPosition() and GetGridPosition() functions nested inside the BuildGrid() function.
I also removed an unnecessary 'new' operation on line 35. I would not have hurt anything, but it is not required for Javascript.
pragma strict
var terrain : Terrain; var CellSize : int; var terrainSize : Vector3; var origin : Vector3; var width : int; var height : int; var pos : Vector3; var go : GameObject;
function Start () { CellSize = 10; terrainSize = terrain.terrainData.size; origin = terrain.transform.position; width = terrainSize.z / CellSize; height = terrainSize.x / CellSize; BuildGrid(); }
function Update () {
}
function BuildGrid() { for(var x = 0; x < width; x++) { for(var y = 0; y < height; y++) { go = GameObject.CreatePrimitive(PrimitiveType.Cube); pos = GetWorldPosition(new Vector2(x,y)); pos += Vector3(CellSize / 2, terrain.SampleHeight(GetWorldPosition(new Vector2(x,y))), CellSize /2); go.transform.position = pos; go.transform.localScale = new Vector3(CellSize, CellSize, CellSize); go.transform.parent = transform; go.renderer.material.color = Color.blue; } } }
function GetWorldPosition(gridPosition : Vector2 ) : Vector3 { return Vector3(origin.z + (gridPosition.x CellSize), origin.y, origin.x + (gridPosition.y CellSize)); }
function GetGridPosition(worldPosition : Vector3) : Vector2 { return Vector2(worldPosition.z / CellSize, worldPosition.x / CellSize); }
One more question, how could I translate this:
foreach(GameObject obj in objects)
obj.SetActive(ShowGrid);
for (obj : GameObject in objects)
obj.SetActive(ShowGrid);