c# - Understanding and using Unity's Physics2D.OverlapArea() -


i'm trying write script instantiates square prefab @ random location on screen. if space occupied instantiated square, should write in console. here's code i've written:

    public void spawnsquares(int difficulty){     float r1=random.range(25.0f,_screenwidth);     float r2=random.range(25.0f,_screenheight);      _overlapa=new vector2(r1-50f,r2+50f);     _overlapb=new vector2(r1+50f,r2-50f);      if(physics2d.overlaparea(_overlapa,_overlapb,layermask,mathf.infinity,mathf.infinity)==null){         _position=camera.main.getcomponent<camera>().screentoworldpoint(new vector3(r1,r2,10));         instantiate(resources.load("square",typeof (gameobject)),_position,quaternion.identity);      }     else{         debug.log("avoided collision");     } } 

the layermask variable set layermask=1<<9; because squares located on layer 9.

what expected generate random location , modify _overlapa , _overlapb account size of squares, check if there colliders in area. if there not (overlaparea()==null) should spawn new square @ position. if space occupied write console.

what happening? execution never branches off on else, means physics2d.overlaparea(_overlapa,_overlapb,layermask,mathf.infinity,mathf.infinity) returns null. however, after calling spawnsquares() around 10 times screen pretty filled squares should @ least detect 1 collider.

what doing wrong? project has stalled last 2 days because can't seem solve issue, appreciated.

thanks time!

you have logic error, you're on right path. missing main concept of checking.

3 main problems.

  1. you're checking pre-instantiated physics2d no matter happens, return null;

    if(physics2d.overlaparea(_overlapa,_overlapb,layermask,mathf.infinity,mathf.infinity)==null){ _position=camera.main.getcomponent<camera>().screentoworldpoint(new vector3(r1,r2,10)); instantiate(resources.load("square",typeof (gameobject)),_position,quaternion.identity);  } else{     debug.log("avoided collision"); } 

the logic here says, hey check physics2d. if overlapping "myown _overlapa , _overlapb. instantiated, return true. not yet instantiated, meaning give false.

to fix problem, need tell compare "other physics2d" exists in game.

example

if(physics2d.overlaparea(otherphysics2d.overlapa,otherphysics2d.overlapb 
  1. now understanding first solution, make think second problem. how can reference have instantiated? how can physics2d?

this part need make list

   // needs out of method accessible all.      public list<gameobject> myspawns = new list<gameobject>(); 

now after instantiated them, add them list this.

if(physics2d.overlaparea(_overlapa,_overlapb,layermask,mathf.infinity,mathf.infinity)==null){     _position=camera.main.getcomponent<camera>().screentoworldpoint(new vector3(r1,r2,10)); gameobject newgameobject = instantiate(resources.load("square",typeof (gameobject)),_position,quaternion.identity);     myspawns.add(newgameobject);     }     else{         debug.log("avoided collision");     }     // take note if miss cast reason, add "as gameobject" @ end downcast it. 
  1. checking part how check every list. easy use foreach.

     foreach(gameobject check in myspawns)      {         vector2 myx = check.collider2d.transform.x;         vector2 myy = check.collider2d.transform.y;                       if(physics2d.overlaparea(myx,myy,layermask,mathf.infinity,mathf.infinity)==null){ _position=camera.main.getcomponent<camera>().screentoworldpoint(new vector3(r1,r2,10));       gameobject newgameobject = instantiate(resources.load("square",typeof (gameobject)),_position,quaternion.identity); myspawns.add(newgameobject); }   else   {        debug.log("avoided collision");    }  } 
  2. lastly can on own. add physics2d component on prefab , makesure there in there , make sure there no physics on cause 3d.

welcome stockoverflow


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -