Can a Polygon Collider 2D work with a Mesh Collider in Unity? - unity3d

EDIT: This is now a rather simple question. I have a 2D sprite that really needs the precision of a polygonal hitbox. The 2D, tile-based world around it uses a tile Mesh for efficiency reasons, and thus has a Mesh Collider.
Before, the tiles in the world were each GameObjects with Box Colliders and Rigidbody 2D's, and the ship and the tiles collided just fine. Now that I am using a Mesh Collider, however, they cannot collide. (I have read that this is because one is 2D and one is 3D.) So what should I do to get collisions (preferably with rigidbody physics) between a polygonal ship and a 2D tile mesh? [end edit]
In a 2D, tile-based, procedurally-generated, chunk-based exploration game (in Unity 4.5), I have a player ship which uses a Rigidbody 2D and a Polygon Collider 2D for collision detection.
This worked fine back when I used a Rigidbody 2D / Box Collider 2D for world tiles. However, this is horribly slow, so I replaced the discrete blocks with a tile mesh, using a Mesh Collider and other associated paraphernalia.
The problem is: I simply cannot get collision detection to work. I have tiles on the x-y plane, and the collision mesh (I can see it in the Scene View, so I know it works) consists of four rectangles perpendicular to the tile. (If you can't visualize this, I don't blame you. See here.)
What have I looked at so far? Well, I verified that the (2D) ship actually passes through the collision boxes in the Scene View. Also, neither of the colliders "Is Trigger".Since there seems to be no official documentation on how to actually use meshes (is there? Where?), I can't find out whether Mesh Colliders and Polygon Colliders actually can interact. Because one is 2D and one is 3D, does this not work? If so, then what should I do instead? I tried using a Box Collider [3D] for the ship, but this didn't work either. I could have potentially made a mistake here, though.
Am I supposed to handle the collision manually (with the OnCollisionEntered [or something] method)? Before, the rigidbody2D objects handled everything automatically. Otherwise, is there any other possible reason the collision might not work?

Well, I'm quite disappointed there seems to be no built-in way to do this in Unity. My solution was to attach a GameObject to the player that would read the block data from the world and create (pooled, of course) real but invisible "collider blocks" with Box Colliders 2D in a small area around the player, such that the player could collide with blocks near them. It works great, and I also implemented an algorithm to spawn rectangular collider blocks over groups of blocks; this eliminates the "ghost pixel" bug in the 2D physics engine.

Uni2D plugin (https://www.assetstore.unity3d.com/en/#!/content/3826) automatically creates 3d colliders (as a group of mesh colliders) from any 2 texture with transparency. A bit expensive but works.

Related

Unity fast way mesh collider to pipes

I am working on a project and this project has pipes. When ı add a mesh collider on pipes this is happening in photo
is there any fast way to add collider correctly?
You might need to checkout the convex checkbox:
That has its implications dealing with collitions, check the documentation.
At it says in the docs 'Enable the checkbox to make the Mesh Collider collide with other Mesh Colliders. Convex Mesh Colliders are limited to 255 triangles.' so it is not convex you might have problems to make it collide with other convex gameObjects, but can be OK for your determined case for example if you need for a raycast.
Edit:
I had a similar problem to yours working with unity lineRenderer but instead of a pipe was a longitudinal plane following a 3d path. I think that your chances are to try to make your mesh below 255 triangles and check if that works, or as you say, wrapp your types with primitive colliders (box or capsule). As it seems in your game the pipes are straight, so you can attach a capsule or box collider to each of the chunks of your pipe. If the pipe was curved and could move in the game as was my case, the problem gets out of control because you have to code the collider wrapping of your curved game object(s) and the collider behaviour as it moves.
I would check anyhow if with the convex checkbox unchecked it works. On the other hand you might find this helpfull

How to flip the normals of the collider mesh in Unity

I have a sphere in Unity and have used a script to flip the normals so I can see a 360 texture on the inside. I'm doing this because eventually I want to play a video on the inside, and also have other spheres inside hat can be thrown around and bounce off the inside walls of the outer sphere. The problem is, despite the normals of the sphere facing inwards, the collision is still standard. Objects within the sphere just fall straight through.
Do I need to add to my script to include the collision mesh?
you cannot have non-convex MeshColliders behave as Rigidbodies in Unity, you need to fake the walls as a series of convex colliders, approximate with box colliders maybe?

Collider for blender pot model inside Unity

i've made a simple pot model inside blender:
So the idea is that after importing to unity, generating mesh collider, adding ridgin body i would like to be able to put smothing inside the pot. Now it's generating collider but without a "hole" inside, so if i throw smth inside it just bounce off the top. Is there any way to do it simple way? I'd like to avoid making a collider by hand in unity, using cubes and so...
Image overview:
Your collider needs to be concave
It isn't clear from your question how you're "generating mesh collider," but the results are clearly generating a convex collider.
That said, some things to know about mesh colliders (and concave ones even more so): They are very compultationally heavy to calculate, so they should never...
move
scale (especially non-uniformly)
rotate
...at runtime.
Alternatively you can use multiple box colliders in the same orientations as the side-segments of your can (along with one or two for the bottom, depending on how small of an object you'll be dropping inside).
Uncheck the generate collider box from the import settings, add a mesh collider component and check convex.
I would use four or six box colliders for the walls of the pot and another for the floor. You can scale each box collider along each axis, but if you want to rotate a box collider you will need to give it a parent and rotate the parent. Box colliders are very low cost for the physics engine compared to mesh colliders.

Blender to Unity: Add Custom Collider to A Low Poly Style Terrain

I have created a low poly terrain object from Blender using a Plane, displaced and decimated it. I then exported it to Unity as an FBX object. As what I expected, my player simply falls through it since it has no colliders on. I could place a Box Collider on the object, but seeing it is a low poly style terrain with lots of bumps, it'll be pretty awkward to have my player walk straightly even if the ground is slightly raised. Even so, there are mountain areas in my terrain object and box colliders would totally be out of the question.
I was thinking of using the Mesh Collider in Unity. I tried experimenting with it but failed. There are also no clear tutorials online on the said situation. How can I add a custom collider to this terrain so the player can walk to it, plus without using the hastly Box Collider.
Here is my terrain object in Blender:
As you can see, putting a box collider is totally impossible.
You can make individual Sphere, Capsule and Box Colliders, hide their Mesh Renderers and then rotate, scale and place them to make something similar to your mesh. However, Mesh Colliders should be your best option, after adding a mesh collider to your mesh, make sure the convex option is NOT checked and also make sure to select the same mesh that the GameObject is using from the Mesh field in the Mesh Collider component.
Alternatively, if you want your collider to be simpler than your actual mesh, you could make a simpler mesh (either with an external program or by downloading the ProBuilder package in Unity), overlay that mesh with the terrain you have, give it a Mesh Collider and then hide its Mesh Renderer component.
I don't exactly understand what you meant by "I tried experimenting with it but failed." Did your character keep falling through? Did the character get stuck? Was the mesh collider generated in an incorrect way? I would be able to help you better if you can give me a detailed explanation.

Unity3d Is there something like Mesh Collider for 2D objects?

Im making simple 2D game, I designed the "way" the player going through in the game.
If the player touches the wall the game is over, so I must detect collisions.
At first I created the walls from sprites, but then I realized there is no 2D collider that suit himself to the shape of the sprit(it was to difficult to create the collider manually from a lot of smaller polygon and circle colliders), so I made all the walls that builds the "way" 3D objects(.dae files with z=0.001), and added them the mash collider which suits himself to the 3D object. I changed my player to be 3D as well so the collisions types will match.
It worked, but I'm sure the same game can work by much better performance with the same resources if all the game objects were 2D.
How can it be that there if a collider that suits himself to a 3D object but there is none for 2D?
Maybe I miss something?
At my first attempt I didn't export the sprite correctly so the polygon collider didn't recognized the shape of my sprite.
I export the sprite as JPEG, when I export it as .png it worked like a charm, just added the Polygon Collider and it match to my sprite shape perfectly, thanks!