How can I reset a variable for the opposite labeled object?
Hi there!
I'm working on a game with a 2D overworld map built out of tiles using Unity's tilemap system. While the character traverses the map, a manager gleans what kind of terrain (Desert, Plains, Forest, Hills, etc.) the character is walking over.
This works decently right now with what I have.
However, I have a situation where I need two tilemaps to overlap on occasion, I have both overlapping tiles correctly being "sensed", but when I leave an area that is overlapping, the opposing layer data doesn't reset to "NONE".... Honestly, it's probably because my code isn't telling it to reset, but... I've been around the block trying to figure out how to get the layers to reset correctly AND keep the correct data.
To be a bit more specific, I provide both overlapping layers their own tags (Region and UnRegion (Un for Under)), I then look at whether the trigger has one of these tags and then request the region type from the tile gameObject.
How would I get this to reset the data to "NONE" for the opposing tagged object once one of the regions are no longer overlapping?
Here's the code in question:
void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Region"))
{
region = other.gameObject.GetComponent<RegionData>().regionType;
}
if (other.CompareTag("UnRegion"))
{
//region = RegionData.RegionType.NONE;
unregion = other.gameObject.GetComponent<RegionData>().regionType;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Region"))
{
region = other.gameObject.GetComponent<RegionData>().regionType;
}
if (other.CompareTag("UnRegion"))
{
unregion = other.gameObject.GetComponent<RegionData>().regionType;
}
}
Thanks for the assistance in advance.
Answer by Vaulcul · Dec 16, 2020 at 06:39 PM
I think I figured this out.
The code that ended up doing what I needed is:
void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Region"))
{
region = RegionData.RegionType.NONE;
}
if (other.CompareTag("UnRegion"))
{
unregion = RegionData.RegionType.NONE;
}
}
void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag("Region"))
{
region = other.gameObject.GetComponent<RegionData>().regionType;
}
if (other.CompareTag("UnRegion"))
{
unregion = other.gameObject.GetComponent<RegionData>().regionType;
}
}