2D Toolkit Forum

2D Toolkit => Support => Topic started by: Artifact on February 17, 2012, 12:36:22 am

Title: Collider size is set to 0 when image is changed on sprite
Post by: Artifact 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 :)
Title: Re: Collider size is set to 0 when image is changed on sprite
Post by: unikronsoftware 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
Title: Re: Collider size is set to 0 when image is changed on sprite
Post by: Artifact on February 17, 2012, 02:35:40 pm
that solved the issue, thanks a lot :)
Title: Re: Collider size is set to 0 when image is changed on sprite
Post by: nikolic 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?
Title: Re: Collider size is set to 0 when image is changed on sprite
Post by: unikronsoftware 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.
Title: Re: Collider size is set to 0 when image is changed on sprite
Post by: db82 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
Title: Re: Collider size is set to 0 when image is changed on sprite
Post by: unikronsoftware 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.
Title: Re: Collider size is set to 0 when image is changed on sprite
Post by: db82 on March 09, 2012, 04:14:39 pm
Great thanks!
Title: Re: Collider size is set to 0 when image is changed on sprite
Post by: helioxfilm 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.