Make gameObject observe and compare value for multiple gameObjects around it?

Let's say I have a board peg that never move and the disc has fallen from the top, Plinko style. When the disc falls and hits the pegs, they generate a random value between 1 and 6, which is displayed on the peg. Now I want every binding that has a direct neighbor with the corresponding value to turn purple, like this:

enter image description here

I have attached a game object to each binding called Neighbor Checker and attached circular colliders as triggers that overlap with their neighbors, for example:

enter image description here

Game Neighbor Checker objects attached to pegs are set with a circular collider set as the trigger and the 2d rigid body is set to kinematic, discrete collision detection and never sleep (this continuous check seems to make the value check more accurate but kills performance , and the disc falls and bounces off all intermittent).

The main problem I ran into was accuracy: sometimes 3 or 4 bindings are all neighbors with the same value, and one will still be green.

I am using OnTriggerStay2D (adjacent Collider2D) in an adjacent validation script to keep track of the values ​​of the pegs and their neighbors and change their colors when the values ​​match:

 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 for improving accuracy and efficiency here? Do I really disagree about this? I saw this in the Unity docs: Physics2D.OverlapCircleAll and wondering if I should first use this to create a list of neighbors for each binding, then in an update loop through them and check their values, but that can get expensive too, so I'm not sure ...

This is my first 2D unity project. I was fine with text based material, but physics is quite hard to understand, although I read the documentation inside and out! Any help and suggestions would be greatly appreciated.

Thanks in advance!

+3


source to share


1 answer


If you want to maximize performance, you should only update the bindings if they change. You can store all the bindings in an array (or if you want to add additional bindings while the list is running) and then call a function on each neighbor that will tell him to check his environment, this will reduce the number of times you check neighbors thoroughly, resulting in improving the code.

If you used Physics2D.OverlapCircle in a custom function, you could check if other colliders overlap it. Therefore, once the ball has registered a collision, you loop through the array with a for loop and call the update function on each ball. It should be noted that you will also need to do this cycle early in the game if you want the colors to update before the ball hits the circle.



If you want to take it one step further, you can call the update function on the one that the ball hit, and then inside that function, update all colliders returned [Physics2D.OverlapCircle][1]

, if you had bindings that were outside the range of the group the ball hit, you would have retained some performance there.

+1


source







All Articles