- Home /
Help with a collision problem.
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.
I would probably go with your original approach: deactivate the colliders until the mouse is clicked. This should be the simplest way really.. you could set the collider to a trigger or deactivate the collider entirely, the answer of $$anonymous$$ $$anonymous$$ehreron on this question might help - http://answers.unity3d.com/questions/10883/how-do-i-disable-collision-for-an-object.html
Aldo's trick of clever algorithmic trick of propogating fixes only one-way, will get you through something like this. Hope it helps!
Answer by aldonaletto · May 29, 2012 at 12:06 PM
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 }
Thanks for that, I am not a coder so will take me a good few hours to digest that info. But I really appreciate the work. Thanks
Answer by Artmann · May 29, 2012 at 11:06 AM
I would create an area that's reserved for new T-Shapes, so when an T-Shape is in the specified area it can't rotate.
if(!inStartArea()) Rotate();
When I remove the start area as any of the objects is clickable then I am back at the same issue :(
Your answer

Follow this Question
Related Questions
Detecting multiple gameobjects 1 Answer
Collisions occassionaly fail. 4 Answers
Creating a Barrier 1 Answer
Collider Shape 1 Answer
Rigid Body Collision 0 Answers