Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help for an algorithm that use Physics2D.OverlapAreaAll

Discussion in 'Scripting' started by BDCJR, May 29, 2014.

  1. BDCJR

    BDCJR

    Joined:
    Apr 25, 2013
    Posts:
    19
    Hello good people. I encounter a problem using OverlapArea and i don't know how can I fix it. The next image explains what i'm trying to do, and below is my code. THE PROBLEM is that sometimes for one or more "photons" the code part for OUT is not executed, is very rarely
    but it happens. Maybe someone can give me a hand of help or how can i make this work fine.

    $Collider problem.jpg

    Code (csharp):
    1.  
    2.         private Cell parentRef;
    3.         private int photonMask = (1 << 9);
    4.         private List<GameObject> arrPhotons;
    5.         private float maxDistance;
    6.         public void Init()
    7.         {
    8.             parentRef = transform.GetComponent<Cell>();
    9.             arrPhotons = new List<GameObject>();
    10.             maxDistance = parentRef.Size/DataStorage.PPU/2;
    11.         }
    12.    
    13.         private void Update()
    14.         {
    15.             if(parentRef.Rank == Cell.EnumRank.Mother)
    16.                 return;
    17.    
    18.             //*** get the photons that are in the overlap area
    19.             Vector2 pA = new Vector2(transform.position.x - parentRef.Size/DataStorage.PPU/3, transform.position.y - parentRef.Size/DataStorage.PPU/3);
    20.             Vector2 pB = new Vector2(transform.position.x + parentRef.Size/DataStorage.PPU/3, transform.position.y + parentRef.Size/DataStorage.PPU/3);
    21.             Collider2D[] photons = Physics2D.OverlapAreaAll(pA, pB, photonMask);
    22.             if(photons.Length > 0)
    23.             {
    24.                 Photon photon = photons[0].GetComponent<Photon>();
    25.             }
    26.  
    27.             //*** OUT
    28.             for(int i=0; i < arrPhotons.Count; i++)
    29.             {
    30.                 if((arrPhotons[i].transform.position - transform.position).magnitude >= maxDistance  arrPhotons[i].layer == 8)
    31.                 {
    32.                     //Debug.Log("out: " + arrPhotons[i].name);
    33.                     arrPhotons[i].layer = 9;
    34.                     arrPhotons.Remove(arrPhotons[i]);
    35.                 }
    36.             }
    37.  
    38.             //*** IN
    39.             if(photons.Length > 0)
    40.             {
    41.                 Photon photon = photons[0].GetComponent<Photon>();
    42.                 if((photon.transform.position - transform.position).magnitude < 0.02f)
    43.                 {
    44.                     photon.gameObject.layer = 8;
    45.                     arrPhotons.Add(photon.gameObject);
    46.                     //Debug.Log("in: " + photon.name);
    47.                 }
    48.             }
    49.  
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    First question I may ask is are you sure your Photons are not traveling at a rate of speed which could bypass the collider in question?

    Basically as far as I can see, you are trying to capture a photon that has entered a field. Usually, you want to do a raycast or spherecast or whatever to see if the photon is going to hit an object before moving it, if it does, then you can do something there.
     
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Is it in Unity 4.5? as a slew of Physics bugs were fixed in that version.
     
  4. BDCJR

    BDCJR

    Joined:
    Apr 25, 2013
    Posts:
    19
    Yes, is unity 4.5 version.
     
  5. BDCJR

    BDCJR

    Joined:
    Apr 25, 2013
    Posts:
    19
    Thanks, I will try this two methods, but one question before, this methods needs rigidbody?