- Home /
Checking the probability of an attack in certain tiles
Hi everyone! I have a problem writing code that checks the chance of attacking a player in certain tiles. I would like to implement the start of the fight as in Undertale. That is, you just run around the location and with some chance you can be attacked by monsters. The problem is that I can't implement it. All I did was find all the hostile tiles on the map and also determine which tile the player is currently on. Here's my code:
[SerializeField] private BoundsInt area;
 [SerializeField] private List<TileBase> battleTiles;
 [SerializeField] private LayerMask whatIsPlayer;
 void Awake()
 {
     //set tile center
     Vector2 point = new Vector2(area.position.x + 0.5f, area.position.y + 0.5f);
     Tilemap tilemap = GetComponent<Tilemap>();
     TileBase[] allTiles = tilemap.GetTilesBlock(area);
     for (int x = 0; x < area.size.x; x++)
     {
         for (int y = 0; y < area.size.y; y++)
         {
             TileBase tile = allTiles[x + y * area.size.x];
             if (tile != null)
             {
                 //adding hostile tile to list
                 battleTiles.Add(tile);
             }
             //Find player in current tile
             Collider2D colInfo = Physics2D.OverlapBox(point, new Vector2(1, 1), 0, whatIsPlayer);
             if (colInfo != null)
             {
                 if (colInfo.GetComponent<Player_Movement>() != null)
                 {
                     //Set coordinates
                     colInfo.GetComponent<Player_Movement>().x = x;
                     colInfo.GetComponent<Player_Movement>().y = y;
                 }
             }
             point += Vector2.up;
         }
         point += Vector2.right;
         point.y = area.position.y + 0.5f;
     }
 }
P.S. I wrote the code using this answer: https://answers.unity.com/questions/1500125/trigger-for-every-tilemap-square.html
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                