- Home /
[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[i] = DoTwist(baseVertices[i], baseVertices[i].z * Bend); //twist vertices
}
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?
hmm.. Did you ever figure out how to make it do what you wanted, thats certainly the shortest script i have ever seen to do that in unity! That makes me wonder if i could make something like this with handels :D
Answer by nimach · Feb 28, 2015 at 01:38 PM
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[i].z + formPos;
float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);
zeroPos -= 2 * vertices[i].x * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);
vertices[i].x += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices[i].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[i].z + formPos;
float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);
zeroPos -= 2 * vertices[i].y * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);
vertices[i].y += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices[i].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[i].x + formPos;
float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);
zeroPos -= 2 * vertices[i].y * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);
vertices[i].y += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices[i].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 script worked in start method perfectly but when it came to update method it did not function properly at all. Would you please tell me how to do it when it comes to the update method part? thank you very much
You should never change a mesh in an update function, that is going to kill performance.
is there a way for the object to be bent back to its original position?if so please tell me how because im stuck at this
You can use some tricks. copy mesh vertices into another global variable before bending and then set it back when u need it. Or if you are bending object 60 degrees in x axis you can bend it back with -60 degrees in x axis (if it works, I didn't test it).
Answer by Aldeminor · May 16, 2014 at 09:31 PM
The problem is (it is seen by picture) that you bend tube up to 180 degrees, but turn all vertices only up to 90.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092814im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bending a stick problem (Probably easy for an intermediate programmer) 0 Answers
Unity3D Water Bending with Physics 3 Answers
Trees Bend 1 Answer
"Tube Light" hanging in a Rope? 1 Answer
Bend a flower 0 Answers