简体   繁体   English

如何使用脚本在特定位置打洞(统一)

[英]How can I make a hole at a specific place using script (unity)

I am working on making a hole in the terrain in real-time with a mouse click.我正在通过鼠标点击实时在地形上打一个洞。 I found this script which allows the user to raise terrain at a circle shape with a mouse click and I tried to add the code to generate a hole to it like so:我找到了这个脚本,它允许用户通过单击鼠标将地形提升为圆形,我尝试添加代码以生成一个洞,如下所示:

void PaintCircle(Vector3  point )
{
    int x ;
     int z;
     int heightX;
     int heightZ;
      float heightY;
    Vector2 calc ;
     var b = new bool[heightmapWidth-1, heightmapHeight-1];

    for (z = -circleRadius; z <= circleRadius; z++)
    {
        for (x = -circleRadius; x <= circleRadius; x++)
        {
            // for a circle, calcualate a relative Vector2
            calc = new Vector2(x, z);
            // check if the magnitude is within the circle radius
            if (calc.magnitude <= circleRadius)
            {
                // is within circle, paint height
                heightX = (int)(point.x + x);
                heightZ = (int)(point.z + z);

                // check if heightX and Z is within the heightmapData array size
                if (heightX >= 0 && heightX < heightmapWidth && heightZ >= 0 && heightZ < heightmapHeight)
                {
                    // read current height
                    heightY = heightmapData[heightZ, heightX]; // note that in heightmapData, X and Z are reversed

                    // add paintWeight to the current height
                    heightY -= paintWeight;
                    // reach the bottom terrain.terrainData.GetHeight((int)heightmapPos.x, (int)heightmapPos.z)

                    // update heightmapData array
                    heightmapData[heightZ, heightX] = heightY;
                    b[heightZ, heightX] = (heightX < 20 && heightX > 80 && heightZ < 20 && heightZ > 80);
                    

                }
                

            }
           
        }
    }
     terrain.terrainData.SetHoles(0, 0, b);
    // apply new heights to terrainData
    terrainData.SetHeights(0, 0, heightmapData);
 }

I added the raise terrain and the hole together because I need them to work together.我将高地地形和洞一起添加,因为我需要它们一起工作。 Unfortunately, this did not work all of the terrains disappeared so I tried to invert the area where the hole should be like this:不幸的是,这并没有奏效,所有的地形都消失了,所以我试图反转洞应该是这样的区域:

b[heightZ, heightX] = !(heightX < 20 && heightX > 80 && heightZ < 20 && heightZ > 80);

but it gave me what I want in reverse, the area where the hole should remain and all the other terrain disappeared so I thought that the problem could be in this line但它给了我我想要的东西,洞应该保留的区域和所有其他地形都消失了,所以我认为问题可能出在这条线上

 var b = new bool[heightmapWidth-1, heightmapHeight-1];

so I changed it to this:所以我把它改成这样:

  bool[,]  b = terrainData.GetHoles(0, 0, heightmapWidth, heightmapHeight);

but it gave me this error:但它给了我这个错误:

ArgumentException: Trying to access out-of-bounds terrain holes information.
UnityEngine.TerrainData.GetHoles (System.Int32 xBase, System.Int32 yBase, System.Int32 width, System.Int32 height) 

I am out of ideas so I am hoping if someone can tell me what is the problem and how can I fix it我没有想法,所以我希望有人能告诉我问题是什么,我该如何解决

the solution was this:解决方案是这样的:

  bool[,]  b = terrainData.GetHoles(0, 0, heightmapWidth-1, heightmapHeight-1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM