English 中文(简体)
我如何在团结的无能的地图中清除tile子?
原标题:How do I remove tiles in a tilemap in unity?

我有一块微薄的地图(Tl),当火箭击中它时,某些半径的碎片应当销毁。

我曾试图利用GetTilesBlock和Destroy功能,但我没有工作。

public void OnCollisionEnter2D(Collision2D collision)
    {
        
            int posx = (int)transform.position.x;
            int posy = (int)transform.position.y;
            int posz = (int)transform.position.z;
            Vector3Int pos = new Vector3Int(posx, posy, posz);
            Vector3Int radiusBox = new Vector3Int(3, 3, 3);
            BoundsInt Box = new BoundsInt(pos, radiusBox);
            TileBase[] tiles = Tl.GetTilesBlock(Box);
            foreach(TileBase x in tiles)
            {
                Destroy(x);
            }
            

        Destroy(gameObject);
    }
问题回答

What I did was loop through all tiles within a box with sides of the radius and then check distance to make it a circle. If that sounds confusing then heres my code. A little messy but it gets the job done.

        Vector2 pos = transform.position; //center of the circle

    ground = FindAnyObjectByType<Tilemap>();//Get the tilemap

    for (int x = -ExplosionRadius; x < ExplosionRadius; x++)
    {
        for (int y = -ExplosionRadius; y < ExplosionRadius; y++) //find the box
        {
            Vector3Int Tilepos = ground.WorldToCell(new Vector2(pos.x + x, pos.y + y));
            if (Vector3.Distance(pos, Tilepos) <= ExplosionRadius) //check distance to make it a circle
            {
                ground.SetTile(Tilepos, null);
            }
        }
    }

Hope this helps.





相关问题
c# wpf 2d graphics - Looking for tutorial / examples [closed]

Could you please point me to a good C# tutorial for drawing 2d graphics like Ellipse and Rectangle (that inherit from Shape) on a Canvas using WPF ? I m also interested later to click on shapes and ...

How to multiply two sprites in SpriteBatch Draw XNA (2D)

I am writing simple hex engine for action-rpg in XNA 3.1. I want to light ground near hero and torches just as they were lighted in Diablo II. I though the best way to do so was to calculate field-of-...

2D Ball Collisions with Corners

I m trying to write a 2D simulation of a ball that bounces off of fixed vertical and horizontal walls. Simulating collisions with the faces of the walls was pretty simple--just negate the X-velocity ...

2D Mathematics Geographical Directions

I am trying to check the location of a point(px, py) on 2D graph in relation to a line segment (lx1, ly1) (lx2, ly2), using logic of North South East West directions. The logic I have implemented is ...

Perpendicular on a line from a given point

How can I draw a perpendicular on a line segment from a given point? My line segment is defined as (x1, y1), (x2, y2), If I draw a perpendicular from a point (x3,y3) and it meets to line on point (x4,...

How to select a line

So I m trying to figure out how to implement a method of selecting lines or edges in a drawing area but my math is a bit lacking. This is what I got so far: A collection of lines, each line has two ...

How do I generate a contour graph?

How do I generate a contour graph like this: contour http://www.fz-juelich.de/vislab/software/gsharp/Gsharp/userguide/interpolate/ex6.gif It s easy enough if the points are on a regular grid, but ...

Mapping points from Euclician 2-space onto a Poincare disc

For some reason it seems that everyone writing webpages about Poincare discs is only concerned with how to represent lines and measure distances. I d like to morph a collection of 2D points (as ...

热门标签