Hey guys! Here’s the situation: I have a game in which you place blocks. You have a prespawn block that takes your mouse position. If this block is over another one already placed on the world, you will NOT be able to place it. So no blocks will be over others.
EVERYTHING Is in 2D space! But the prespawn block is over all of the other placed blocks!
Here’s what I did: I put a script on the prespawn block that verify if there’s a block trigerring it. I put:
public bool onTrigger;
void OnTriggerEnter2D()
{
onTrigger = true;
}
void OnTriggerExit2D()
{
onTrigger = false;
}
void OnTriggerStay2D()
{
onTrigger = true;
}
/*on other script*/
void Update()
{
if(Input.GetMouseButton(0) && !onTrigger) Spawn();
}
Here’s the problem: The function OnTriggerExit2D is too fast, so even if the onTrigger is false for one single frame and then true during the other, it placed the block.
2nd problem: If I put 2 blocks one next to the other and I move my prespawn object from the exact position of the 1st block to the other, It will exit the first block’s collider to enter the second’s one. It’s during that frame that onTrigger becomes false when it’s not suppose to. So here’s the 2nd question: is there a way I can use the OnTriggerExit2D function to set the variable onTrigger ONLY when there’s absolutly NO block triggering? It will be really usefull!
Thanks