- Home /
Comparing the value of a peg to its neighboring pegs' values
Hi all,
Let's say I have a board of pegs, and a disc is dropped from the top, Plinko style. As the disc falls and hits pegs, they generate a random value from 1-6, which is displayed on the peg. Now I want each peg that has a direct neighbor with a matching value to turn purple, like so:
I've attached game objects to each peg called Neighbor Checker, and attached circular colliders as triggers that overlap with their neighbors, like so:
The Neighbor Checker game objects attached to the pegs are setup with a circular collider set as a trigger, and a rigid body 2d set to kinematic, discrete collision detection, and never sleep (this continuous checking seems to make the value checking more accurate, but kills performance and the disc falls and bounces all choppy).
The problems that I'm running into are 1) performance is crippled when the neighbor checkers are constantly checking their neighbors' text mesh values, 2) accuracy: sometimes 3 or 4 pegs are all neighbors with the same value and one will still be green.
I'm using OnTriggerStay2D (Collider2D neighbor) in the neighbor checker script to monitor the values of the pegs and their neighbors and changing their colors when the values match, like so:
void OnTriggerStay2D (Collider2D neighbor) {
if (neighbor.gameObject.tag == "NeighborChecker") {
textMesh = transform.parent.gameObject.GetComponentInChildren<TextMesh>();
neighborTextMesh = neighbor.transform.parent.gameObject.GetComponentInChildren<TextMesh>();
if (textMesh.text == neighborTextMesh.text) { // Make it purple
transform.parent.GetComponent<SpriteRenderer>().color = new Color(.5f,0f, 0.5f);
} else { // Change it back to the original green starting color
transform.parent.GetComponent<SpriteRenderer>().color = startingColor;
}
}
}
Any suggestions on how to increase the accuracy and efficiency here? Am I going about this totally wrong? This is my first 2D unity project, I was ok with the text based stuff, but the physics is kind of hard to grok even though I've read the documentation inside and out!
Thanks in advance!