Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jaovssilva · Apr 27, 2014 at 11:37 PM · grid

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
     }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jaovssilva · Apr 28, 2014 at 01:01 AM 0
Share

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);
     }
 }
avatar image robertbu · Apr 28, 2014 at 01:15 AM 0
Share

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); }

avatar image Jaovssilva · Apr 28, 2014 at 01:33 AM 0
Share

Thank you very much!

avatar image Jaovssilva · Apr 28, 2014 at 01:42 AM 0
Share

One more question, how could I translate this:

 foreach(GameObject obj in objects)
          obj.SetActive(ShowGrid);
avatar image robertbu · Apr 28, 2014 at 01:52 AM 0
Share
 for (obj : GameObject in objects)
     obj.SetActive(ShowGrid);
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to fill a volume with a procedurally generated 3d voxel grid? 0 Answers

Accessing C# from Javascript? 2 Answers

2D Game Visual Grid to Place Objects 2 Answers

Grid Based Movement (Pokemon-like) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges