Working with pixel art and need to operate with single pixels and thin lines in 1px, so if I have such small objects the standard boxCollider2D / circleCollider2D cannot be assigned and appears too big, like 5x5 size!
so, how it possible to implement collider as 1 pixel ?
For a BoxCollider2D something like this will work:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider2D))]
public class PixelScale : MonoBehaviour {
public int pixelSizeX; // The x size of the BoxCollider2D in pixel units
public int pixelSizeY; // The y size of the BoxCollider2D in pixel units
BoxCollider2D m_boxCollider2D = null;
// Use this for initialization
void Start () {
m_boxCollider2D = gameObject.GetComponent<BoxCollider2D>();
// Convert pixels to world units
float pixelsToWorld =((Camera.main.orthographicSize) / (Screen.height / 2.0f));
// Set the size
m_boxCollider2D.size = new Vector2(pixelsToWorld * pixelSizeX, pixelsToWorld * pixelSizeY);
}
}
However if you set a very small pixel size the collider probably won’t be build because the vertices will be very close together.
Then by the same principle, for CircleCollider2D you scale the radius of the collider:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CircleCollider2D))]
public class PixelScale : MonoBehaviour
{
public int pixelRadius; // The radius in pixel size
CircleCollider2D m_circleCollider2D = null;
// Use this for initialization
void Start()
{
m_circleCollider2D = gameObject.GetComponent<CircleCollider2D>();
// Convert pixels to world units
float pixelsToWorld = ((Camera.main.orthographicSize) / (Screen.height / 2.0f));
// Set the size
m_circleCollider2D.radius = pixelsToWorld * pixelRadius;
}
}
If you need very small box type collider maybe you can add an edge collider and set the XY Scale of the game object in pixels and solve your problem like that.
“However if you set a very small pixel size the collider probably won’t be build because the vertices will be very close together.” , I have 1x1 pixel image, after import to unity it sets pixels per unit = 100, should i change this setting manually? I think no, because it displays correctly in fullscreen mode (2560x1440) as 1 pixel on screen. So in this case any collider becomes to small as you write on answer.
trying edge collider and there are some glitches…
so the best practice with Unity it is not using such small objects?