I am trying to figure out how to do this so that I can easily make push block puzzles have multiple holes blocking the exit but allow for more than one of the same solution, would I use on collision enter 2D? if so, how would I then reference the collider of that hole again to reset the puzzle if the player messes up and lets the block that activates the switch at the end fall into a hole? as of right now I have the code set up for only 1 hole, here is that code.
using UnityEngine;
public class PushCrate : MonoBehaviour
{
Rigidbody2D m_Rigidbody;
public Transform hole;
public BoxCollider2D hbc;
public BoxCollider2D cbc;
public Transform pcs;
public Transform pb;
public Transform bs;
bool inHole = false;
public Vector3 offset;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
m_Rigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (pb.position != bs.position)
{
if (Input.GetKeyDown(KeyCode.R) == true)
{
transform.position = pcs.position;
if (inHole == true)
{
m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
hbc.GetComponent<Collider2D>().enabled = true;
cbc.GetComponent<Collider2D>().enabled = true;
}
}
}
}
void OnCollisionEnter2D(Collision2D collisionInfo)
{
if (collisionInfo.collider.tag == "Hole")
{
transform.position = hole.position + offset;
m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
hbc.GetComponent<Collider2D>().enabled = false;
cbc.GetComponent<Collider2D>().enabled = false;
inHole = true;
}
}
}