I have a group of blocks attached to a holder. When the connection to the holder is lost, the block falls down. Sometimes the whole group moves down a step, when the player f’s up. Similar to some bubble shooters and other puzzle games.
My problem is differentiating the status of the blocks as either “falling” freely, or being a part of the group when it falls down one step. To accomplish this I made a script, and tried to to use a boolean that activates during the controlled descend to make the blocks know if they are falling or not.
This does not seem to be working, but at this point I feel like, the problem might just be in the order of operations and I’m too lost in this to figure it out. Or my entire approach might be faulty.
public class DistanceChecker : MonoBehaviour
{
GameObject block; //block object
GameObject hold; //object that block objects are hanging from
GameObject stones; //parent object of blocks and hold
public Vector2 blockVector;
public Vector2 holdVector;
float startDistance;
float currentDistance;
public BrickFall fallScript;
public Descend descendScript;
private void Start()
{
fallScript = GetComponent<BrickFall>();
stones = GameObject.Find("Stones");
descendScript = stones.GetComponent<Descend>();
block= gameObject;
hold = GameObject.Find("Hold");
DistanceCalc();
}
private void FixedUpdate()
{
blockVector = block.transform.position;
currentDistance = Vector2.Distance(blockVector, holdVector);
if (currentDistance > startDistance+1f)
{
fallScript.falling = true; //if the distance between the blocks and the hold increases one unit, they are falling
}else
{
fallScript.falling = false;
}
}
private void Update()
{
if (descendScript.check == true) //when the whole parent descends one step, run calculations
{
DistanceCalc();
}
}
public void DistanceCalc()
{
blockVector = block.transform.localPosition; //position of the block
holdVector = hold.transform.localPosition; //position of the hanging object
startDistance = Vector2.Distance(blockVector, holdVector); //get the distance between them at start
currentDistance = Vector2.Distance(blockVector, holdVector); //set the current distance of the blocks
Invoke(nameof(DescendEnd), 1f); //wait a sec to make sure all the blocks catch on
}
public void DescendEnd()
{
descendScript.check = false; //set the status off the blocks parent to have completed the descending step
}
}