Pushable Boxes in 3d ISO game
I am trying to make my puppet character push boxes using tag collision.
I have 1 box collider for each side of the box. they each have a tag that matches their name.
the player controller script I wrote, checks for collision on either of those box colliders, using the tag, and if it's true, it sets a bool true for the matching side. when this bool is set true, it enables the player the option to press Fire1, and if they do, another bool for grabbing is set to true, also respectively for what side of the box.
//Collision Detection for "Box"
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "BoxZ")
{
{
rBODY.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY;
}
BOXZ = true;
}
if (col.gameObject.tag == "BoxX")
{
{
rBODY.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY;
}
BOXX = true;
}
if (col.gameObject.tag == "BoxZdown")
{
{
rBODY.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY;
}
BOXZdown = true;
}
if (col.gameObject.tag == "BoxXdown")
{
{
rBODY.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY;
}
BOXXdown = true;
}
}
on the box i have a script that checks is this bool is true on the character controller, and then it parents the Box to the player. this works fine except they share tags and thus both boxes in my scene then become children of my puppet here.
void Update()
{
if ((GameObject.Find("PUPPET").GetComponent<AnimCon>().grabZ) || (GameObject.Find("PUPPET").GetComponent<AnimCon>().grabZdown) || (GameObject.Find("PUPPET").GetComponent<AnimCon>().grabX) || (GameObject.Find("PUPPET").GetComponent<AnimCon>().grabXdown)) //will check if true
{
BOX.transform.parent = PUPPET.transform;
}
else if ( (!GameObject.Find("PUPPET").GetComponent<AnimCon>().grabZ) || (!GameObject.Find("PUPPET").GetComponent<AnimCon>().grabZdown) || (!GameObject.Find("PUPPET").GetComponent<AnimCon>().grabX) || (!GameObject.Find("PUPPET").GetComponent<AnimCon>().grabXdown)) //will check if false
{
BOX.transform.parent = null;
}
}
void LateUpdate()
{
transform.rotation = rotation;
}
Am I going about this correctly? or is there a better way to move these objects.