I have T shaped objects that can collide with each other arranged in a 10x10 grid. If the player clicks one it rotates and if it collides then the object it collides with rotates and a chain reaction happens.
My conundrum is this:
My objects are Instantiated in a random direction so some are colliding as soon as they are in the game scene. This is starting the reaction immediately.
Can anyone think of a way to have the objects ignore collisions when they are Instantiated but will be affected after the mouse has been clicked on one of the objects collides.
I have tried to deactivate the colliders until the mouse is clicked. That was the same result as above.
I cant make them ignore the first collision as not all objects will be colliding on start up.
Any suggestions any one has will be appreciated.
Added Info
Here is a link to an image of the start up…the ones colliding should not start rotating until they have have been collided with another object after it has rotated, even though they are all ready colliding.
Since the random rotation seems to be always a multiple of 90, you could do the following:
1- Instantiate the object with Is Trigger set and at some random rotation;
2- If a OnTriggerEnter event occurs, rotate 90 degrees clockwise (for instance);
3- To avoid more than one atom rotating to runaway from the collision, name each atom with an increasing index, and only rotate the one with “greater” name;
4- When the user clicks one atom, set all atoms’ isTrigger to false;
Atoms creator script:
var size = 10; // grid size
function CreateAtoms(){
var n = 0; // name index
for (var x=0; x< size; x++){
for (var z=0; z< size; z++){
var pos: Vector3 = ... // calculate the position according to x and z
var rot: Quaternion = Quaternion.Euler(0, 90 * Random.Range(0, 4), 0);
var atom = Instantiate(atomPrefab, pos, rot);
atom.tag = "Atom"; // tag atoms as "Atom" to help collecting them
atom.name = "Atom" + n.ToString(); // give it an increasing number
n += 1;
atom.collider.isTrigger = true; // it's a trigger at first
}
}
}
Atom script:
function OnTriggerEnter(other: Collider){ // if some collision occurs...
if (name > other.name){ // only the one with higher index will rotate
transform.Rotate(0, 90, 0);
}
}
function OnMouseDown(){ // when any atom is clicked...
// get all of them in an array...
var atoms: GameObject[] = GameObject.FindGameObjectsWithTag("Atom");
for (var atom: GameObject in atoms){
atom.collider.isTrigger = false; // and enable collisions
}
// do what you want to do here when the atom is clicked
}