Trigger for every tilemap square.
Hello , I have a question and i am hoping you guys can help me out, I am trying to make it so that every time my "hero" walks over specific tiles he would have a chance of getting into a fight "pokemon" style and I want to add that to the tile map, I have been able to make a second tile map and added a script to it that makes it every time the hero enters the area of the second tile map it will calculate if he enters a fight or not. But the problem is its not for every Square of the tile map it for the whole zone that it calculates it for. Please Help Best Regards.
this is the script i am using
void OnTriggerEnter2D(Collider2D col) { if (col.CompareTag ("Player")) {
int a = Random.Range(0, 100);
if (a <= 70)
{
Debug.Log("You are now in a fight");
}
}
}
Answer by DawdleDev · Apr 29, 2018 at 08:48 PM
There is a MUCH better way to do this. First, create a 2D list, where each item represents a tile on the map. Then, each entry needs have the odds of encountering an enemy in that tile in percentages from 0 to 1 (if you just want possible or not possible, just use 0 and 1). You should probably define it through code and NOT manually, or you'll be doing it for the rest of your life. Then, what you do in the player movement script is have a set of two integers that shows the tile that the player is currently in (if you're using something that moves smoothly rather than jumping from tile to tile, use rounding for storing the position). Each time the player moves, check to see at the index of the player position using your code:
int a = Random.Range (0, 100 * (1 - OddsOfEncounteringAnEnemy[Player.position]))
if (a <= 70) {
Debug.Log("You are now in a fight");
}
Hope this helps!
Your answer
