- Home /
Collider2D as 1 pixel size
Hi to Unity devs!
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 ?
Not familiar with 2D stuff, but AFAI$$anonymous$$ a pixel doesn't literally translate into Unity units. Besides, can't you resize any collider to be any size by adjusting its properties in the inspector? Google around for unity pixel perfect or similar terms - I'm sure others have written about this extensively.
thanks to all, the problem solved: set pixel per Unit to 20 for my 1x1 pixel image, then add boxCollider2D with size = 0.05 (it is $$anonymous$$imal values) and set camera size related to pixel per Unit to make pixel perfect view.. all works fine!
Answer by sysameca · Nov 19, 2014 at 11:10 AM
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.
Answer by sinoby · Nov 19, 2014 at 12:23 PM
thanks sysameca,
"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?
There will be other ways for sure to achieve the result you want. $$anonymous$$y suggestion is this: Post a new thread in Unity Forums/Scripting or Forums/2D about exactly what are you trying to achieve so people can help you out and do a discussion. In Unity answers you will mostly receive a straight answer. Also there is a "add new comment" button to reply to the given answer ins$$anonymous$$d giving a new one as reply :)