Hello Guest

Author Topic: Collider size is set to 0 when image is changed on sprite  (Read 11980 times)

Artifact

  • Newbie
  • *
  • Posts: 9
    • View Profile
Collider size is set to 0 when image is changed on sprite
« on: February 17, 2012, 12:36:22 am »
Hello, I have a sprite and I ticked the create collider. It creates a box collider on my sprite.
I set this collider to the size I want it, but whenever I run my game and the sprite is animating, it sets the size of my sprite to 0,0,0.

Can anyone help me solve this issue.

Thx in advance :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collider size is set to 0 when image is changed on sprite
« Reply #1 on: February 17, 2012, 07:05:53 am »
Hi,

This is probably because you haven't set up a collider type on all frames - the sprite sets itself to zero when the current frame has no collider set, mainly to avoid destroying and recreating the collider per frame. You need to set up colliders on every frame, as you can have some frames which don't have a collider.

Cheers,
unikron

Artifact

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Collider size is set to 0 when image is changed on sprite
« Reply #2 on: February 17, 2012, 02:35:40 pm »
that solved the issue, thanks a lot :)

nikolic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: Collider size is set to 0 when image is changed on sprite
« Reply #3 on: February 21, 2012, 04:45:10 pm »
Is it possible that, while animating, animated sprite instance changes its mesh collider by applying polygon colliders previously set to each frame sprite?

I looked into the source code, it seems like this is not supported. Would it introduce some issues if I included the support for it?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collider size is set to 0 when image is changed on sprite
« Reply #4 on: February 21, 2012, 06:02:20 pm »
Yes, there are issues.

Last time I checked, it was not possible to change polygon collider mesh on the fly - it needed to be recreated every time. This meant that there would be large numbers of allocations every frame of animation.

db82

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Collider size is set to 0 when image is changed on sprite
« Reply #5 on: March 09, 2012, 12:42:56 pm »
Is it possible to set a collider for all frames of a sprite collection in one hit? Rather than individually selecting each frame one by one?

I have a lot of frames!

Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collider size is set to 0 when image is changed on sprite
« Reply #6 on: March 09, 2012, 02:38:38 pm »
There isn't a way to multi-select sprites and edit them yet. However you can set the default collider setting before importing them - so if rebuilding the collection is an option, that might be your best bet. The setting is under the "Defaults" foldout in the inspector.

If you have already set these up and can't resize them, a simple script will sort these out for you. Just drop this in /Assets/Editor/tk2dBatchChange.cs

Code: [Select]
using UnityEngine;
using UnityEditor;

public class tk2dTmpBatchChanger
{
[MenuItem("2D Toolkit/Unsupported/Batch Change Collider")]
public static void DoChange()
{
if (Selection.activeGameObject && Selection.activeGameObject.GetComponent<tk2dSpriteCollection>())
{
var spriteCollection = Selection.activeGameObject.GetComponent<tk2dSpriteCollection>();
foreach (var param in spriteCollection.textureParams)
{
param.colliderType = tk2dSpriteCollectionDefinition.ColliderType.BoxTrimmed;
}
Debug.Log("Done");
}
}
}

Now all you will need to do is select the sprite collection, pick the menu option (2D Toolkit / Unsupported / Batch Change Collider) and click Commit.

db82

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Collider size is set to 0 when image is changed on sprite
« Reply #7 on: March 09, 2012, 04:14:39 pm »
Great thanks!

helioxfilm

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collider size is set to 0 when image is changed on sprite
« Reply #8 on: March 12, 2012, 05:28:22 pm »
Well, we just runt into this similar situation, when the collider gets 0,0,0... We will try this script addition, but still it would be great to have an option to apply a collider to all frames... box/poly/mech colliders, so we can use raycasts.