Hello Guest

Author Topic: issues with colliders being set on sprite collection loosing trigger exit events  (Read 7520 times)

drkucho

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 72
  • Retro Arcade Freak
    • View Profile
    • Dr. Kucho!
Hello

i have my player sprite collection in which i have defined custom box colliders (2D) for each frame

then i have my enemies with a circle collider called Vision to spot the player, this vision collider is monitored by a simple script with a OnTriggerEnter() and OnTriggerExit() functions

what happens is , instead of getting a single ENTER/EXIT event when the player enters/exit the Vision collider , i get a ENTER event every time the sprite frame of its animation is updated, and the worse is many EXIT events are lost, not happening, its about 50% of chance to loose them

this was not happening before when my player had a simple box collider2D component and all colliders of the sprite frames were set to "user defined"

am i doing something wrong? is this normal ? if so … any work around?

mheydlauf

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile
Hi drkucho,

I'm guessing you're just using the Vision collider to determine when the player is within certain proximity to the player, correct?  If so, you probably don't need to use a collider at all, just check the distance between the player and the enemy each frame.  Something like:

Code: [Select]
bool playerIsInKillZone = false;
void FixedUpdate()
{
      if (Vector2.Distance(player.transform, enemy.transform) < killZone)
      {
          if (playerIsInKillZone == false)
         {
             playerIsInKillZone = true;
             OnPlayerEnterKillZone();
         }
      }
      else if (playerIsInKillZone)
      {
         playerIsInKillZone = false;
         OnPlayerExitKillZone();
      }
}

Hope that helps.

Cheers,
Mike.

drkucho

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 72
  • Retro Arcade Freak
    • View Profile
    • Dr. Kucho!
it does help  :) , but colliders are so convenient to graphically set the vision of each enemy in the scene, plus sometimes, the vision collider is better to be a box , not a circle, some enemies are guys on a window that i want them to see not in a circle around them but on a rectangle under them, so the rectangle collider is not only a rectangle but its also shifted down

mheydlauf

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile

drkucho

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 72
  • Retro Arcade Freak
    • View Profile
    • Dr. Kucho!
edited, the last post i did seems to be related with a  unity bug which shows this every time i hit play:

Uncaught exception in async net callback: Object reference not set to an instance of an object
UnityEditor.AsyncHTTPClient:Done(State, Int32)

i fixed it going back to a previous scene file , now i still get the "normal" many enter events and loosing 50% exit events  … hope unikron can add some light on this … if it is not really a unity bug
« Last Edit: January 14, 2014, 10:07:35 pm by drkucho »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
This sounds very much like that Unity bug you've linked to. I dont think there is any workaround for this.

drkucho

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 72
  • Retro Arcade Freak
    • View Profile
    • Dr. Kucho!
so this won't happen if i use 3d colliders?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
It shouldn't as far as I know, but remember you can't have 2 non-convex 3d colliders colliding...
Its worth a try though, as its pretty easy to switch.