- Home /
What is the best method to control the movement of a group of objects based on the collision trigger of any individual?
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";
}
}
}
Answer by highpockets · Aug 11, 2013 at 07:36 AM
Give all your cubes a "Cube" tag and when one hits the floor, do something like this:
public GameObject [] cubes;
void OnTriggerEnter(Collider collider) {
if (collider.gameobject.tag == "Floor")
{
cubes = GameObject.FindGameObjectsWithTag("Cubes");
foreach (GameObject cube in cubes)
{
cube.tag = "Floor";
}
}
}
And above where you translate when falling is true, you could do something like this:
if (gameObject.tag != "Floor") {
myTransform.Translate(Vector3.down * cubeSpeed * Time.deltaTime);
}
In this case you won't need your falling boolean variable.
That sounds like a promising approach, though I think it will mean that if more than one group is falling at a time then all of them will freeze. I could cycle through a set of tags (cube1, cube2, etc) up to a limit of however many groups will be falling at once. Do you think that would be the best approach?
Personally that is what I would do. I'm sure there is another way to do it, but this is the first thing that popped into my $$anonymous$$d. If you read what I put right away, take another glance because I had a couple errors that I had to edit.
Another thought. You could also have an empty game object that parents each group during the fall.
I was initially trying to do that, but I couldn't work out how to get them to communicate. I would be interested to see how you would implement a parent object. I'm new to program$$anonymous$$g and still getting my head about that aspect of things.