- Home /
How do you bend a cylindrical gameobject?
I'm trying to simply bend a cylinder in C#. The only way I can see to do this is to use mesh vertices.
void Update () {
Mesh cyl = GetComponent<MeshFilter> ().mesh;
Vector3[] vertices = cyl.vertices;
int i = 0;
Debug.Log (vertices.Length);
while(i < vertices.Length){
vertices[i] += Vector3.up * Time.deltaTime;
i++;
}
cyl.vertices = vertices;
cyl.RecalculateBounds ();
}
Obviously this code moves the cylinder up. I want to bend, but I'm not sure how to select certain vertices and then move them appropriately. Is it even possible to deform the cylinder with this method? If not how can it be done?
Thanks for your response, I'm using the built in cylinder. It says that it has a 88 vertices though. At least where I print the length of the vertices.
Debug.Log(vertices.Length);
After that though, how would I select certain vertices? like say the top half?
edit-- I'm going to try to import a cylinder from $$anonymous$$aya
You select verts by their position in relation to the anchor/origin(all the same thing just different names) point. All verts coordinates are in local space and calculated in relation to this anchor point which is always position (0, 0, 0) of your model. So if you wanted all the top half verts you would select all the verts that have a greater y value than 0(assu$$anonymous$$g that you don't get any axis mix ups which may happen, in which case it will be either x or z rather than y).
I believe the built-in cylinder has verts at just the top and the bottom. There's no ring of verts in the middle. You will need rings of verts as bend points.
@robertbu: I know it only has vertices at the top and bottom, so you're right, there's no way around using a custom mesh or a procedurally generated mesh.
edit
I've edited my comment as i don't like posts with 20+ comments...
@detroit91:
Can you be a bit more specific about your "bending"? There are like ten million ways how to bend an object. Bending usually happens either by having a "fix end" and a moving & bending end and a bending direction which goes from the fixed to the moving end. This line is bend in one or two directions.
The second way is to kind or "warp" an object around an arbitrary axis. The first case is the more common one.
robertbu is right you can see the wireframe of the cylinder when you create it in unity. top and bottom only.
You'll need to create it yourself in a 3d modeler. A good free one is blender.
If it needs to be dynamic then create one in let's say blender with multiple "ribs" and alter it's shape with code as needed.
Answer by robertbu · Nov 13, 2014 at 07:22 PM
Is the 'cly' object Unity's build-in cyclinder, or a cylinder you created? I don't believe the built-in cylinder has any vertices on the sides...just top and bottom. You are going to need vertices at each bend point. You can author your tube in a 3D modeling program, or you could create one from script. The Tube object at the following link will give you a starting point, though you will need to expand it:
Your answer
Follow this Question
Related Questions
Mesh deformation simulating a fire 0 Answers
Mesh Deformation 2 Answers
Expanding mesh animation 0 Answers
Adding vertices to procedural mesh generator 2 Answers
Handles does not modify mesh... 1 Answer