I am working on a puzzle game, I implemented a pushblock puzzle and got everything working, but I decided to go back and add another kind of block that can’t be used to finish the puzzle, but can be used to help the other block get to the end of the puzzle by filling in holes in the ground, both blocks can fill in the hole though, I coppied my code from the puzzle end script to use as a base for the hole filling script, but it for some reason won’t work, here is both scripts for the main block in one block of code,
using UnityEngine;
public class PushPuzzleScript : MonoBehaviour
{
public Transform bs;
Rigidbody2D m_Rigidbody;
public Transform pbs;
public Transform hole;
bool inHole = false;
void Start()
{
m_Rigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (transform.position != bs.position)
{
if (Input.GetKey("r"))
{
transform.position = pbs.position;
if (inHole == true)
{
m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
}
}
}
}
void OnCollisionEnter2D(Collision2D collisionInfo)
{
if (collisionInfo.collider.tag == "Block Switch")
{
transform.position = bs.position;
m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
}
if (collisionInfo.collider.tag == "Hole")
{
transform.position = hole.position;
m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
inHole = true;
Debug.Log("The Push Block Fell In The Hole!");
}
}
}
I am trying to figure out why this code isn’t working, the top if statement in the on collision enter 2D works perfectly fine, however, the second one doesn’t, the code for the second block type has the same problem and can be seen here
using UnityEngine;
public class PushCrate : MonoBehaviour
{
Rigidbody2D m_Rigidbody;
public Transform hole;
public Transform pcs;
public Transform pb;
public Transform bs;
bool inHole = false;
// 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.GetKey("r"))
{
transform.position = pcs.position;
if (inHole == true)
{
m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
}
}
}
}
void OnCollisionEnter2D(Collision2D collisionInfo)
{
if (collisionInfo.collider.tag == "Hole")
{
transform.position = hole.position;
m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
inHole = true;
}
}
}
I have also checked to see if it is something to do with unity itself, but that does not appear to be the case, the hole and the end pad for the puzzle have the same exact settings on everything, the only difference is the sprite and the tag itself as well as the layer. All help is apreciated!
