2D Toolkit Forum

2D Toolkit => Support => Topic started by: drkucho on January 14, 2014, 07:39:30 pm

Title: issues with colliders being set on sprite collection loosing trigger exit events
Post by: drkucho on January 14, 2014, 07:39:30 pm
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?
Title: Re: issues with colliders being set on sprite collection loosing trigger exit events
Post by: mheydlauf on January 14, 2014, 08:48:54 pm
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.
Title: Re: issues with colliders being set on sprite collection loosing trigger exit events
Post by: drkucho on January 14, 2014, 08:57:40 pm
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
Title: Re: issues with colliders being set on sprite collection loosing trigger exit events
Post by: mheydlauf on January 14, 2014, 09:09:37 pm
Hmm, it looks like the collider events thing is a bug in Unity.
http://forum.unity3d.com/threads/222608-OnTriggerEnter2D-OnTriggerExit2D-Unity-Bug-Workaround (http://forum.unity3d.com/threads/222608-OnTriggerEnter2D-OnTriggerExit2D-Unity-Bug-Workaround)
Title: Re: issues with colliders being set on sprite collection loosing trigger exit events
Post by: drkucho on January 14, 2014, 09:47:49 pm
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
Title: Re: issues with colliders being set on sprite collection loosing trigger exit events
Post by: unikronsoftware on January 15, 2014, 11:12:54 am
This sounds very much like that Unity bug you've linked to. I dont think there is any workaround for this.
Title: Re: issues with colliders being set on sprite collection loosing trigger exit events
Post by: drkucho on January 15, 2014, 02:58:24 pm
so this won't happen if i use 3d colliders?
Title: Re: issues with colliders being set on sprite collection loosing trigger exit events
Post by: unikronsoftware on January 15, 2014, 04:29:28 pm
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.