如何使用分为多个图块的“平面"对象创建3D图块地图?
我想在3D世界空间中创建一个以0,0,0为中心的2D平面.然后,我想在2D瓷砖坐标中定义此平面.所以例如一个由3个图块划分的平面将成为数组[3,3]的映射
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]
问题在于,这仍然会产生带有负点的图块.世界左上角-1,1在数组-1中为0,0,-将为1,0,依此类推...
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...
世界:
-1,1 0,1 1,1
-1,0 0,0 1,0
-1,-1 0,-1 1,-1
数组:
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
我对Unity的主要希望是我可以避免图形数学,而专注于逻辑脚本.因此,我问Unity 2018是否具有可以轻松实现我上面描述的功能的任何一组功能?
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?
从长远来看,使用此代码的原因是要创建游戏&编辑器,用于将3d预制件放置到3d世界中,但使用2D数组定义其属性.y轴混乱目前不是问题.我想知道是否可以使用此工具创建一个开源XCOM风格的游戏.我目前无法负担资产,因为我在经济上很依赖并且不喜欢询问.我也注意到免费3D瓷砖游戏编辑器也很匮乏.感谢您的帮助...
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...
要将2d平面映射到2d数据数组:
To map a 2d plane to a 2d data array:
-
使用numTiles和tileSize在3d空间中创建一个网格.它的宽度为(numTiles * tileSize),因此该平面的顶点与Vector3.zero点的距离为该值的一半.
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
在Update()函数中,使用光线投射检测网格上的鼠标单击.我使用了来自Camera.main.ScreenPointToRay(Input.mousePosition)的射线,并检查是否从飞机的MeshCollider受到了撞击.使用该命中点的数据,我能够将float值映射到整数xz坐标.
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.
要将xz整数坐标映射到数据数组的行r和列c:
To map an xz integer coordinate to the row r and column c of a data array:
它比我喜欢的要多,但它能起作用.
It's more math than I like but it works.