如何使用划分为瓦片的平面对象创建 3D 瓦片地图? [英] How to create 3D tile maps using a Plane object divided into tiles?
问题描述
我想在 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...
推荐答案
将二维平面映射到二维数据数组:
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 中得到了命中.使用该命中点的数据,我能够将浮点值映射到整数 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.
这篇关于如何使用划分为瓦片的平面对象创建 3D 瓦片地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!