unity3d - Colliding objects bouncing off when isTrigger is true instead of passing through -


i have looped through game objects set istrigger property before collision occurs though property true collision object still occurs. please see relevant code below:

    void oncollisionenter2d(collision2d col)    {      if (col.gameobject.tag == "object1") {          (int = 0; < badguys.count; i++)    {          badguys[i].getbadguygameobject().getcomponent<collider2d>().istrigger = true;       }        }      else if (col.gameobject.tag == "object2") {         // collision object1 occurs before object2, though istrigger true colliding object doesn't pass through badguys game objects, bounces off on collision       }    else if (col.gameobject.tag == "object3") {       (int = 0; < badguys.count; i++)   {          badguys[i].getbadguygameobject().getcomponent<collider2d>().istrigger = false;       }       }     else if (col.gameobject.tag == "badguy") {        }  } 

collision object1 occurs before object2, though istrigger true colliding object doesn't pass through badguys game objects, bounces off on collision. how can solve this?

update 1

i realized istrigger value true or false in inspector depending on time stop game. example if stop game after object1 collision istrigger true , when stop game after collision object3 false. inconsistency makes debugging cumbersome. bug or something?

update 2

based on recommendation joe blow , agustin0987 used enabled property of collider2d instead of istrigger. removed virtually code in oncollisionenter2d simple test scenario. please see code below:

void oncollisionenter2d(collision2d col)    {      if (col.gameobject.tag == "object1") {          if (badguys[4].getbadguygameobject().getcomponent<collider2d>().enabled = false) {             badguys[4].getbadguygameobject().getcomponent<collider2d>().enabled = true;             debug.log ("changed true");             debug.log ("bad guy  collider enabled " + badguys[4].getbadguygameobject().getcomponent<collider2d>().enabled);          }           else if (badguys[4].getbadguygameobject().getcomponent<collider2d>().enabled = true) {             badguys[4].getbadguygameobject().getcomponent<collider2d>().enabled = false;             debug.log ("changed false");             debug.log ("bad guy collider enabled " + badguys[4].getbadguygameobject().getcomponent<collider2d>().enabled);           }     }     else if (col.gameobject.tag == "badguy") {        debug.log("collided bad guy"); // doesn't occur     }  } 

instead of looping through time, decided test one. else if enabled true satisfied though log prints false , if enabled false never satisfied. in inspector, box collider2d bad guys unchecked. collision badguy never occurs. based on simple scenario code thought should working.

never, ever ever

i mean absolutely never

use "else if".

simply, never, ever - ever - type "else if" rest of life, until die.

in first instance, delete code , replace this:

void oncollisionenter2d(collision2d col) { debug.log("we deal ......... " +col.gameobject.name); if (col.gameobject.tag == "object1")     {     debug.log("\t handling 'object' type issue");     handleobjectissue(col)     return; //note here     } if (col.gameobject.tag == "object1")     {     debug.log("\t handling 'bad guy' type issue");     handlebadguyissue(col)     return; //note here     } }  private void handleobjectissue(collision2d col) { debug.log("'\t\t object', i'm dealing .. " +col.gameobject.name); }   private void handlebadguyissue(collision2d col) { debug.log("\t\t 'badguy', i'm dealing .. " +col.gameobject.name); } 

run extensively , "unit test" it. if like, convert (using code) "loop" method @ items. in event, test extensively.

--

secondly, add routine ..

private void togglecolliderongameobject( gameobject gg ) { debug.log("i'm supposed toggle collider on: " +gg.name); collider2d { or whatever } col = gg.getcomponent<collider2d>(); bool currentstate = getcomponent<collider2d>().enabled;  debug.log("\t currently: " +currentstate);  bool newstate = ! currentstate; col.enabled = newstate;  debug.log("\t\t is: " +newstate); } 

... , start unit testing it. have code like:

 togglecolliderongameobject( badguys[4].getbadguygameobject() ); 

for example, try things this:

void start()     {     invokerepeating("teste", 1f, 1f);     } private void teste()     {     togglecolliderongameobject( badguys[4].getbadguygameobject() );     } 

after experimenting time , unit testing, able move on.

note apart other problems, (for example) routine "getbadguygameobject" returning wrong thing - or similar problem.

edit:

remove badguy = badguyprefab; public void createbadguy(gameobject badguyprefab, badguytype badguytype, vector3 badguyposition) function. mistake in programmer's code link in comment.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -