Hello Guest

Author Topic: Collider Unset - Safe to use?  (Read 3301 times)

KyleStaves

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Collider Unset - Safe to use?
« on: February 14, 2012, 08:28:32 pm »
Hello,

I have a couple of sprites that are significantly smaller than their trigger area - so I'm setting up the colliders myself the usual Unity way. However, at runtime they always set themselves to position 0,0,-100000 with 0,0,0 extends.

I looked through the source to find the area where this was happening and found it in sprite base.

Code: [Select]
else if (sprite.colliderType == tk2dSpriteDefinition.ColliderType.Unset)
{
// Don't do anything here, for backwards compatibility
}
else // in all cases, if the collider doesn't match is requested, null it out
{
if (boxCollider != null)
{
// move the box far far away, boxes with zero extents still collide
boxCollider.center = new Vector3(0, 0, -100000.0f);
boxCollider.extents = Vector3.zero;
}
}

I then set the collider type for the sprites in question to "Unset" and it fixed the issue. I'm just curious if this is the correct/safe way to handle this moving forward (as the code suggests it's legacy support and the conditional may be removed in the future) - or if there is a better way to handle this.

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collider Unset - Safe to use?
« Reply #1 on: February 14, 2012, 08:37:52 pm »
Yes that is the right thing to do. The documents aren't particularly clear on this, and I am improving the docs now - I will make sure it is clearer.

The idea with unset is, the sprite has no collider set up, so if you set anything up manually, it will just remain there. The backwards compatiblity comment is in regards to one version of 2D toolkit where if the user upgraded from that version, to make everything behave as it did in the previous version as opposed to the new default behaviour.

Collider type of None does that seemingly bizzare thing - the reasoning behind it is that when you are playing an animation, you could have a few frames with no collider - you can't really reliably disable the collider apart from deleting it (which will cause memory allocations when added again), so it simply moves it far far away and makes it tiny. That way it doesn't need to delete and recreate colliders frequently. One of the things we used it for was for a mario "piranha plant" style monster, where it only triggers the player collider when its fully extended out of the pipe.

Hope that helps.

KyleStaves

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Collider Unset - Safe to use?
« Reply #2 on: February 15, 2012, 12:17:33 am »
Okay excellent; thanks for the quick response! Just wanted to make sure I wasn't going to shoot myself in the foot by using Unset.