This is the key. Always great when you can think about the logic at a high level first. Like you said, a block should only break if:
- The player is above it (ie touching it)
- The player clicks the dig buttons while (1) is true.
To answer why you cant select OnCollisionEnter2D
from the functions list: Acceptable function signatures that can be selected from this drop down menu must be:
- public
- accept 0 or 1 arguments (only of type float, int, string, bool or unity object).
OnCollisionEnter2D
unfortunately doesn’t fit that requirement as it takes a Collision2D object as it’s one argument.
That said, if you think about it a bit more, you might see that your OnCollisionEnter2D
function shouldn’t be the function handling the deletion/block breaking in the first place. Now theres many ways you can do this, but let me suggest one approach.
Let OnCollisionEnter2D
simply turn on a flag that indicates your player is touching the ground (if indeed it is)
public void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Ground"
{
touchingGround = true;
}
}
Additionally, don’t forget to flip this flag to false whenever your player is not touching some breakable ground (or jumping).
where touchingGround
is a boolean defined as a class member.
Now, create another function called OnDigButtonPressed
that you can hook up to your button. Now everytime the button is pressed, this function will check if the player is touching the ground. If they are, then go ahead and find/destroy the object below you.
public void OnDigButtonPressed()
{
if (touchingGround)
{
<<code to delete the tile/block/object right below the player>>
}
}
Alternatively, if you dont want to search / get the object below you everytime, you could cache it on the OnCollisionEnter2D
call, and the try and destroy it in the OnDigButtonPressed
.