1

I want to create a 2D plane centered on 0,0,0 in 3D world space. Then, I want to define this plane in 2d tile coordinates. So e.g. a plane divided by 3 tiles would become a map of an array [3,3]

The issue is that this would still give a tilemap with negative points. The world upper left -1,1 would be 0,0 in the array -1,- would be 1,0 and so on...

World:

-1,1   0,1   1,1             
-1,0   0,0   1,0
-1,-1  0,-1  1,-1 

Array:

0,0  0,1  0,2   
1,0  1,1  1,2
2,0  2,1  2,2   

My main hope with Unity was that I could avoid the math of graphics and focus on logic scripting. So, I am asking if Unity 2018 has any group of functions that could easily do what I described above?

The reason for this code would be, in the long run, to create a game & editor for placing 3d prefabs into a 3d world but using 2D arrays to define their properties. Messing with the y-axis is not currently an issue. I am wondering if I can create an opensource XCOM style game using this. I currently can't afford assets since I am financially dependent and don't like asking. I have noticed a strong lack of free 3D tile game editors too. Thanks for the help...

Anoop Alex
  • 11
  • 2
  • To clarify: you want a function to automatically divide an arbitrarily sized 2d plane into n sections and have the raycast return the section [0-n]? – pale bone Oct 07 '18 at 04:05
  • yes. 0,0 being the center section of the plane – Anoop Alex Oct 08 '18 at 10:40
  • also to clarify, i have a method that does this. In my update() i use the following: ray = Camera.main.ScreenPointToRay(Input.mousePosition); – Anoop Alex Oct 08 '18 at 11:33
  • then in a mouseclick: if (coll.Raycast(ray, out hitInfo,100f)) gives me the hitinfo values i need. This keeps working even as I translate the Camera position. But it breaks as soon as I rotate the position.by using Camera.main.transform.Rotate(new Vector3(0,-45,0),Space.World); The starting position of the camera is (0, 20, -40) and its rotation is (30,0,0) – Anoop Alex Oct 08 '18 at 11:41
  • I am going to edit the OP to update with the info i presented in these comments. Still learning how to use SO. – Anoop Alex Oct 08 '18 at 11:54

1 Answers1

0

To map a 2d plane to a 2d data array:

  • Using numTiles and tileSize, create a mesh in 3d space. It will have a width of (numTiles * tileSize) so the vertices of the plane with have a distance of half that value from the point Vector3.zero

  • In your Update() function, use raycasting to detect mouseclicks on the mesh. I used a ray from Camera.main.ScreenPointToRay(Input.mousePosition) and checked if I got a hit from the MeshCollider of my plane. Using the data of that hitpoint, I was able to map the float values into integer xz-coordinates.

  • To map an xz integer coordinate to the row r and column c of a data array:

  • shiftX = x + Floor(numTiles / 2)
  • shiftZ = z - Floor(numTiles / 2)
  • r = Floor(shiftZ)*-1
  • c = Floor(shiftX)

It's more math than I like but it works.

Anoop Alex
  • 11
  • 2