What is the proper way of creating 2D Mesh with collider?
All I want to do is create some shapes (not very simple like triangle or rectangle) like slide or rounded rectangle. And I want to define a repeating pattern inside it to fill and reduce memory. Lastly I will need a collider around my shape.
Create the entire thing in a modeling program like Blender... same as you would for a 3D model, but all of your shapes/meshes will be flat. Create your textures the same also... all in your modeling program. Then save your file (.blend for Blender) to your Unity assets folder. Now switch back to Unity and drag/drop the prefab that is created form the .blend file on to your scene. Change the renderer if you want (I use Mobile Unlit, or Unlit Transparent most of the time).
Works just like a normal 2D sprite now... but the texture will repeat and you can make whatever shape you want in your modeling program.
For the collider, you can add any type of collider you want. If your shape isn't standard, you'll probably want an Edge Collider 2D or a Polygon Collider 2D.
I would suggest you the following:
Create a polygonCollider2D directly in unity as you need it
Add a mesh filter and a mesh renderer
Add a custom script made by yourself, in which you convert the Collider polygon to a mesh (Mesh that will be used on your MeshFilter at runtime).
3.1. In order to do this, just copy the vertices of the polygon
3.2. Use the triangulator class in this link to help you create your mesh (http://wiki.unity3d.com/index.php?title=Triangulator)
Enjoy
If you want to see the custom script find it here: http://answers.unity3d.com/questions/835675/how-to-fill-polygon-collider-with-a-solid-color.html
Related
I'm trying to import models from blender to Unity, but the mesh colliders are wrong when they are adding them to an object in Unity.
Here are my steps:
Imported new asset.
Checked "Generate colliders".
Added mesh collider and the result is:
Don't generate the collider when you import the mesh, uncheck the box.
You can later add in the inspector a mesh collider component and set it to convex, but this would be the same result you already have. You can uncheck convex, then unity tries to use your current geometry. But if the vertex count is to hight, unity won't apply a collider because the physic system only supports a certain amount of geometry.
enter image description here
you can also duplicate your object in blender and add a decimate modifier and play around with the Ratio or create your own geometry around your object with vertex snapping or any workflow you like, just keep the collider geometry as simple as possible.
Then you can set that mesh in the mesh collider component as mesh source and have a nice and proper collider.
I want to limit the area where my player can walk. In this case only on red segments.
And I want to use 2D sprites as background images not 3D models. But my main player would be in 3D
Can I use NavMesh here? Or should it be done with Tilemap? Or should I place some 3D objects above it?
here is another scene that I would like to understand how to build
You can try adding invisible colliders attached to an empty gameobject to prevent your character from walking into certain areas.
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.
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.
In Unity 3D I make GameObject → 3D Object → *Cylinder. And this cylinder has the components: Cylinder (Mesh Filter), Capsule Collider, and Mesh Renderer. I can add the last two components to my exported cylinder from Blender, but I can't add the Cylinder (Mesh Filter) component. What is Cylinder (Mesh Filter)? Is it just a Mesh Filter component?
Now I have some problems. I created some Cylinders in Blender. All of them I did using Knife Project. And the last step was to click on Delete → Faces. If I'm not clicking on Delete and make delete faces, it will not cut the hole.
The first problem is in the first cylinder I created in Blender. I cut a simple circle hole, and it's working fine exported it to Unity 3D. But the next cylinders I exported for some reason, when I'm looking at them from the front, are hollow. I see the other side outside, but if I'm moving around from the back, it's not hollow. I am not sure why.
In this screenshot are the cylinders I exported and they are in Unity 3D. The one that is fine is on the left. The others on the right hollow like. The hole cut was to the other side, but if I will look from behind, it's not open in the back:
Back:
The cylinders on the right I changed the circle. The one on the left I didn't change; it only rotated it.
My main goal in the end is to create a cylinder in Blender with just a hole I can walk inside with my character in Unity 3D.
The on the left seems fine, but the hole is too small and high, so I can't walk in.
In Blender it looks fine only in Unity. It looks hollow side to side from the front:
The problem is not the normals.
Unity does not render backfaces. Faces have front and back sides, and game engines don't render back sides by default for optimization reasons. Your mesh is not watertight, and from the hole you are seeing the back side of the faces of the other side.
The first cylinder looks fine, because it has volume, so it's watertight and its backfaces are hidden inside it.
Solution 1: in Unity, set the cylinder's material as double-sided. This will tell Unity to not disregard backfaces that use this material.
Solution 2: in Blender, extrude all faces of the cylinder a bit so that it has some volume to it. This way, the mesh will be watertight, so there will be no backfaces visible (backfaces will be hidden inside the model).
Regarding collision problems;
if you want a character to be able to walk inside the cylinder, but not through its walls, do solution 2 and put a "mesh collider component" on the cylinder in Unity. This will make each face collideable instead of wrapping a simple 3D object around the model as the collider.
You simply need to flip your Normals in Blender. That is the reason you are seeing this effect in the whole of your cylinder. Follow this tutorial for the inside faces of your cylinder.