Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sinoby · Nov 19, 2014 at 10:20 AM · collider2d

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 ?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Nov 19, 2014 at 12:03 AM 0
Share

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.

avatar image sinoby · Nov 19, 2014 at 01:31 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sysameca · Nov 19, 2014 at 12:56 PM 0
Share

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 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

void OnTriggerEnter2D(Collider2D other) ..... What is is "other" called as ??? 2 Answers

Why can't I drag text into public text in the inspector when instantiated? 1 Answer

Player jump only once 1 Answer

Adding Collider2D changed physics on race car.How to compensate for mass loss in the back of the car?or do it some other way. 0 Answers

Tile collision by sprite is not accurate with Tilemap Collider 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges