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
2
Question by jesta · Nov 29, 2011 at 02:35 AM · 2dcollisiontexture2dalphaplane

2D collisions on a Texture2D with transparent areas

I have a 2D texture assigned on a plane with some transparent areas : image Black is transparent and white is solid. The texture is procedurally generated.

I need the white areas to have colliders, so that rigidbodies could fall through black areas and land on white ones.

Is there any way to do this?

Comment
Add comment · Show 1
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 Fattie · Jun 14, 2012 at 06:15 PM 0
Share

SImply add a virtual camera to the object as it is descending, have an AI system decide if it is heading to white or black! :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by syclamoth · Nov 29, 2011 at 05:49 AM

You'll have to do collisions manually- there's no way of tweaking the physics engine to have this kind of functionality. However, if you are happy with having somewhat unrealistic collisions, it is possible to check the UV coordinates of the mesh against the mesh's texture, to check if it's black or white- you could use this with manual raycast collisions.

 public bool CheckBlackWhite(Ray castThis)
 {
     RaycastHit hit;
     if(Physics.Raycast(Ray, out hit))
     {
         // Just in case, also make sure the collider also has a renderer
         // material and texture. Also we should ignore primitive colliders.
         Renderer hitRender = hit.collider.renderer;
         MeshCollider meshCollider = hit.collider as MeshCollider;
         if (hitRender == null || hitRender.sharedMaterial == null ||
         hitRender.sharedMaterial.mainTexture == null || meshCollider == null)
             return false;
         Texture2D hitTex = hitRender.material.mainTexture;
         Vector2 pixelUV = hit.textureCoord;
         pixelUV = new Vector2(pixelUV.x * tex.width, pixelUV.y * tex.height);

         // this is the important bit!
         // Make sure that the texture has 'isReadable' set to true, or this won't work.
         Color pixelValue = hitTex.GetPixel(pixelUV.x, pixelUV.y);

         // returns true if the colour hit is light, false if it's dark
         return (pixelValue.grayscale > 0.5f);
     }
 }


Otherwise, you can precache your colliders if it's procedurally generated by creating a mesh that matches the black and white sections and using a mesh collider, but that's way more difficult. I hope this is enough for you! I think you might be able to use certain other Physics functions in a similar way to get more accurate results, but of course that would be slower.

Comment
Add comment · Show 2 · 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 jesta · Nov 29, 2011 at 06:55 AM 0
Share

I followed your second suggestion and it works perfectly. Thanks!

avatar image syclamoth · Nov 29, 2011 at 07:04 AM 0
Share

Wow, and there I was thinking the second suggestion was the more complicated one!

avatar image
0

Answer by _md_ · Jul 03, 2013 at 03:09 PM

Hello,

I am sorry for asking a question on this old topic (and for my English level), but the title of the topic is perfectly adapted to my question.

syclamoth suggests two ways to check the collisions on a texture with transparent areas: - check if the pixel is transparent - create a mesh adapted to the shape on the texture The both works.

My question is : what is the best solution concerning the memory ?

I read that set a texture "readable" doubles the memory needed, because a copy of the data is done. But if I do a mesh corresponding to a complicated 2D shape, I need a lot of polygones in my world.

Considering I have a lot of objects and my game has to work on mobile devices, is someone knows what is the best solution ?

Thank you for your help

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple collision areas within plane 0 Answers

I want to collisionbetween planes 1 Answer

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

Cutting hole from player character to camera through multiple sprites with edge blending 0 Answers

Ignore collision at high velocity. 1 Answer


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