I’m not sure what the best approach to this is, but I have searched around a lot for some solution but nothing I try seems to work.
I want to have a group of four cubes fall down (tetris style), and when one of them hits the ground, or a previously grounded cube, for all members of the group to stop falling and snap to a grid. The reason I don’t want to set the arrangement to be a single object is that they are being shot at and destroyed on their way down.
At the moment I have a empty prefab “shape” with the four “cubes” in it. The shape script just sets the initial position of the group and nothing else. The cube script is below. I’ve tried a lot to set some sort of groupFalling variable in the shape, but I cannot for the life of me work out how to get a cube to access and change it. Nor am I sure that is the best approach.
My current cube script looks like this (just in case it helps):
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
private Transform myTransform;
private float cubeSpeed = 5;
public bool falling;
// Use this for initialization
void Start () {
myTransform = transform;
falling = true;
}
// Update is called once per frame
void Update () {
if (falling) {
myTransform.Translate(Vector3.down * cubeSpeed * Time.deltaTime);
}
}
void OnTriggerEnter(Collider collider) {
if (collider.gameObject.CompareTag("Bullet") && falling) {
DestroyObject(this.gameObject);
DestroyObject(collider.gameObject);
}
if (collider.gameObject.CompareTag("Floor")) {
falling = false;
transform.gameObject.tag = "Floor";
}
}
}