- Home /
FixedJoint for wall
I'm creating a system to break the wall, one with the fragmented mesh like this
But first I'm doing unity tests with a wall made of cubes.
1.the Script is assigned the rigidbody of the fragments that it has around it creates a FixedJoint and assigns the rigidbody and when I have fired and ends the fragment life eliminates the FixedJoint that it has with the surrounding blocks and activates rigidbody. Gravity to fall.
2.but if you stay with one you'll still be on the wall by the FixedJoint
3.my problem is when you don't have any block attached to the wall should drop the block but as they still detect the fixedJoint blocks that are also not attached to the wall does not fall, I do not know what is the best way to do it
This is the code that handles everything
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallSystem : MonoBehaviour, IHurt
{
[SerializeField]
private Rigidbody[] _RigidFixed;
private Rigidbody _rigid;
public FixedJoint[] fixeds;
[Space(10)]
[SerializeField]
private int lifeBlock = 2, speed;
private int i = 0;
public bool canInput;
private void Start()
{
fixeds = new FixedJoint[_RigidFixed.Length];
_rigid = GetComponent<Rigidbody>();
foreach (Rigidbody rigid in _RigidFixed)
{
FixedJoint fixeJoint = gameObject.AddComponent<FixedJoint>();
fixeJoint.connectedBody = rigid;
fixeds[i] = fixeJoint;
i++;
}
}
#region DestroyFixedJoint
public void Damage(int damage)
{
lifeBlock -= damage;
if (lifeBlock <= 0)
{
StartCoroutine(EliminateFixedJoint());
_rigid.constraints = RigidbodyConstraints.None;
_rigid.useGravity = true;
_rigid.AddRelativeForce(Random.onUnitSphere * speed);
}
}
IEnumerator EliminateFixedJoint()
{
while (true)
{
if (gameObject.GetComponent<FixedJoint>() == null)
break;
destroyThisFixedJointInOther();
FixedJoint fixedJoint = GetComponent<FixedJoint>();
Destroy(fixedJoint);
yield return null;
}
}
void destroyThisFixedJointInOther()
{
foreach (Rigidbody g in _RigidFixed)
{
WallSystem wallSystem = g.transform.gameObject.GetComponent<WallSystem>();
foreach (FixedJoint fixedJ in wallSystem.fixeds)
{
if (fixedJ != null && fixedJ.connectedBody.name == this.name)
{
Destroy(fixedJ);
}
}
}
}
#endregion
//En teoria si no se conecta con la pared deberia caer
//In theory if you don't connect to the wall you should fall
private void Update()
{
foreach (FixedJoint fixedJ in fixeds)
{
if (fixedJ == null)
Destroy(fixedJ);
}
if (GetComponent<FixedJoint>() == null)
{
_rigid.constraints = RigidbodyConstraints.None;
_rigid.useGravity = true;
}
}
}
I'd like it to be like this wall that when it doesn't connect to the wall the block falls