问题
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...
回答1:
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.
来源:https://stackoverflow.com/questions/52685240/how-to-create-3d-tile-maps-using-a-plane-object-divided-into-tiles