Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nimach · Feb 16, 2013 at 03:54 PM · bendtube

[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.

alt text

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?

bend.jpg (182.1 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daniel G · Jul 08, 2013 at 05:10 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kosukito · Apr 23, 2017 at 08:13 PM 0
Share

thank you! it was just what i was looking for

avatar image captin2osha · Mar 24, 2018 at 08:04 PM 0
Share

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

avatar image RobAnthem captin2osha · Mar 24, 2018 at 08:11 PM 1
Share

You should never change a mesh in an update function, that is going to kill performance.

avatar image captin2osha · Mar 27, 2018 at 05:12 PM 0
Share

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

avatar image nimach captin2osha · Apr 13, 2018 at 05:17 PM 0
Share

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).

avatar image nadeemsaleem · Mar 12, 2020 at 02:33 PM 0
Share

@nimach How can I bend mesh and roll like carpet?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges