[Solved] Bend Object by script

hello guys ,

I get a script from some page about bending objects in unity but when I test it for my tube, my tube bend but not how I want it. here is a screen shot from it.

And the script is:

    var Bend : float = 0.7;
    internal var baseVertices : Vector3[];
    
    function Start ()
    {
    	var mesh : Mesh = GetComponent(MeshFilter).mesh; //get mesh from transform's component
    	if (baseVertices == null) baseVertices = mesh.vertices; //get mesh vertices
    	var changedVertices = new Vector3[baseVertices.Length]; //avoid changing base vertices
    	
    	for (var i=0; i < baseVertices.Length; i++)
    	{
    	     changedVertices <em>= DoTwist(baseVertices<em>, baseVertices_.z * Bend); //twist vertices_</em></em>

* }*
* mesh.vertices = changedVertices;*
}

function DoTwist(pos : Vector3 , t : float)
{
* var new_pos : Vector3 = Vector3.zero;
new_pos.x = pos.x * Mathf.Cos(t) - pos.z * Mathf.Sin(t);
new_pos.z = pos.x * Mathf.Sin(t) + pos.z * Mathf.Cos(t);
new_pos.y = pos.y;*

* return new_pos;
_}*
So how I can fix the script for smoother bending?_

I finally write the code to bend any object. it took me days but it’s worth it.
It works perfectly.

here it is in C#:

public class Bend : MonoBehaviour
{
	public enum BendAxis {X, Y, Z};

	public float rotate = 90;
	public float fromPosition = 0.5F; //from 0 to 1
	public BendAxis axis = BendAxis.X;
	Mesh mesh;
	Vector3[] vertices;
	
	void Start()
	{
		mesh = GetComponent<MeshFilter>().mesh;
		vertices = mesh.vertices;

		if (axis == BendAxis.X)
		{
			float meshWidth = mesh.bounds.size.z;
			for (var i = 0; i < vertices.Length; i++)
			{
				float formPos = Mathf.Lerp(meshWidth / 2, -meshWidth / 2, fromPosition);
				float zeroPos = vertices*.z + formPos;*

_ float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);_

zeroPos -= 2 * vertices_.x * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);_

vertices_.x += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices.z = zeroPos * Mathf.Cos(rotateValue * Mathf.Deg2Rad) - formPos;
* }
}
else if (axis == BendAxis.Y)
{
float meshWidth = mesh.bounds.size.z;
for (var i = 0; i < vertices.Length; i++)
{
float formPos = Mathf.Lerp(meshWidth / 2, -meshWidth / 2, fromPosition);
float zeroPos = vertices.z + formPos;*

float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);_

zeroPos -= 2 * vertices_.y * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);_

vertices_.y += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices.z = zeroPos * Mathf.Cos(rotateValue * Mathf.Deg2Rad) - formPos;
* }
}
else if (axis == BendAxis.Z)
{
float meshWidth = mesh.bounds.size.x;
for (var i = 0; i < vertices.Length; i++)
{
float formPos = Mathf.Lerp(meshWidth / 2, -meshWidth / 2, fromPosition);
float zeroPos = vertices.x + formPos;*

float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);_

zeroPos -= 2 * vertices_.y * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);_

vertices_.y += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices.x = zeroPos * Mathf.Cos(rotateValue * Mathf.Deg2Rad) - formPos;
* }
}*_

* mesh.vertices = vertices;*
* mesh.RecalculateBounds ();*
* }*
}

you can change axis from x to y and z. fromPosition is determine from what position it will bend (0 is the beginning of the object and 1 is the end).
enjoy it.

The problem is (it is seen by picture) that you bend tube up to 180 degrees, but turn all vertices only up to 90.