├── LICENSE ├── README.md └── rope.cs /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rope-in-unity 2 | 3 | Feel free to modiyfy existing code and make pull request . 4 | -------------------------------------------------------------------------------- /rope.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | public class Tentacle : MonoBehaviour { 5 | 6 | [System.SerializableAttribute] 7 | public class TentacleData { 8 | public float length ; 9 | public float radius ; 10 | public float spacing ; 11 | public float friction ; 12 | // public float shade ; 13 | 14 | public List nodes ; 15 | 16 | 17 | public TentacleData(float _length , float _radius , float _spacing , float _friction) { 18 | length = _length ; 19 | radius = _radius ; 20 | spacing = _spacing ; 21 | friction = _friction ; 22 | // shade = _shade ; 23 | nodes = new List() ; 24 | for(var i = 0 ; i < _length ; i++ ) { 25 | nodes.Add(new Node() ); 26 | } 27 | } 28 | public void Move(Vector2 _position) { 29 | nodes[0].x = _position.x ; 30 | nodes[0].y = _position.y ; 31 | } 32 | public void Update(float _radius , float _length , float _friction , float _wind , float _gravity) { 33 | var i = 0 ; 34 | var j = 0 ; 35 | // var n = 0; 36 | var s = 0f; 37 | var c = 0f; 38 | var dx = 0f; 39 | var dy = 0f; 40 | var da = 0f; 41 | var px = 0f; 42 | var py = 0f; 43 | var node = new Node() ; 44 | var prev = nodes[0]; 45 | 46 | _radius = radius * _radius ; 47 | var step = _radius/length ; 48 | for(j = 0 , i = 1 ; i < length ; i++ , j++) { 49 | node = nodes[i] ; 50 | 51 | node.x += node.vx ; 52 | node.y += node.vy ; 53 | 54 | dx = prev.x - node.x ; 55 | dy = prev.y - node.y ; 56 | da = Mathf.Atan2(dy , dx) ; 57 | 58 | px = node.x + Mathf.Cos(da) * spacing * _length ; 59 | py = node.y + Mathf.Sin(da) * spacing * _length ; 60 | 61 | node.x = prev.x - (px - node.x) ; 62 | node.y = prev.y - (py - node.y) ; 63 | 64 | node.vx = node.x - node.ox ; 65 | node.vy = node.y - node.oy ; 66 | 67 | node.vx *= friction * (1-_friction) ; 68 | node.vy *= friction * (1-_friction) ; 69 | 70 | node.vx += _wind ; 71 | node.vy += _gravity ; 72 | 73 | node.ox = node.x ; 74 | node.oy = node.y ; 75 | 76 | s = Mathf.Sin(da + 1.57079632679489661923f) ; 77 | c = Mathf.Cos(da + 1.57079632679489661923f) ; 78 | 79 | // 80 | 81 | radius -= step ; 82 | prev = node ; 83 | } 84 | } 85 | } 86 | [System.SerializableAttribute] 87 | public class Node { 88 | public float x ; 89 | public float y ; 90 | public float ox ; 91 | public float oy ; 92 | public float vx ; 93 | public float vy ; 94 | public Node(float _x , float _y) { 95 | x = _x ; 96 | y = _y ; 97 | ox = _x ; 98 | oy = _y ; 99 | } 100 | public Node() {} 101 | public Vector2 GetVec() { 102 | return new Vector2(x,y); 103 | } 104 | } 105 | public float CustomLength ; 106 | public TentacleData _tentacleData ; 107 | void Start () { 108 | // StartParts() ; 109 | _setting = new Setting(10 , 5 , 0.5f , -0.5f ,0.02f , 2f) ; 110 | _tentacleData = new TentacleData(20 , 2 , 0.5f , 0.2f); 111 | 112 | } 113 | public GameObject origin ; 114 | void Update() { 115 | // UpdateParts() ; 116 | _tentacleData.Move(origin.transform.position) ; 117 | _tentacleData.Update(_setting.radius , _setting.length , _setting.friction, _setting.wind , _setting.gravity) ; 118 | 119 | } 120 | public Setting _setting ; 121 | [System.SerializableAttribute] 122 | public class Setting { 123 | public float length ; 124 | public float radius ; 125 | public float gravity ; 126 | public float wind ; 127 | public float friction ; 128 | public float thickness ; 129 | public Setting(float _length , float _radius , float _gravity , float _wind , float _friction , float _thickness) { 130 | length = _length ; 131 | radius = _radius ; 132 | gravity = _gravity ; 133 | wind = _wind ; 134 | friction = _friction ; 135 | thickness = _thickness ; 136 | } 137 | } 138 | void OnDrawGizmos() { 139 | Gizmos.color = Color.cyan ; 140 | for (int i = 0; i < _tentacleData.nodes.Count - 1; i++) { 141 | Gizmos.DrawLine(_tentacleData.nodes[i].GetVec() , _tentacleData.nodes[i+1].GetVec()); 142 | } 143 | } 144 | } 145 | --------------------------------------------------------------------------------